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

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

Issue 687633003: Greatly improve (non-android) java support (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 6 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 There are several different types of build_configs:
16 android_library: An android library containing java code.
17 android_resources: A target containing android resources.
18
19 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
20 build specification should instead pass information in using the special 16 build specification should instead pass information in using the special
21 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
22 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:
23 --python-arg=@FileArg(build_config_path:javac:classpath) 19 --python-arg=@FileArg(build_config_path:javac:classpath)
24 20
25 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:
26 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
27 time the action runs. 23 time the action runs.
28 2. Either (a) or (b) 24 2. Either (a) or (b)
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 'dependencies may not write build_config files. Missing build_config ' 65 'dependencies may not write build_config files. Missing build_config '
70 'files are handled differently based on the type of this target.') 66 'files are handled differently based on the type of this target.')
71 67
72 # android_resources options 68 # android_resources options
73 parser.add_option('--srcjar', help='Path to target\'s resources srcjar.') 69 parser.add_option('--srcjar', help='Path to target\'s resources srcjar.')
74 parser.add_option('--resources-zip', help='Path to target\'s resources zip.') 70 parser.add_option('--resources-zip', help='Path to target\'s resources zip.')
75 parser.add_option('--package-name', 71 parser.add_option('--package-name',
76 help='Java package name for these resources.') 72 help='Java package name for these resources.')
77 parser.add_option('--android-manifest', help='Path to android manifest.') 73 parser.add_option('--android-manifest', help='Path to android manifest.')
78 74
79 # android_library/apk options 75 # java library options
80 parser.add_option('--jar-path', help='Path to target\'s jar output.') 76 parser.add_option('--jar-path', help='Path to target\'s jar output.')
77 parser.add_option('--supports-android', action='store_true',
78 help='Whether this library supports running on the Android platform.')
79 parser.add_option('--requires-android', action='store_true',
80 help='Whether this library requires running on the Android platform.')
81
82 # android library options
81 parser.add_option('--dex-path', help='Path to target\'s dex output.') 83 parser.add_option('--dex-path', help='Path to target\'s dex output.')
82 84
83 # apk native library options 85 # native library options
84 parser.add_option('--native-libs', help='List of top-level native libs.') 86 parser.add_option('--native-libs', help='List of top-level native libs.')
85 parser.add_option('--readelf-path', help='Path to toolchain\'s readelf.') 87 parser.add_option('--readelf-path', help='Path to toolchain\'s readelf.')
86 88
87 options, args = parser.parse_args(argv) 89 options, args = parser.parse_args(argv)
88 90
89 if args: 91 if args:
90 parser.error('No positional arguments should be given.') 92 parser.error('No positional arguments should be given.')
91 93
92 94
93 if not options.type in [ 95 if not options.type in [
94 'android_library', 'android_resources', 'android_apk']: 96 'java_library', 'android_resources', 'android_apk']:
95 raise Exception('Unknown type: <%s>' % options.type) 97 raise Exception('Unknown type: <%s>' % options.type)
96 98
97
98 required_options = ['build_config'] + { 99 required_options = ['build_config'] + {
99 'android_library': ['jar_path', 'dex_path'], 100 'java_library': ['jar_path'],
100 'android_resources': ['resources_zip'], 101 'android_resources': ['resources_zip'],
101 'android_apk': ['jar_path', 'dex_path', 'resources_zip'] 102 'android_apk': ['jar_path', 'dex_path', 'resources_zip']
102 }[options.type] 103 }[options.type]
103 104
104 if options.native_libs: 105 if options.native_libs:
105 required_options += ['readelf_path'] 106 required_options.append('readelf_path')
106 107
107 build_utils.CheckOptions(options, parser, required_options) 108 build_utils.CheckOptions(options, parser, required_options)
108 109
110 if options.type == 'java_library':
111 if options.supports_android and not options.dex_path:
112 raise Exception('java_library that supports Android requires a dex path.')
113
114 if options.requires_android and not options.supports_android:
115 raise Exception(
116 '--supports-android is required when using --requires-android')
117
109 possible_deps_config_paths = build_utils.ParseGypList( 118 possible_deps_config_paths = build_utils.ParseGypList(
110 options.possible_deps_configs) 119 options.possible_deps_configs)
111 120
112
113 allow_unknown_deps = options.type == 'android_apk' 121 allow_unknown_deps = options.type == 'android_apk'
114 unknown_deps = [ 122 unknown_deps = [
115 c for c in possible_deps_config_paths if not os.path.exists(c)] 123 c for c in possible_deps_config_paths if not os.path.exists(c)]
116 if unknown_deps and not allow_unknown_deps: 124 if unknown_deps and not allow_unknown_deps:
117 raise Exception('Unknown deps: ' + str(unknown_deps)) 125 raise Exception('Unknown deps: ' + str(unknown_deps))
118 126
119 direct_deps_config_paths = [ 127 direct_deps_config_paths = [
120 c for c in possible_deps_config_paths if not c in unknown_deps] 128 c for c in possible_deps_config_paths if not c in unknown_deps]
121 all_deps_config_paths = GetAllDepsConfigsInOrder(direct_deps_config_paths) 129 all_deps_config_paths = GetAllDepsConfigsInOrder(direct_deps_config_paths)
122 130
123 direct_deps_configs = [GetDepConfig(p) for p in direct_deps_config_paths] 131 direct_deps_configs = [GetDepConfig(p) for p in direct_deps_config_paths]
124 all_deps_configs = [GetDepConfig(p) for p in all_deps_config_paths] 132 all_deps_configs = [GetDepConfig(p) for p in all_deps_config_paths]
125 133
126 direct_library_deps = DepsOfType('android_library', direct_deps_configs) 134 direct_library_deps = DepsOfType('java_library', direct_deps_configs)
127 all_library_deps = DepsOfType('android_library', all_deps_configs) 135 all_library_deps = DepsOfType('java_library', all_deps_configs)
128 136
129 direct_resources_deps = DepsOfType('android_resources', direct_deps_configs) 137 direct_resources_deps = DepsOfType('android_resources', direct_deps_configs)
130 all_resources_deps = DepsOfType('android_resources', all_deps_configs) 138 all_resources_deps = DepsOfType('android_resources', all_deps_configs)
131 139
132 # Initialize some common config. 140 # Initialize some common config.
133 config = { 141 config = {
134 'deps_info': { 142 'deps_info': {
143 'name': os.path.basename(options.build_config),
135 'path': options.build_config, 144 'path': options.build_config,
136 'type': options.type, 145 'type': options.type,
137 'deps_configs': direct_deps_config_paths, 146 'deps_configs': direct_deps_config_paths,
138 } 147 }
139 } 148 }
140 deps_info = config['deps_info'] 149 deps_info = config['deps_info']
141 150
142 if options.type in ['android_library', 'android_apk']: 151
152 if options.type == 'java_library':
153 deps_info['requires_android'] = options.requires_android
154 deps_info['supports_android'] = options.supports_android
155
156 deps_require_android = (all_resources_deps +
157 [d['name'] for d in direct_library_deps if d['requires_android']])
158 deps_not_support_android = (
159 [d['name'] for d in direct_library_deps if not d['supports_android']])
160
161 if deps_require_android and not options.requires_android:
162 raise Exception('Some deps require building for the Android platform: ' +
163 str(deps_require_android))
164
165 if deps_not_support_android and options.supports_android:
166 raise Exception('Not all deps support the Android platform: ' +
167 str(deps_not_support_android))
168
169
170 if options.type in ['java_library', 'android_apk']:
143 javac_classpath = [c['jar_path'] for c in direct_library_deps] 171 javac_classpath = [c['jar_path'] for c in direct_library_deps]
172 java_full_classpath = [c['jar_path'] for c in all_library_deps]
144 deps_info['resources_deps'] = [c['path'] for c in all_resources_deps] 173 deps_info['resources_deps'] = [c['path'] for c in all_resources_deps]
145 deps_info['jar_path'] = options.jar_path 174 deps_info['jar_path'] = options.jar_path
146 deps_info['dex_path'] = options.dex_path 175 if options.type == 'android_apk' or options.supports_android:
176 deps_info['dex_path'] = options.dex_path
147 config['javac'] = { 177 config['javac'] = {
148 'classpath': javac_classpath, 178 'classpath': javac_classpath,
149 } 179 }
180 config['java'] = {
181 'full_classpath': java_full_classpath
182 }
150 183
151 if options.type == 'android_library': 184 if options.type == 'java_library':
152 # Only resources might have srcjars (normal srcjar targets are listed in 185 # Only resources might have srcjars (normal srcjar targets are listed in
153 # srcjar_deps). A resource's srcjar contains the R.java file for those 186 # srcjar_deps). A resource's srcjar contains the R.java file for those
154 # resources, and (like Android's default build system) we allow a library to 187 # resources, and (like Android's default build system) we allow a library to
155 # refer to the resources in any of its dependents. 188 # refer to the resources in any of its dependents.
156 config['javac']['srcjars'] = [ 189 config['javac']['srcjars'] = [
157 c['srcjar'] for c in direct_resources_deps if 'srcjar' in c] 190 c['srcjar'] for c in direct_resources_deps if 'srcjar' in c]
158 191
159 if options.type == 'android_apk': 192 if options.type == 'android_apk':
193 # Apks will get their resources srcjar explicitly passed to the java step.
160 config['javac']['srcjars'] = [] 194 config['javac']['srcjars'] = []
161 195
162
163 if options.type == 'android_resources': 196 if options.type == 'android_resources':
164 deps_info['resources_zip'] = options.resources_zip 197 deps_info['resources_zip'] = options.resources_zip
165 if options.srcjar: 198 if options.srcjar:
166 deps_info['srcjar'] = options.srcjar 199 deps_info['srcjar'] = options.srcjar
167 if options.package_name: 200 if options.package_name:
168 deps_info['package_name'] = options.package_name 201 deps_info['package_name'] = options.package_name
169 202
170 if options.type == 'android_resources' or options.type == 'android_apk': 203 if options.type == 'android_resources' or options.type == 'android_apk':
171 config['resources'] = {} 204 config['resources'] = {}
172 config['resources']['dependency_zips'] = [ 205 config['resources']['dependency_zips'] = [
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 build_utils.WriteJson(config, options.build_config, only_if_changed=True) 250 build_utils.WriteJson(config, options.build_config, only_if_changed=True)
218 251
219 if options.depfile: 252 if options.depfile:
220 build_utils.WriteDepfile( 253 build_utils.WriteDepfile(
221 options.depfile, 254 options.depfile,
222 all_deps_config_paths + build_utils.GetPythonDependencies()) 255 all_deps_config_paths + build_utils.GetPythonDependencies())
223 256
224 257
225 if __name__ == '__main__': 258 if __name__ == '__main__':
226 sys.exit(main(sys.argv[1:])) 259 sys.exit(main(sys.argv[1:]))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698