Home | History | Annotate | Download | only in gyp
      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     suppress_output = not options.chromium_code
     63     build_utils.CheckCallDie(javac_cmd, suppress_output=suppress_output)
     64 
     65   record_path = '%s/javac.md5.stamp' % options.output_dir
     66   md5_check.CallAndRecordIfStale(
     67       Compile,
     68       record_path=record_path,
     69       input_paths=java_files + jar_inputs,
     70       input_strings=javac_cmd)
     71 
     72 
     73 def main(argv):
     74   parser = optparse.OptionParser()
     75   parser.add_option('--src-dirs', help='Directories containing java files.')
     76   parser.add_option('--javac-includes',
     77       help='A list of file patterns. If provided, only java files that match' +
     78         'one of the patterns will be compiled.')
     79   parser.add_option('--classpath', help='Classpath for javac.')
     80   parser.add_option('--output-dir', help='Directory for javac output.')
     81   parser.add_option('--stamp', help='Path to touch on success.')
     82   parser.add_option('--chromium-code', type='int', help='Whether code being '
     83                     'compiled should be built with stricter warnings for '
     84                     'chromium code.')
     85 
     86   # TODO(newt): remove this once http://crbug.com/177552 is fixed in ninja.
     87   parser.add_option('--ignore', help='Ignored.')
     88 
     89   options, _ = parser.parse_args()
     90 
     91   DoJavac(options)
     92 
     93   if options.stamp:
     94     build_utils.Touch(options.stamp)
     95 
     96 
     97 if __name__ == '__main__':
     98   sys.exit(main(sys.argv))
     99 
    100 
    101