Home | History | Annotate | Download | only in gyp_gen
      1 #!/usr/bin/python
      2 
      3 # Copyright 2014 Google Inc.
      4 #
      5 # Use of this source code is governed by a BSD-style license that can be
      6 # found in the LICENSE file.
      7 
      8 """
      9 Functions for creating an Android.mk from already created dictionaries.
     10 """
     11 
     12 import os
     13 
     14 def write_group(f, name, items, append):
     15   """Helper function to list all names passed to a variable.
     16 
     17   Args:
     18     f: File open for writing (Android.mk)
     19     name: Name of the makefile variable (e.g. LOCAL_CFLAGS)
     20     items: list of strings to be passed to the variable.
     21     append: Whether to append to the variable or overwrite it.
     22   """
     23   if not items:
     24     return
     25 
     26   # Copy the list so we can prepend it with its name.
     27   items_to_write = list(items)
     28 
     29   if append:
     30     items_to_write.insert(0, '%s +=' % name)
     31   else:
     32     items_to_write.insert(0, '%s :=' % name)
     33 
     34   f.write(' \\\n\t'.join(items_to_write))
     35 
     36   f.write('\n\n')
     37 
     38 
     39 def write_local_vars(f, var_dict, append, name):
     40   """Helper function to write all the members of var_dict to the makefile.
     41 
     42   Args:
     43     f: File open for writing (Android.mk)
     44     var_dict: VarsDict holding the unique values for one configuration.
     45     append: Whether to append to each makefile variable or overwrite it.
     46     name: If not None, a string to be appended to each key.
     47   """
     48   for key in var_dict.keys():
     49     _key = key
     50     _items = var_dict[key]
     51     if key == 'LOCAL_CFLAGS':
     52       # Always append LOCAL_CFLAGS. This allows us to define some early on in
     53       # the makefile and not overwrite them.
     54       _append = True
     55     elif key == 'DEFINES':
     56       # For DEFINES, we want to append to LOCAL_CFLAGS.
     57       _append = True
     58       _key = 'LOCAL_CFLAGS'
     59       _items_with_D = []
     60       for define in _items:
     61         _items_with_D.append('-D' + define)
     62       _items = _items_with_D
     63     elif key == 'KNOWN_TARGETS':
     64       # KNOWN_TARGETS are not needed in the final make file.
     65       continue
     66     else:
     67       _append = append
     68     if name:
     69       _key += '_' + name
     70     write_group(f, _key, _items, _append)
     71 
     72 
     73 AUTOGEN_WARNING = (
     74 """
     75 ###############################################################################
     76 #
     77 # THIS FILE IS AUTOGENERATED BY GYP_TO_ANDROID.PY. DO NOT EDIT.
     78 #
     79 ###############################################################################
     80 
     81 """
     82 )
     83 
     84 DEBUGGING_HELP = (
     85 """
     86 ###############################################################################
     87 #
     88 # PROBLEMS WITH SKIA DEBUGGING?? READ THIS...
     89 #
     90 # The debug build results in changes to the Skia headers. This means that those
     91 # using libskia must also be built with the debug version of the Skia headers.
     92 # There are a few scenarios where this comes into play:
     93 #
     94 # (1) You're building debug code that depends on libskia.
     95 #   (a) If libskia is built in release, then define SK_RELEASE when building
     96 #       your sources.
     97 #   (b) If libskia is built with debugging (see step 2), then no changes are
     98 #       needed since your sources and libskia have been built with SK_DEBUG.
     99 # (2) You're building libskia in debug mode.
    100 #   (a) RECOMMENDED: You can build the entire system in debug mode. Do this by
    101 #       updating your build/core/config.mk to include -DSK_DEBUG on the line
    102 #       that defines COMMON_GLOBAL_CFLAGS
    103 #   (b) You can update all the users of libskia to define SK_DEBUG when they are
    104 #       building their sources.
    105 #
    106 # NOTE: If neither SK_DEBUG or SK_RELEASE are defined then Skia checks NDEBUG to
    107 #       determine which build type to use.
    108 ###############################################################################
    109 
    110 """
    111 )
    112 
    113 SKIA_TOOLS = (
    114 """
    115 #############################################################
    116 # Build the skia tools
    117 #
    118 
    119 # benchmark (timings)
    120 include $(BASE_PATH)/bench/Android.mk
    121 
    122 # golden-master (fidelity / regression test)
    123 include $(BASE_PATH)/gm/Android.mk
    124 
    125 # unit-tests
    126 include $(BASE_PATH)/tests/Android.mk
    127 
    128 # diamond-master (one test to rule them all)
    129 include $(BASE_PATH)/dm/Android.mk
    130 """
    131 )
    132 
    133 
    134 class VarsDictData(object):
    135   """Helper class to keep a VarsDict along with a name and optional condition.
    136   """
    137   def __init__(self, vars_dict, name, condition=None):
    138     """Create a new VarsDictData.
    139 
    140     Args:
    141       vars_dict: A VarsDict. Can be accessed via self.vars_dict.
    142       name: Name associated with the VarsDict. Can be accessed via
    143         self.name.
    144       condition: Optional string representing a condition. If not None,
    145         used to create a conditional inside the makefile.
    146     """
    147     self.vars_dict = vars_dict
    148     self.condition = condition
    149     self.name = name
    150 
    151 def write_local_path(f):
    152     """Add the LOCAL_PATH line to the makefile.
    153 
    154     Args:
    155       f: File open for writing.
    156     """
    157     f.write('LOCAL_PATH:= $(call my-dir)\n')
    158 
    159 def write_clear_vars(f):
    160     """Add the CLEAR_VARS line to the makefile.
    161 
    162     Args:
    163       f: File open for writing.
    164     """
    165     f.write('include $(CLEAR_VARS)\n')
    166 
    167 def write_include_stlport(f):
    168     """Add a line to include stlport.
    169 
    170     Args:
    171       f: File open for writing.
    172     """
    173     f.write('include external/stlport/libstlport.mk\n')
    174 
    175 def write_android_mk(target_dir, common, deviations_from_common):
    176   """Given all the variables, write the final make file.
    177 
    178   Args:
    179     target_dir: The full path to the directory to write Android.mk, or None
    180       to use the current working directory.
    181     common: VarsDict holding variables definitions common to all
    182       configurations.
    183     deviations_from_common: List of VarsDictData, one for each possible
    184       configuration. VarsDictData.name will be appended to each key before
    185       writing it to the makefile. VarsDictData.condition, if not None, will be
    186       written to the makefile as a condition to determine whether to include
    187       VarsDictData.vars_dict.
    188   """
    189   target_file = 'Android.mk'
    190   if target_dir:
    191     target_file = os.path.join(target_dir, target_file)
    192   with open(target_file, 'w') as f:
    193     f.write(AUTOGEN_WARNING)
    194     f.write('BASE_PATH := $(call my-dir)\n')
    195     write_local_path(f)
    196 
    197     f.write(DEBUGGING_HELP)
    198 
    199     write_clear_vars(f)
    200     f.write('LOCAL_ARM_MODE := thumb\n')
    201 
    202     # need a flag to tell the C side when we're on devices with large memory
    203     # budgets (i.e. larger than the low-end devices that initially shipped)
    204     # On arm, only define the flag if it has VFP. For all other architectures,
    205     # always define the flag.
    206     f.write('ifeq ($(TARGET_ARCH),arm)\n')
    207     f.write('\tifeq ($(ARCH_ARM_HAVE_VFP),true)\n')
    208     f.write('\t\tLOCAL_CFLAGS += -DANDROID_LARGE_MEMORY_DEVICE\n')
    209     f.write('\tendif\n')
    210     f.write('else\n')
    211     f.write('\tLOCAL_CFLAGS += -DANDROID_LARGE_MEMORY_DEVICE\n')
    212     f.write('endif\n\n')
    213 
    214     f.write('# used for testing\n')
    215     f.write('#LOCAL_CFLAGS += -g -O0\n\n')
    216 
    217     f.write('ifeq ($(NO_FALLBACK_FONT),true)\n')
    218     f.write('\tLOCAL_CFLAGS += -DNO_FALLBACK_FONT\n')
    219     f.write('endif\n\n')
    220 
    221     write_local_vars(f, common, False, None)
    222 
    223     for data in deviations_from_common:
    224       if data.condition:
    225         f.write('ifeq ($(%s), true)\n' % data.condition)
    226       write_local_vars(f, data.vars_dict, True, data.name)
    227       if data.condition:
    228         f.write('endif\n\n')
    229 
    230     write_include_stlport(f)
    231     f.write('include $(BUILD_SHARED_LIBRARY)\n')
    232     f.write(SKIA_TOOLS)
    233 
    234