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 re
22import sys
23import zipfile
24
25from util import build_utils
26from util import md5_check
27
28
29def GetClassesInZipFile(zip_file):
30  classes = []
31  files = zip_file.namelist()
32  for f in files:
33    if f.endswith('.class'):
34      # f is of the form org/chromium/base/Class$Inner.class
35      classes.append(f.replace('/', '.')[:-6])
36  return classes
37
38
39def CallJavap(classpath, classes):
40  javap_cmd = [
41      'javap',
42      '-package',  # Show public/protected/package.
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.CheckOutput(javap_cmd)
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():
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  options, _ = parser.parse_args()
100
101  DoJarToc(options)
102
103  if options.stamp:
104    build_utils.Touch(options.stamp)
105
106
107if __name__ == '__main__':
108  sys.exit(main())
109