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 """Creates a TOC file from a Java jar.
      8 
      9 The TOC file contains the non-package API of the jar. This includes all
     10 public/protected classes/functions/members and the values of static final
     11 variables. Some other information (major/minor javac version) is also included.
     12 
     13 This TOC file then can be used to determine if a dependent library should be
     14 rebuilt when this jar changes. I.e. any change to the jar that would require a
     15 rebuild, will have a corresponding change in the TOC file.
     16 """
     17 
     18 import optparse
     19 import os
     20 import re
     21 import sys
     22 import zipfile
     23 
     24 from util import build_utils
     25 from util import md5_check
     26 
     27 
     28 def GetClassesInZipFile(zip_file):
     29   classes = []
     30   files = zip_file.namelist()
     31   for f in files:
     32     if f.endswith('.class'):
     33       # f is of the form org/chromium/base/Class$Inner.class
     34       classes.append(f.replace('/', '.')[:-6])
     35   return classes
     36 
     37 
     38 def CallJavap(classpath, classes):
     39   javap_cmd = [
     40       'javap',
     41       '-protected',  # In reality both public & protected.
     42       # -verbose is required to get constant values (which can be inlined in
     43       # dependents).
     44       '-verbose',
     45       '-classpath', classpath
     46       ] + classes
     47   return build_utils.CheckCallDie(javap_cmd, suppress_output=True)
     48 
     49 
     50 def ExtractToc(disassembled_classes):
     51   # javap output is structured by indent (2-space) levels.
     52   good_patterns = [
     53       '^[^ ]', # This includes all class/function/member signatures.
     54       '^  SourceFile:',
     55       '^  minor version:',
     56       '^  major version:',
     57       '^  Constant value:',
     58       ]
     59   bad_patterns = [
     60       '^const #', # Matches the constant pool (i.e. literals used in the class).
     61     ]
     62 
     63   def JavapFilter(line):
     64     return (re.match('|'.join(good_patterns), line) and
     65         not re.match('|'.join(bad_patterns), line))
     66   toc = filter(JavapFilter, disassembled_classes.split('\n'))
     67 
     68   return '\n'.join(toc)
     69 
     70 
     71 def UpdateToc(jar_path, toc_path):
     72   classes = GetClassesInZipFile(zipfile.ZipFile(jar_path))
     73   javap_output = CallJavap(classpath=jar_path, classes=classes)
     74   toc = ExtractToc(javap_output)
     75 
     76   with open(toc_path, 'w') as tocfile:
     77     tocfile.write(toc)
     78 
     79 
     80 def DoJarToc(options):
     81   jar_path = options.jar_path
     82   toc_path = options.toc_path
     83   record_path = '%s.md5.stamp' % toc_path
     84   md5_check.CallAndRecordIfStale(
     85       lambda: UpdateToc(jar_path, toc_path),
     86       record_path=record_path,
     87       input_paths=[jar_path],
     88       )
     89   build_utils.Touch(toc_path)
     90 
     91 
     92 def main(argv):
     93   parser = optparse.OptionParser()
     94   parser.add_option('--jar-path', help='Input .jar path.')
     95   parser.add_option('--toc-path', help='Output .jar.TOC path.')
     96   parser.add_option('--stamp', help='Path to touch on success.')
     97 
     98   # TODO(newt): remove this once http://crbug.com/177552 is fixed in ninja.
     99   parser.add_option('--ignore', help='Ignored.')
    100 
    101   options, _ = parser.parse_args()
    102 
    103   DoJarToc(options)
    104 
    105   if options.stamp:
    106     build_utils.Touch(options.stamp)
    107 
    108 
    109 if __name__ == '__main__':
    110   sys.exit(main(sys.argv))
    111