1 #!/usr/bin/env python 2 # 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 5 # found in the LICENSE file. 6 7 import fnmatch 8 import optparse 9 import os 10 import sys 11 12 from util import build_utils 13 from util import md5_check 14 15 16 def DoJavac(options): 17 output_dir = options.output_dir 18 19 src_dirs = build_utils.ParseGypList(options.src_dirs) 20 java_files = build_utils.FindInDirectories(src_dirs, '*.java') 21 if options.javac_includes: 22 javac_includes = build_utils.ParseGypList(options.javac_includes) 23 filtered_java_files = [] 24 for f in java_files: 25 for include in javac_includes: 26 if fnmatch.fnmatch(f, include): 27 filtered_java_files.append(f) 28 break 29 java_files = filtered_java_files 30 31 # Compiling guava with certain orderings of input files causes a compiler 32 # crash... Sorted order works, so use that. 33 # See https://code.google.com/p/guava-libraries/issues/detail?id=950 34 java_files.sort() 35 classpath = build_utils.ParseGypList(options.classpath) 36 37 jar_inputs = [] 38 for path in classpath: 39 if os.path.exists(path + '.TOC'): 40 jar_inputs.append(path + '.TOC') 41 else: 42 jar_inputs.append(path) 43 44 javac_cmd = [ 45 'javac', 46 '-g', 47 '-source', '1.5', 48 '-target', '1.5', 49 '-classpath', ':'.join(classpath), 50 '-d', output_dir, 51 '-Xlint:unchecked', 52 '-Xlint:deprecation', 53 ] + java_files 54 55 def Compile(): 56 # Delete the classes directory. This ensures that all .class files in the 57 # output are actually from the input .java files. For example, if a .java 58 # file is deleted or an inner class is removed, the classes directory should 59 # not contain the corresponding old .class file after running this action. 60 build_utils.DeleteDirectory(output_dir) 61 build_utils.MakeDirectory(output_dir) 62 build_utils.CheckOutput(javac_cmd, print_stdout=options.chromium_code) 63 64 record_path = '%s/javac.md5.stamp' % options.output_dir 65 md5_check.CallAndRecordIfStale( 66 Compile, 67 record_path=record_path, 68 input_paths=java_files + jar_inputs, 69 input_strings=javac_cmd) 70 71 72 def main(argv): 73 parser = optparse.OptionParser() 74 parser.add_option('--src-dirs', help='Directories containing java files.') 75 parser.add_option('--javac-includes', 76 help='A list of file patterns. If provided, only java files that match' + 77 'one of the patterns will be compiled.') 78 parser.add_option('--classpath', help='Classpath for javac.') 79 parser.add_option('--output-dir', help='Directory for javac output.') 80 parser.add_option('--stamp', help='Path to touch on success.') 81 parser.add_option('--chromium-code', type='int', help='Whether code being ' 82 'compiled should be built with stricter warnings for ' 83 'chromium code.') 84 85 # TODO(newt): remove this once http://crbug.com/177552 is fixed in ninja. 86 parser.add_option('--ignore', help='Ignored.') 87 88 options, _ = parser.parse_args() 89 90 DoJavac(options) 91 92 if options.stamp: 93 build_utils.Touch(options.stamp) 94 95 96 if __name__ == '__main__': 97 sys.exit(main(sys.argv)) 98 99 100