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