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 # diamond-master (one test to rule them all) 126 include $(BASE_PATH)/dm/Android.mk 127 """ 128 ) 129 130 131 class VarsDictData(object): 132 """Helper class to keep a VarsDict along with a name and optional condition. 133 """ 134 def __init__(self, vars_dict, name, condition=None): 135 """Create a new VarsDictData. 136 137 Args: 138 vars_dict: A VarsDict. Can be accessed via self.vars_dict. 139 name: Name associated with the VarsDict. Can be accessed via 140 self.name. 141 condition: Optional string representing a condition. If not None, 142 used to create a conditional inside the makefile. 143 """ 144 self.vars_dict = vars_dict 145 self.condition = condition 146 self.name = name 147 148 def write_local_path(f): 149 """Add the LOCAL_PATH line to the makefile. 150 151 Args: 152 f: File open for writing. 153 """ 154 f.write('LOCAL_PATH:= $(call my-dir)\n') 155 156 def write_clear_vars(f): 157 """Add the CLEAR_VARS line to the makefile. 158 159 Args: 160 f: File open for writing. 161 """ 162 f.write('include $(CLEAR_VARS)\n') 163 164 def write_include_stlport(f): 165 """Add a line to include stlport. 166 167 Args: 168 f: File open for writing. 169 """ 170 f.write('include external/stlport/libstlport.mk\n') 171 172 def write_android_mk(target_dir, common, deviations_from_common): 173 """Given all the variables, write the final make file. 174 175 Args: 176 target_dir: The full path to the directory to write Android.mk, or None 177 to use the current working directory. 178 common: VarsDict holding variables definitions common to all 179 configurations. 180 deviations_from_common: List of VarsDictData, one for each possible 181 configuration. VarsDictData.name will be appended to each key before 182 writing it to the makefile. VarsDictData.condition, if not None, will be 183 written to the makefile as a condition to determine whether to include 184 VarsDictData.vars_dict. 185 """ 186 target_file = 'Android.mk' 187 if target_dir: 188 target_file = os.path.join(target_dir, target_file) 189 with open(target_file, 'w') as f: 190 f.write(AUTOGEN_WARNING) 191 f.write('BASE_PATH := $(call my-dir)\n') 192 write_local_path(f) 193 194 f.write(DEBUGGING_HELP) 195 196 write_clear_vars(f) 197 198 # need flags to enable feedback driven optimization (FDO) when requested 199 # by the build system. 200 f.write('LOCAL_FDO_SUPPORT := true\n') 201 f.write('ifneq ($(strip $(TARGET_FDO_CFLAGS)),)\n') 202 f.write('\t# This should be the last -Oxxx specified in LOCAL_CFLAGS\n') 203 f.write('\tLOCAL_CFLAGS += -O2\n') 204 f.write('endif\n\n') 205 206 f.write('LOCAL_ARM_MODE := thumb\n') 207 208 # need a flag to tell the C side when we're on devices with large memory 209 # budgets (i.e. larger than the low-end devices that initially shipped) 210 # On arm, only define the flag if it has VFP. For all other architectures, 211 # always define the flag. 212 f.write('ifeq ($(TARGET_ARCH),arm)\n') 213 f.write('\tifeq ($(ARCH_ARM_HAVE_VFP),true)\n') 214 f.write('\t\tLOCAL_CFLAGS += -DANDROID_LARGE_MEMORY_DEVICE\n') 215 f.write('\tendif\n') 216 f.write('else\n') 217 f.write('\tLOCAL_CFLAGS += -DANDROID_LARGE_MEMORY_DEVICE\n') 218 f.write('endif\n\n') 219 220 f.write('# used for testing\n') 221 f.write('#LOCAL_CFLAGS += -g -O0\n\n') 222 223 f.write('ifeq ($(NO_FALLBACK_FONT),true)\n') 224 f.write('\tLOCAL_CFLAGS += -DNO_FALLBACK_FONT\n') 225 f.write('endif\n\n') 226 227 write_local_vars(f, common, False, None) 228 229 for data in deviations_from_common: 230 if data.condition: 231 f.write('ifeq ($(%s), true)\n' % data.condition) 232 write_local_vars(f, data.vars_dict, True, data.name) 233 if data.condition: 234 f.write('endif\n\n') 235 236 write_include_stlport(f) 237 f.write('include $(BUILD_SHARED_LIBRARY)\n') 238 f.write(SKIA_TOOLS) 239 240