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