Chromium Code Reviews| 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 fnmatch | 7 import fnmatch |
| 8 import optparse | 8 import optparse |
| 9 import os | 9 import os |
| 10 import shutil | 10 import shutil |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 '-Xepdisable:' | 58 '-Xepdisable:' |
| 59 # Something in chrome_private_java makes this check crash. | 59 # Something in chrome_private_java makes this check crash. |
| 60 'com.google.errorprone.bugpatterns.ClassCanBeStatic,' | 60 'com.google.errorprone.bugpatterns.ClassCanBeStatic,' |
| 61 # These crash on lots of targets. | 61 # These crash on lots of targets. |
| 62 'com.google.errorprone.bugpatterns.WrongParameterPackage,' | 62 'com.google.errorprone.bugpatterns.WrongParameterPackage,' |
| 63 'com.google.errorprone.bugpatterns.GuiceOverridesGuiceInjectableMethod,' | 63 'com.google.errorprone.bugpatterns.GuiceOverridesGuiceInjectableMethod,' |
| 64 'com.google.errorprone.bugpatterns.GuiceOverridesJavaxInjectableMethod,' | 64 'com.google.errorprone.bugpatterns.GuiceOverridesJavaxInjectableMethod,' |
| 65 'com.google.errorprone.bugpatterns.ElementsCountedInLoop' | 65 'com.google.errorprone.bugpatterns.ElementsCountedInLoop' |
| 66 ] | 66 ] |
| 67 | 67 |
| 68 def DoJavac( | 68 def _FilterJavaFiles(paths, javac_includes): |
| 69 bootclasspath, classpath, classes_dir, chromium_code, | 69 if not javac_includes: |
| 70 use_errorprone_path, java_files): | 70 return paths |
| 71 """Runs javac. | 71 ret = [] |
|
jbudorick
2015/08/23 02:50:51
return [f for f in paths if any(fnmatch.fnmatch(f,
agrieve
2015/08/26 05:52:58
Done (and moved to build_utils.py since it was cop
| |
| 72 for f in paths: | |
| 73 for include in javac_includes: | |
| 74 if fnmatch.fnmatch(f, include): | |
| 75 ret.append(f) | |
| 76 break | |
| 77 return ret | |
| 72 | 78 |
| 73 Builds |java_files| with the provided |classpath| and puts the generated | 79 def _DoJavac(bootclasspath=None, classpath=None, java_files=None, |
| 74 .class files into |classes_dir|. If |chromium_code| is true, extra lint | 80 java_srcjars=None, javac_includes=None, jar_path=None, |
| 75 checking will be enabled. | 81 chromium_code=False, use_errorprone_path=None, main_class=None, |
| 82 manifest_entry=None, jar_excluded_classes=None): | |
| 83 """Compiles .java into a .jar. | |
| 84 | |
| 85 If |chromium_code| is true, extra lint checking will be enabled. | |
| 76 """ | 86 """ |
| 77 | 87 java_srcjars = java_srcjars or [] |
| 88 java_files = _FilterJavaFiles(java_files, javac_includes) | |
| 78 jar_inputs = [] | 89 jar_inputs = [] |
| 79 for path in classpath: | 90 for path in classpath: |
| 80 if os.path.exists(path + '.TOC'): | 91 if os.path.exists(path + '.TOC'): |
| 81 jar_inputs.append(path + '.TOC') | 92 jar_inputs.append(path + '.TOC') |
| 82 else: | 93 else: |
| 83 jar_inputs.append(path) | 94 jar_inputs.append(path) |
| 84 | 95 |
| 85 javac_args = [ | 96 javac_args = [ |
| 86 '-g', | 97 '-g', |
| 87 # Chromium only allows UTF8 source files. Being explicit avoids | 98 # Chromium only allows UTF8 source files. Being explicit avoids |
| 88 # javac pulling a default encoding from the user's environment. | 99 # javac pulling a default encoding from the user's environment. |
| 89 '-encoding', 'UTF-8', | 100 '-encoding', 'UTF-8', |
| 90 '-classpath', ':'.join(classpath), | 101 '-classpath', ':'.join(classpath), |
| 91 '-d', classes_dir] | 102 ] |
| 92 | 103 |
| 93 if bootclasspath: | 104 if bootclasspath: |
| 94 javac_args.extend([ | 105 javac_args.extend([ |
| 95 '-bootclasspath', ':'.join(bootclasspath), | 106 '-bootclasspath', ':'.join(bootclasspath), |
| 96 '-source', '1.7', | 107 '-source', '1.7', |
| 97 '-target', '1.7', | 108 '-target', '1.7', |
| 98 ]) | 109 ]) |
| 99 | 110 |
| 100 if chromium_code: | 111 if chromium_code: |
| 101 # TODO(aurimas): re-enable '-Xlint:deprecation' checks once they are fixed. | 112 # TODO(aurimas): re-enable '-Xlint:deprecation' checks once they are fixed. |
| 102 javac_args.extend(['-Xlint:unchecked']) | 113 javac_args.extend(['-Xlint:unchecked']) |
| 103 else: | 114 else: |
| 104 # XDignore.symbol.file makes javac compile against rt.jar instead of | 115 # XDignore.symbol.file makes javac compile against rt.jar instead of |
| 105 # ct.sym. This means that using a java internal package/class will not | 116 # ct.sym. This means that using a java internal package/class will not |
| 106 # trigger a compile warning or error. | 117 # trigger a compile warning or error. |
| 107 javac_args.extend(['-XDignore.symbol.file']) | 118 javac_args.extend(['-XDignore.symbol.file']) |
| 108 | 119 |
| 120 javac_cmd = ['javac'] | |
| 109 if use_errorprone_path: | 121 if use_errorprone_path: |
| 110 javac_cmd = [use_errorprone_path] + ERRORPRONE_OPTIONS | 122 javac_cmd = [use_errorprone_path] + ERRORPRONE_OPTIONS |
| 111 else: | |
| 112 javac_cmd = ['javac'] | |
| 113 | |
| 114 javac_cmd = javac_cmd + javac_args + java_files | |
| 115 | 123 |
| 116 def Compile(): | 124 def Compile(): |
| 117 build_utils.CheckOutput( | 125 with build_utils.TempDir() as temp_dir: |
| 118 javac_cmd, | 126 if java_srcjars: |
| 119 print_stdout=chromium_code, | 127 java_dir = os.path.join(temp_dir, 'java') |
| 120 stderr_filter=ColorJavacOutput) | 128 os.makedirs(java_dir) |
| 129 for srcjar in java_srcjars: | |
| 130 build_utils.ExtractAll(srcjar, path=java_dir, pattern='*.java') | |
| 131 jar_srcs = build_utils.FindInDirectory(java_dir, '*.java') | |
| 132 java_files.extend(_FilterJavaFiles(jar_srcs, javac_includes)) | |
| 121 | 133 |
| 122 record_path = os.path.join(classes_dir, 'javac.md5.stamp') | 134 classes_dir = os.path.join(temp_dir, 'classes') |
|
jbudorick
2015/08/23 02:50:50
This is the fix, right?
It's kinda hard to tell w
agrieve
2015/08/26 05:52:58
Yes, it's the crux of it. The script shouldn't be
| |
| 135 os.makedirs(classes_dir) | |
| 136 # Don't include the output directory in the initial set of args since it | |
| 137 # being in a temp dir makes it unstable (breaks md5 stamping). | |
| 138 cmd = javac_cmd + javac_args + ['-d', classes_dir] + java_files | |
| 139 | |
| 140 build_utils.CheckOutput( | |
| 141 cmd, | |
| 142 print_stdout=chromium_code, | |
| 143 stderr_filter=ColorJavacOutput) | |
| 144 | |
| 145 if main_class or manifest_entry: | |
| 146 if manifest_entry: | |
| 147 entries = map(lambda e: e.split(":"), manifest_entry) | |
|
jbudorick
2015/08/23 02:50:51
entries = [e.split(':') for e in manifest_entry]
agrieve
2015/08/26 05:52:58
Done.
| |
| 148 else: | |
| 149 entries = [] | |
| 150 manifest_file = os.path.join(temp_dir, 'manifest') | |
| 151 _CreateManifest(manifest_file, classpath, main_class, entries) | |
| 152 else: | |
| 153 manifest_file = None | |
| 154 jar.JarDirectory(classes_dir, | |
| 155 jar_excluded_classes, | |
| 156 jar_path, | |
| 157 manifest_file=manifest_file) | |
| 158 | |
| 159 record_path = jar_path + '.javac.md5.stamp' | |
| 123 md5_check.CallAndRecordIfStale( | 160 md5_check.CallAndRecordIfStale( |
| 124 Compile, | 161 Compile, |
| 125 record_path=record_path, | 162 record_path=record_path, |
| 126 input_paths=java_files + jar_inputs, | 163 input_paths=java_files + jar_inputs + java_srcjars, |
| 127 input_strings=javac_cmd) | 164 input_strings=javac_cmd + javac_args) |
| 128 | 165 |
| 129 | 166 |
| 130 _MAX_MANIFEST_LINE_LEN = 72 | 167 _MAX_MANIFEST_LINE_LEN = 72 |
| 131 | 168 |
| 132 | 169 |
| 133 def CreateManifest(manifest_path, classpath, main_class=None, | 170 def _CreateManifest(manifest_path, classpath, main_class=None, |
| 134 manifest_entries=None): | 171 manifest_entries=None): |
| 135 """Creates a manifest file with the given parameters. | 172 """Creates a manifest file with the given parameters. |
| 136 | 173 |
| 137 This generates a manifest file that compiles with the spec found at | 174 This generates a manifest file that compiles with the spec found at |
| 138 http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html#JAR_Manifes t | 175 http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html#JAR_Manifes t |
| 139 | 176 |
| 140 Args: | 177 Args: |
| 141 manifest_path: The path to the manifest file that should be created. | 178 manifest_path: The path to the manifest file that should be created. |
| 142 classpath: The JAR files that should be listed on the manifest file's | 179 classpath: The JAR files that should be listed on the manifest file's |
| 143 classpath. | 180 classpath. |
| 144 main_class: If present, the class containing the main() function. | 181 main_class: If present, the class containing the main() function. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 192 default=[], | 229 default=[], |
| 193 help='Boot classpath for javac. If this is specified multiple times, ' | 230 help='Boot classpath for javac. If this is specified multiple times, ' |
| 194 'they will all be appended to construct the classpath.') | 231 'they will all be appended to construct the classpath.') |
| 195 parser.add_option( | 232 parser.add_option( |
| 196 '--classpath', | 233 '--classpath', |
| 197 action='append', | 234 action='append', |
| 198 help='Classpath for javac. If this is specified multiple times, they ' | 235 help='Classpath for javac. If this is specified multiple times, they ' |
| 199 'will all be appended to construct the classpath.') | 236 'will all be appended to construct the classpath.') |
| 200 parser.add_option( | 237 parser.add_option( |
| 201 '--javac-includes', | 238 '--javac-includes', |
| 239 default='', | |
| 202 help='A list of file patterns. If provided, only java files that match' | 240 help='A list of file patterns. If provided, only java files that match' |
| 203 'one of the patterns will be compiled.') | 241 'one of the patterns will be compiled.') |
| 204 parser.add_option( | 242 parser.add_option( |
| 205 '--jar-excluded-classes', | 243 '--jar-excluded-classes', |
| 206 default='', | 244 default='', |
| 207 help='List of .class file patterns to exclude from the jar.') | 245 help='List of .class file patterns to exclude from the jar.') |
| 208 | 246 |
| 209 parser.add_option( | 247 parser.add_option( |
| 210 '--chromium-code', | 248 '--chromium-code', |
| 211 type='int', | 249 type='int', |
| 212 help='Whether code being compiled should be built with stricter ' | 250 help='Whether code being compiled should be built with stricter ' |
| 213 'warnings for chromium code.') | 251 'warnings for chromium code.') |
| 214 | 252 |
| 215 parser.add_option( | 253 parser.add_option( |
| 216 '--use-errorprone-path', | 254 '--use-errorprone-path', |
| 217 help='Use the Errorprone compiler at this path.') | 255 help='Use the Errorprone compiler at this path.') |
| 218 | 256 |
| 219 parser.add_option( | |
| 220 '--classes-dir', | |
| 221 help='Directory for compiled .class files.') | |
| 222 parser.add_option('--jar-path', help='Jar output path.') | 257 parser.add_option('--jar-path', help='Jar output path.') |
| 223 parser.add_option( | 258 parser.add_option( |
| 224 '--main-class', | 259 '--main-class', |
| 225 help='The class containing the main method.') | 260 help='The class containing the main method.') |
| 226 parser.add_option( | 261 parser.add_option( |
| 227 '--manifest-entry', | 262 '--manifest-entry', |
| 228 action='append', | 263 action='append', |
| 229 help='Key:value pairs to add to the .jar manifest.') | 264 help='Key:value pairs to add to the .jar manifest.') |
| 230 | 265 |
| 231 parser.add_option('--stamp', help='Path to touch on success.') | 266 parser.add_option('--stamp', help='Path to touch on success.') |
| 232 | 267 |
| 233 options, args = parser.parse_args(argv) | 268 options, args = parser.parse_args(argv) |
| 234 | 269 build_utils.CheckOptions(options, parser, required=('jar_path',)) |
| 235 if options.main_class and not options.jar_path: | |
| 236 parser.error('--main-class requires --jar-path') | |
| 237 | 270 |
| 238 bootclasspath = [] | 271 bootclasspath = [] |
| 239 for arg in options.bootclasspath: | 272 for arg in options.bootclasspath: |
| 240 bootclasspath += build_utils.ParseGypList(arg) | 273 bootclasspath += build_utils.ParseGypList(arg) |
| 241 | 274 |
| 242 classpath = [] | 275 classpath = [] |
| 243 for arg in options.classpath: | 276 for arg in options.classpath: |
| 244 classpath += build_utils.ParseGypList(arg) | 277 classpath += build_utils.ParseGypList(arg) |
| 245 | 278 |
| 246 java_srcjars = [] | 279 java_srcjars = [] |
| 247 for arg in options.java_srcjars: | 280 for arg in options.java_srcjars: |
| 248 java_srcjars += build_utils.ParseGypList(arg) | 281 java_srcjars += build_utils.ParseGypList(arg) |
| 249 | 282 |
| 250 java_files = args | 283 java_files = args |
| 251 if options.src_gendirs: | 284 if options.src_gendirs: |
| 252 src_gendirs = build_utils.ParseGypList(options.src_gendirs) | 285 src_gendirs = build_utils.ParseGypList(options.src_gendirs) |
| 253 java_files += build_utils.FindInDirectories(src_gendirs, '*.java') | 286 java_files += build_utils.FindInDirectories(src_gendirs, '*.java') |
| 254 | 287 |
| 255 input_files = bootclasspath + classpath + java_srcjars + java_files | 288 input_files = bootclasspath + classpath + java_srcjars + java_files |
| 256 with build_utils.TempDir() as temp_dir: | |
| 257 classes_dir = os.path.join(temp_dir, 'classes') | |
| 258 os.makedirs(classes_dir) | |
| 259 if java_srcjars: | |
| 260 java_dir = os.path.join(temp_dir, 'java') | |
| 261 os.makedirs(java_dir) | |
| 262 for srcjar in java_srcjars: | |
| 263 build_utils.ExtractAll(srcjar, path=java_dir, pattern='*.java') | |
| 264 java_files += build_utils.FindInDirectory(java_dir, '*.java') | |
| 265 | 289 |
| 266 if options.javac_includes: | 290 javac_includes = build_utils.ParseGypList(options.javac_includes) |
| 267 javac_includes = build_utils.ParseGypList(options.javac_includes) | 291 jar_excluded_classes = build_utils.ParseGypList(options.jar_excluded_classes) |
| 268 filtered_java_files = [] | |
| 269 for f in java_files: | |
| 270 for include in javac_includes: | |
| 271 if fnmatch.fnmatch(f, include): | |
| 272 filtered_java_files.append(f) | |
| 273 break | |
| 274 java_files = filtered_java_files | |
| 275 | 292 |
| 276 if len(java_files) != 0: | 293 _DoJavac(bootclasspath=bootclasspath, |
| 277 DoJavac( | 294 classpath=classpath, |
| 278 bootclasspath, | 295 java_files=java_files, |
| 279 classpath, | 296 java_srcjars=java_srcjars, |
| 280 classes_dir, | 297 javac_includes = javac_includes, |
| 281 options.chromium_code, | 298 jar_path=options.jar_path, |
| 282 options.use_errorprone_path, | 299 chromium_code=options.chromium_code, |
| 283 java_files) | 300 use_errorprone_path=options.use_errorprone_path, |
| 284 | 301 main_class=options.main_class, |
| 285 if options.jar_path: | 302 manifest_entry=options.manifest_entry, |
| 286 if options.main_class or options.manifest_entry: | 303 jar_excluded_classes=jar_excluded_classes) |
| 287 if options.manifest_entry: | |
| 288 entries = map(lambda e: e.split(":"), options.manifest_entry) | |
| 289 else: | |
| 290 entries = [] | |
| 291 manifest_file = os.path.join(temp_dir, 'manifest') | |
| 292 CreateManifest(manifest_file, classpath, options.main_class, entries) | |
| 293 else: | |
| 294 manifest_file = None | |
| 295 jar.JarDirectory(classes_dir, | |
| 296 build_utils.ParseGypList(options.jar_excluded_classes), | |
| 297 options.jar_path, | |
| 298 manifest_file=manifest_file) | |
| 299 | |
| 300 if options.classes_dir: | |
| 301 # Delete the old classes directory. This ensures that all .class files in | |
| 302 # the output are actually from the input .java files. For example, if a | |
| 303 # .java file is deleted or an inner class is removed, the classes | |
| 304 # directory should not contain the corresponding old .class file after | |
| 305 # running this action. | |
| 306 build_utils.DeleteDirectory(options.classes_dir) | |
| 307 shutil.copytree(classes_dir, options.classes_dir) | |
| 308 | 304 |
| 309 if options.depfile: | 305 if options.depfile: |
| 310 build_utils.WriteDepfile( | 306 build_utils.WriteDepfile( |
| 311 options.depfile, | 307 options.depfile, |
| 312 input_files + build_utils.GetPythonDependencies()) | 308 input_files + build_utils.GetPythonDependencies()) |
| 313 | 309 |
| 314 if options.stamp: | 310 if options.stamp: |
| 315 build_utils.Touch(options.stamp) | 311 build_utils.Touch(options.stamp) |
| 316 | 312 |
| 317 | 313 |
| 318 if __name__ == '__main__': | 314 if __name__ == '__main__': |
| 319 sys.exit(main(sys.argv[1:])) | 315 sys.exit(main(sys.argv[1:])) |
| 320 | 316 |
| 321 | 317 |
| OLD | NEW |