OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2013 The Chromium Authors. All rights reserved. | 3 # Copyright 2013 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 import optparse | 7 import optparse |
8 import os | 8 import os |
9 import shutil | 9 import shutil |
10 import re | 10 import re |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 wrapper = textwrap.TextWrapper(break_long_words=True, | 101 wrapper = textwrap.TextWrapper(break_long_words=True, |
102 drop_whitespace=False, | 102 drop_whitespace=False, |
103 subsequent_indent=' ', | 103 subsequent_indent=' ', |
104 width=_MAX_MANIFEST_LINE_LEN - 2) | 104 width=_MAX_MANIFEST_LINE_LEN - 2) |
105 output = '\r\n'.join(w for l in output for w in wrapper.wrap(l)) | 105 output = '\r\n'.join(w for l in output for w in wrapper.wrap(l)) |
106 | 106 |
107 with open(manifest_path, 'w') as f: | 107 with open(manifest_path, 'w') as f: |
108 f.write(output) | 108 f.write(output) |
109 | 109 |
110 | 110 |
111 def _ExtractClassFiles(jar_path, dest_dir, java_files): | |
112 """Extracts all .class files not corresponding to |java_files|.""" | |
113 def extract_predicate(path): | |
114 if not path.endswith('.class'): | |
115 return False | |
116 path_without_suffix = re.sub(r'(?:\$|\.class)[^/]*$', '', path) | |
jbudorick
2015/09/23 00:26:20
path[:-len('.class')]
or something along those li
agrieve
2015/09/23 02:07:55
Hmm, yeah, sorely lacking a comment here.
Added:
| |
117 ret = not any(path_without_suffix in p for p in java_files) | |
118 return ret | |
jbudorick
2015/09/23 00:26:20
nit: just
return not any(path_without_suffix in
agrieve
2015/09/23 02:07:55
Done.
| |
119 | |
120 build_utils.ExtractAll(jar_path, path=dest_dir, predicate=extract_predicate) | |
121 | |
122 | |
123 def _OnStaleMd5(changes, options, javac_cmd, java_files, runtime_classpath): | |
124 with build_utils.TempDir() as temp_dir: | |
125 srcjars = options.java_srcjars | |
126 # The .excluded.jar contains .class files excluded from the main jar. | |
127 # It is used for incremental compiles. | |
128 excluded_jar_path = options.jar_path.replace('.jar', '.excluded.jar') | |
129 | |
130 classes_dir = os.path.join(temp_dir, 'classes') | |
131 os.makedirs(classes_dir) | |
132 | |
133 changed_paths = None | |
134 if changes.AddedOrModifiedOnly(): | |
135 changed_paths = set(changes.IterModifiedPaths()) | |
136 srcjars = [p for p in srcjars if p in changed_paths] | |
137 | |
138 if srcjars: | |
139 java_dir = os.path.join(temp_dir, 'java') | |
140 os.makedirs(java_dir) | |
141 for srcjar in options.java_srcjars: | |
142 build_utils.ExtractAll(srcjar, path=java_dir, pattern='*.java') | |
143 jar_srcs = build_utils.FindInDirectory(java_dir, '*.java') | |
144 java_files.extend(_FilterJavaFiles(jar_srcs, options.javac_includes)) | |
145 | |
146 if java_files: | |
147 if changed_paths: | |
148 # When no files have been added / removed and the output jar already | |
149 # exists, reuse .class files from the existing jar. | |
150 for path in srcjars: | |
151 changed_paths.update(changes.IterChangedSubpaths(path)) | |
152 | |
153 java_files = [p for p in java_files if p in changed_paths] | |
154 | |
155 _ExtractClassFiles(options.jar_path, classes_dir, java_files) | |
156 _ExtractClassFiles(excluded_jar_path, classes_dir, java_files) | |
157 # Add the extracted files to the classpath. | |
158 classpath_idx = javac_cmd.index('-classpath') | |
159 javac_cmd[classpath_idx + 1] += ':' + classes_dir | |
160 | |
161 # Don't include the output directory in the initial set of args since it | |
162 # being in a temp dir makes it unstable (breaks md5 stamping). | |
163 cmd = javac_cmd + ['-d', classes_dir] + java_files | |
164 | |
165 build_utils.CheckOutput( | |
166 cmd, | |
167 print_stdout=options.chromium_code, | |
168 stderr_filter=ColorJavacOutput) | |
169 | |
170 if options.main_class or options.manifest_entry: | |
171 entries = [] | |
172 if options.manifest_entry: | |
173 entries = [e.split(':') for e in options.manifest_entry] | |
174 manifest_file = os.path.join(temp_dir, 'manifest') | |
175 _CreateManifest(manifest_file, runtime_classpath, options.main_class, | |
176 entries) | |
177 else: | |
178 manifest_file = None | |
179 | |
180 glob = options.jar_excluded_classes | |
181 inclusion_predicate = lambda f: not build_utils.MatchesGlob(f, glob) | |
182 exclusion_predicate = lambda f: not inclusion_predicate(f) | |
183 | |
184 jar.JarDirectory(classes_dir, | |
185 options.jar_path, | |
186 manifest_file=manifest_file, | |
187 predicate=inclusion_predicate) | |
188 jar.JarDirectory(classes_dir, | |
189 excluded_jar_path, | |
190 predicate=exclusion_predicate) | |
191 | |
192 | |
111 def _ParseOptions(argv): | 193 def _ParseOptions(argv): |
112 parser = optparse.OptionParser() | 194 parser = optparse.OptionParser() |
113 build_utils.AddDepfileOption(parser) | 195 build_utils.AddDepfileOption(parser) |
114 | 196 |
115 parser.add_option( | 197 parser.add_option( |
116 '--src-gendirs', | 198 '--src-gendirs', |
117 help='Directories containing generated java files.') | 199 help='Directories containing generated java files.') |
118 parser.add_option( | 200 parser.add_option( |
119 '--java-srcjars', | 201 '--java-srcjars', |
120 action='append', | 202 action='append', |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
242 # Compute the list of paths that when changed, we need to rebuild. | 324 # Compute the list of paths that when changed, we need to rebuild. |
243 input_paths = options.bootclasspath + options.java_srcjars + java_files | 325 input_paths = options.bootclasspath + options.java_srcjars + java_files |
244 # TODO(agrieve): Remove this .TOC heuristic once GYP is no more. | 326 # TODO(agrieve): Remove this .TOC heuristic once GYP is no more. |
245 if not options.use_ijars: | 327 if not options.use_ijars: |
246 for path in compile_classpath: | 328 for path in compile_classpath: |
247 if os.path.exists(path + '.TOC'): | 329 if os.path.exists(path + '.TOC'): |
248 input_paths.append(path + '.TOC') | 330 input_paths.append(path + '.TOC') |
249 else: | 331 else: |
250 input_paths.append(path) | 332 input_paths.append(path) |
251 | 333 |
252 def on_stale_md5(): | 334 output_paths = [ |
253 with build_utils.TempDir() as temp_dir: | 335 options.jar_path, |
254 if options.java_srcjars: | 336 options.jar_path.replace('.jar', '.excluded.jar'), |
255 java_dir = os.path.join(temp_dir, 'java') | 337 ] |
256 os.makedirs(java_dir) | |
257 for srcjar in options.java_srcjars: | |
258 build_utils.ExtractAll(srcjar, path=java_dir, pattern='*.java') | |
259 jar_srcs = build_utils.FindInDirectory(java_dir, '*.java') | |
260 java_files.extend(_FilterJavaFiles(jar_srcs, options.javac_includes)) | |
261 | 338 |
262 classes_dir = os.path.join(temp_dir, 'classes') | 339 # An escape hatch to be able to check if incremental compiles are causing |
263 os.makedirs(classes_dir) | 340 # problems. |
264 | 341 force = int(os.environ.get('DISABLE_INCREMENTAL_JAVAC', 0)) |
265 if java_files: | |
266 # Don't include the output directory in the initial set of args since it | |
267 # being in a temp dir makes it unstable (breaks md5 stamping). | |
268 cmd = javac_cmd + ['-d', classes_dir] + java_files | |
269 | |
270 build_utils.CheckOutput( | |
271 cmd, | |
272 print_stdout=options.chromium_code, | |
273 stderr_filter=ColorJavacOutput) | |
274 | |
275 if options.main_class or options.manifest_entry: | |
276 entries = [] | |
277 if options.manifest_entry: | |
278 entries = [e.split(':') for e in options.manifest_entry] | |
279 manifest_file = os.path.join(temp_dir, 'manifest') | |
280 _CreateManifest(manifest_file, runtime_classpath, options.main_class, | |
281 entries) | |
282 else: | |
283 manifest_file = None | |
284 jar.JarDirectory(classes_dir, | |
285 options.jar_excluded_classes, | |
286 options.jar_path, | |
287 manifest_file=manifest_file) | |
288 | |
289 | 342 |
290 # List python deps in input_strings rather than input_paths since the contents | 343 # List python deps in input_strings rather than input_paths since the contents |
291 # of them does not change what gets written to the depsfile. | 344 # of them does not change what gets written to the depsfile. |
292 build_utils.CallAndRecordIfStale( | 345 build_utils.CallAndRecordIfStale( |
293 on_stale_md5, | 346 lambda changes: _OnStaleMd5(changes, options, javac_cmd, java_files, |
347 runtime_classpath), | |
294 options, | 348 options, |
295 input_paths=input_paths, | 349 input_paths=input_paths, |
296 input_strings=javac_cmd, | 350 input_strings=javac_cmd, |
297 output_paths=[options.jar_path]) | 351 output_paths=output_paths, |
352 force=force, | |
353 pass_changes=True) | |
298 | 354 |
299 | 355 |
300 if __name__ == '__main__': | 356 if __name__ == '__main__': |
301 sys.exit(main(sys.argv[1:])) | 357 sys.exit(main(sys.argv[1:])) |
OLD | NEW |