jar_toc.py revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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 classes/functions/members and the values of static final 11variables. Some other information (major/minor javac version) is also included. 12 13This TOC file then can be used to determine if a dependent library should be 14rebuilt when this jar changes. I.e. any change to the jar that would require a 15rebuild, will have a corresponding change in the TOC file. 16""" 17 18import optparse 19import os 20import re 21import sys 22import zipfile 23 24from util import build_utils 25from util import md5_check 26 27 28def 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 38def 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 51def 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 72def 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 81def DoJarToc(options): 82 jar_path = options.jar_path 83 toc_path = options.toc_path 84 record_path = '%s.md5.stamp' % toc_path 85 md5_check.CallAndRecordIfStale( 86 lambda: UpdateToc(jar_path, toc_path), 87 record_path=record_path, 88 input_paths=[jar_path], 89 ) 90 build_utils.Touch(toc_path) 91 92 93def 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 110if __name__ == '__main__': 111 sys.exit(main(sys.argv)) 112 113