OLD | NEW |
(Empty) | |
| 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 '-public', |
| 42 '-protected', |
| 43 # -verbose is required to get constant values (which can be inlined in |
| 44 # dependents). |
| 45 '-verbose', |
| 46 '-classpath', classpath |
| 47 ] + classes |
| 48 return build_utils.CheckCallDie(javap_cmd, suppress_output=True) |
| 49 |
| 50 |
| 51 def ExtractToc(disassembled_classes): |
| 52 # javap output is structured by indent (2-space) levels. |
| 53 good_patterns = [ |
| 54 '^[^ ]', # This includes all class/function/member signatures. |
| 55 '^ SourceFile:', |
| 56 '^ minor version:', |
| 57 '^ major version:', |
| 58 '^ Constant value:', |
| 59 ] |
| 60 bad_patterns = [ |
| 61 '^const #', # Matches the constant pool (i.e. literals used in the class). |
| 62 ] |
| 63 |
| 64 def JavapFilter(line): |
| 65 return (re.match('|'.join(good_patterns), line) and |
| 66 not re.match('|'.join(bad_patterns), line)) |
| 67 toc = filter(JavapFilter, disassembled_classes.split('\n')) |
| 68 |
| 69 return '\n'.join(toc) |
| 70 |
| 71 |
| 72 def UpdateToc(jar_path, toc_path): |
| 73 classes = GetClassesInZipFile(zipfile.ZipFile(jar_path)) |
| 74 javap_output = CallJavap(classpath=jar_path, classes=classes) |
| 75 toc = ExtractToc(javap_output) |
| 76 |
| 77 with open(toc_path, 'w') as tocfile: |
| 78 tocfile.write(toc) |
| 79 |
| 80 |
| 81 def DoJarToc(options): |
| 82 jar_path = options.jar_path |
| 83 toc_path = options.toc_path |
| 84 md5_stamp_path = '%s.md5' % toc_path |
| 85 md5_checker = md5_check.Md5Checker(stamp=md5_stamp_path, inputs=[jar_path]) |
| 86 if md5_checker.IsStale(): |
| 87 UpdateToc(jar_path, toc_path) |
| 88 md5_checker.Write() |
| 89 else: |
| 90 build_utils.Touch(toc_path) |
| 91 |
| 92 |
| 93 def main(argv): |
| 94 parser = optparse.OptionParser() |
| 95 parser.add_option('--jar-path', help='Input .jar path.') |
| 96 parser.add_option('--toc-path', help='Output .jar.TOC path.') |
| 97 parser.add_option('--stamp', help='Path to touch on success.') |
| 98 |
| 99 # TODO(newt): remove this once http://crbug.com/177552 is fixed in ninja. |
| 100 parser.add_option('--ignore', help='Ignored.') |
| 101 |
| 102 options, _ = parser.parse_args() |
| 103 |
| 104 DoJarToc(options) |
| 105 |
| 106 if options.stamp: |
| 107 build_utils.Touch(options.stamp) |
| 108 |
| 109 |
| 110 if __name__ == '__main__': |
| 111 sys.exit(main(sys.argv)) |
| 112 |
OLD | NEW |