Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: build/android/gyp/write_build_config.py

Issue 1418243003: Add GN template for android_assets(). Use it in content_shell_apk. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: outputs -> renaming_destination Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # 2 #
3 # Copyright 2014 The Chromium Authors. All rights reserved. 3 # Copyright 2014 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be 4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file. 5 # found in the LICENSE file.
6 6
7 """Writes a build_config file. 7 """Writes a build_config file.
8 8
9 The build_config file for a target is a json file containing information about 9 The build_config file for a target is a json file containing information about
10 how to build that target based on the target's dependencies. This includes 10 how to build that target based on the target's dependencies. This includes
11 things like: the javac classpath, the list of android resources dependencies, 11 things like: the javac classpath, the list of android resources dependencies,
12 etc. It also includes the information needed to create the build_config for 12 etc. It also includes the information needed to create the build_config for
13 other targets that depend on that one. 13 other targets that depend on that one.
14 14
15 Android build scripts should not refer to the build_config directly, and the 15 Android build scripts should not refer to the build_config directly, and the
16 build specification should instead pass information in using the special 16 build specification should instead pass information in using the special
17 file-arg syntax (see build_utils.py:ExpandFileArgs). That syntax allows passing 17 file-arg syntax (see build_utils.py:ExpandFileArgs). That syntax allows passing
18 of values in a json dict in a file and looks like this: 18 of values in a json dict in a file and looks like this:
19 --python-arg=@FileArg(build_config_path:javac:classpath) 19 --python-arg=@FileArg(build_config_path:javac:classpath)
20 20
21 Note: If paths to input files are passed in this way, it is important that: 21 Note: If paths to input files are passed in this way, it is important that:
22 1. inputs/deps of the action ensure that the files are available the first 22 1. inputs/deps of the action ensure that the files are available the first
23 time the action runs. 23 time the action runs.
24 2. Either (a) or (b) 24 2. Either (a) or (b)
25 a. inputs/deps ensure that the action runs whenever one of the files changes 25 a. inputs/deps ensure that the action runs whenever one of the files changes
26 b. the files are added to the action's depfile 26 b. the files are added to the action's depfile
27 """ 27 """
28 28
29 import itertools
29 import optparse 30 import optparse
30 import os 31 import os
31 import sys 32 import sys
32 import xml.dom.minidom 33 import xml.dom.minidom
33 34
34 from util import build_utils 35 from util import build_utils
35 from util import md5_check 36 from util import md5_check
36 37
37 import write_ordered_libraries 38 import write_ordered_libraries
38 39
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 102
102 def Direct(self, wanted_type=None): 103 def Direct(self, wanted_type=None):
103 if wanted_type is None: 104 if wanted_type is None:
104 return self.direct_deps_configs 105 return self.direct_deps_configs
105 return DepsOfType(wanted_type, self.direct_deps_configs) 106 return DepsOfType(wanted_type, self.direct_deps_configs)
106 107
107 def AllConfigPaths(self): 108 def AllConfigPaths(self):
108 return self.all_deps_config_paths 109 return self.all_deps_config_paths
109 110
110 111
112 def _MergeAssets(all_assets):
113 """Merges all assets from the given deps.
114
115 Returns:
116 A tuple of lists: (compressed, uncompressed)
117 Each tuple entry is a list of "srcPath:zipPath", where ":zipPath" is
118 optional.
pkotwicz 2015/10/30 20:35:53 Nit: For simplicity make zipPath non optional
agrieve 2015/10/31 02:40:03 Done.
119 """
120 compressed = {}
121 uncompressed = {}
122 for asset_dep in all_assets:
123 entry = asset_dep['assets']
124 disable_compression = entry.get('disable_compression', False)
125 dest_map = uncompressed if disable_compression else compressed
126 other_map = compressed if disable_compression else uncompressed
127 outputs = entry.get('outputs', [])
128 for src, dest in itertools.izip_longest(entry['paths'], outputs):
129 if not dest:
130 dest = os.path.basename(src)
131 # Merge so that each path shows up in only one of the lists, and that
132 # deps of the same target override previous ones.
133 other_map.pop(dest, 0)
134 dest_map[dest] = src
135
136 def create_list(asset_map):
137 ret = []
138 for dest, src in asset_map.iteritems():
pkotwicz 2015/10/30 20:35:54 I think that this would be clearer: for dest, src
agrieve 2015/10/31 02:40:03 Done.
139 ret.append(src)
140 if dest != os.path.basename(src):
141 ret[-1] += ':%s' % dest
142 # Sort to ensure deterministic ordering.
143 ret.sort()
144 return ret
145
146 return create_list(compressed), create_list(uncompressed)
147
148
111 def main(argv): 149 def main(argv):
112 parser = optparse.OptionParser() 150 parser = optparse.OptionParser()
113 build_utils.AddDepfileOption(parser) 151 build_utils.AddDepfileOption(parser)
114 parser.add_option('--build-config', help='Path to build_config output.') 152 parser.add_option('--build-config', help='Path to build_config output.')
115 parser.add_option( 153 parser.add_option(
116 '--type', 154 '--type',
117 help='Type of this target (e.g. android_library).') 155 help='Type of this target (e.g. android_library).')
118 parser.add_option( 156 parser.add_option(
119 '--possible-deps-configs', 157 '--possible-deps-configs',
120 help='List of paths for dependency\'s build_config files. Some ' 158 help='List of paths for dependency\'s build_config files. Some '
121 'dependencies may not write build_config files. Missing build_config ' 159 'dependencies may not write build_config files. Missing build_config '
122 'files are handled differently based on the type of this target.') 160 'files are handled differently based on the type of this target.')
123 161
124 # android_resources options 162 # android_resources options
125 parser.add_option('--srcjar', help='Path to target\'s resources srcjar.') 163 parser.add_option('--srcjar', help='Path to target\'s resources srcjar.')
126 parser.add_option('--resources-zip', help='Path to target\'s resources zip.') 164 parser.add_option('--resources-zip', help='Path to target\'s resources zip.')
127 parser.add_option('--r-text', help='Path to target\'s R.txt file.') 165 parser.add_option('--r-text', help='Path to target\'s R.txt file.')
128 parser.add_option('--package-name', 166 parser.add_option('--package-name',
129 help='Java package name for these resources.') 167 help='Java package name for these resources.')
130 parser.add_option('--android-manifest', help='Path to android manifest.') 168 parser.add_option('--android-manifest', help='Path to android manifest.')
131 169
170 # android_assets options
171 parser.add_option('--asset-sources', help='List of asset sources.')
172 parser.add_option('--asset-renaming-sources',
173 help='List of asset sources with custom destinations.')
174 parser.add_option('--asset-renaming-destinations',
175 help='List of asset custom destinations.')
176 parser.add_option('--disable-asset-compression', action='store_true',
177 help='Whether to disable asset compression.')
178
132 # java library options 179 # java library options
133 parser.add_option('--jar-path', help='Path to target\'s jar output.') 180 parser.add_option('--jar-path', help='Path to target\'s jar output.')
134 parser.add_option('--supports-android', action='store_true', 181 parser.add_option('--supports-android', action='store_true',
135 help='Whether this library supports running on the Android platform.') 182 help='Whether this library supports running on the Android platform.')
136 parser.add_option('--requires-android', action='store_true', 183 parser.add_option('--requires-android', action='store_true',
137 help='Whether this library requires running on the Android platform.') 184 help='Whether this library requires running on the Android platform.')
138 parser.add_option('--bypass-platform-checks', action='store_true', 185 parser.add_option('--bypass-platform-checks', action='store_true',
139 help='Bypass checks for support/require Android platform.') 186 help='Bypass checks for support/require Android platform.')
140 187
141 # android library options 188 # android library options
142 parser.add_option('--dex-path', help='Path to target\'s dex output.') 189 parser.add_option('--dex-path', help='Path to target\'s dex output.')
143 190
144 # native library options 191 # native library options
145 parser.add_option('--native-libs', help='List of top-level native libs.') 192 parser.add_option('--native-libs', help='List of top-level native libs.')
146 parser.add_option('--readelf-path', help='Path to toolchain\'s readelf.') 193 parser.add_option('--readelf-path', help='Path to toolchain\'s readelf.')
147 194
148 parser.add_option('--tested-apk-config', 195 parser.add_option('--tested-apk-config',
149 help='Path to the build config of the tested apk (for an instrumentation ' 196 help='Path to the build config of the tested apk (for an instrumentation '
150 'test apk).') 197 'test apk).')
151 198
152 options, args = parser.parse_args(argv) 199 options, args = parser.parse_args(argv)
153 200
154 if args: 201 if args:
155 parser.error('No positional arguments should be given.') 202 parser.error('No positional arguments should be given.')
156 203
157 204 required_options_map = {
158 if not options.type in [ 205 'java_library': ['build_config', 'jar_path'],
159 'java_library', 'android_resources', 'android_apk', 'deps_dex']: 206 'android_assets': ['build_config'],
207 'android_resources': ['build_config', 'resources_zip'],
208 'android_apk': ['build_config', 'jar_path', 'dex_path', 'resources_zip'],
209 'deps_dex': ['build_config', 'dex_path']
210 }
211 required_options = required_options_map.get(options.type)
212 if not required_options:
160 raise Exception('Unknown type: <%s>' % options.type) 213 raise Exception('Unknown type: <%s>' % options.type)
161 214
162 required_options = ['build_config'] + {
163 'java_library': ['jar_path'],
164 'android_resources': ['resources_zip'],
165 'android_apk': ['jar_path', 'dex_path', 'resources_zip'],
166 'deps_dex': ['dex_path']
167 }[options.type]
168
169 if options.native_libs: 215 if options.native_libs:
170 required_options.append('readelf_path') 216 required_options.append('readelf_path')
171 217
172 build_utils.CheckOptions(options, parser, required_options) 218 build_utils.CheckOptions(options, parser, required_options)
173 219
174 if options.type == 'java_library': 220 if options.type == 'java_library':
175 if options.supports_android and not options.dex_path: 221 if options.supports_android and not options.dex_path:
176 raise Exception('java_library that supports Android requires a dex path.') 222 raise Exception('java_library that supports Android requires a dex path.')
177 223
178 if options.requires_android and not options.supports_android: 224 if options.requires_android and not options.supports_android:
179 raise Exception( 225 raise Exception(
180 '--supports-android is required when using --requires-android') 226 '--supports-android is required when using --requires-android')
181 227
182 possible_deps_config_paths = build_utils.ParseGypList( 228 possible_deps_config_paths = build_utils.ParseGypList(
183 options.possible_deps_configs) 229 options.possible_deps_configs)
184 230
185 allow_unknown_deps = (options.type == 'android_apk' or 231 allow_unknown_deps = (options.type in
186 options.type == 'android_resources') 232 ('android_apk', 'android_assets', 'android_resources'))
187 unknown_deps = [ 233 unknown_deps = [
188 c for c in possible_deps_config_paths if not os.path.exists(c)] 234 c for c in possible_deps_config_paths if not os.path.exists(c)]
189 if unknown_deps and not allow_unknown_deps: 235 if unknown_deps and not allow_unknown_deps:
190 raise Exception('Unknown deps: ' + str(unknown_deps)) 236 raise Exception('Unknown deps: ' + str(unknown_deps))
191 237
192 direct_deps_config_paths = [ 238 direct_deps_config_paths = [
193 c for c in possible_deps_config_paths if not c in unknown_deps] 239 c for c in possible_deps_config_paths if not c in unknown_deps]
194 240
195 deps = Deps(direct_deps_config_paths) 241 deps = Deps(direct_deps_config_paths)
196 direct_library_deps = deps.Direct('java_library') 242 direct_library_deps = deps.Direct('java_library')
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 # srcjar_deps). A resource's srcjar contains the R.java file for those 301 # srcjar_deps). A resource's srcjar contains the R.java file for those
256 # resources, and (like Android's default build system) we allow a library to 302 # resources, and (like Android's default build system) we allow a library to
257 # refer to the resources in any of its dependents. 303 # refer to the resources in any of its dependents.
258 config['javac']['srcjars'] = [ 304 config['javac']['srcjars'] = [
259 c['srcjar'] for c in direct_resources_deps if 'srcjar' in c] 305 c['srcjar'] for c in direct_resources_deps if 'srcjar' in c]
260 306
261 if options.type == 'android_apk': 307 if options.type == 'android_apk':
262 # Apks will get their resources srcjar explicitly passed to the java step. 308 # Apks will get their resources srcjar explicitly passed to the java step.
263 config['javac']['srcjars'] = [] 309 config['javac']['srcjars'] = []
264 310
311 if options.type == 'android_assets':
312 assets = []
pkotwicz 2015/10/30 20:35:54 Nit: rename |assets| to |sources|
agrieve 2015/10/31 02:40:03 Done.
313 if options.asset_renaming_sources:
314 assets.extend(build_utils.ParseGypList(options.asset_renaming_sources))
315 if options.asset_sources:
316 assets.extend(build_utils.ParseGypList(options.asset_sources))
317
318 deps_info['assets'] = {
319 'paths': assets
pkotwicz 2015/10/30 20:35:53 Nit: Rename 'paths' key to 'sources'
agrieve 2015/10/31 02:40:03 Done.
320 }
321 if options.asset_renaming_destinations:
322 deps_info['assets']['outputs'] = (
323 build_utils.ParseGypList(options.asset_renaming_destinations))
324 if options.disable_asset_compression:
325 deps_info['assets']['disable_compression'] = True
326
265 if options.type == 'android_resources': 327 if options.type == 'android_resources':
266 deps_info['resources_zip'] = options.resources_zip 328 deps_info['resources_zip'] = options.resources_zip
267 if options.srcjar: 329 if options.srcjar:
268 deps_info['srcjar'] = options.srcjar 330 deps_info['srcjar'] = options.srcjar
269 if options.android_manifest: 331 if options.android_manifest:
270 manifest = AndroidManifest(options.android_manifest) 332 manifest = AndroidManifest(options.android_manifest)
271 deps_info['package_name'] = manifest.GetPackageName() 333 deps_info['package_name'] = manifest.GetPackageName()
272 if options.package_name: 334 if options.package_name:
273 deps_info['package_name'] = options.package_name 335 deps_info['package_name'] = options.package_name
274 if options.r_text: 336 if options.r_text:
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 if not library_paths: 413 if not library_paths:
352 prev_config = build_utils.ReadJson(options.build_config) 414 prev_config = build_utils.ReadJson(options.build_config)
353 java_libraries_list_holder[0] = ( 415 java_libraries_list_holder[0] = (
354 prev_config['native']['java_libraries_list']) 416 prev_config['native']['java_libraries_list'])
355 library_paths.extend(prev_config['native']['libraries']) 417 library_paths.extend(prev_config['native']['libraries'])
356 418
357 config['native'] = { 419 config['native'] = {
358 'libraries': library_paths, 420 'libraries': library_paths,
359 'java_libraries_list': java_libraries_list_holder[0], 421 'java_libraries_list': java_libraries_list_holder[0],
360 } 422 }
423 config['assets'], config['uncompressed_assets'] = (
424 _MergeAssets(deps.All('android_assets')))
361 425
362 build_utils.WriteJson(config, options.build_config, only_if_changed=True) 426 build_utils.WriteJson(config, options.build_config, only_if_changed=True)
363 427
364 if options.depfile: 428 if options.depfile:
365 build_utils.WriteDepfile( 429 build_utils.WriteDepfile(
366 options.depfile, 430 options.depfile,
367 deps.AllConfigPaths() + build_utils.GetPythonDependencies()) 431 deps.AllConfigPaths() + build_utils.GetPythonDependencies())
368 432
369 433
370 if __name__ == '__main__': 434 if __name__ == '__main__':
371 sys.exit(main(sys.argv[1:])) 435 sys.exit(main(sys.argv[1:]))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698