Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / build / android / pylib / utils / proguard.py
blob251cc4de46368436e8c8075d2774cdaebb0adc32
1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import os
6 import re
7 import tempfile
9 from devil.utils import cmd_helper
10 from pylib import constants
13 _PROGUARD_CLASS_RE = re.compile(r'\s*?- Program class:\s*([\S]+)$')
14 _PROGUARD_SUPERCLASS_RE = re.compile(r'\s*? Superclass:\s*([\S]+)$')
15 _PROGUARD_SECTION_RE = re.compile(
16 r'^(?:Interfaces|Constant Pool|Fields|Methods|Class file attributes) '
17 r'\(count = \d+\):$')
18 _PROGUARD_METHOD_RE = re.compile(r'\s*?- Method:\s*(\S*)[(].*$')
19 _PROGUARD_ANNOTATION_RE = re.compile(r'\s*?- Annotation \[L(\S*);\]:$')
20 _PROGUARD_ANNOTATION_CONST_RE = (
21 re.compile(r'\s*?- Constant element value.*$'))
22 _PROGUARD_ANNOTATION_VALUE_RE = re.compile(r'\s*?- \S+? \[(.*)\]$')
24 _PROGUARD_PATH_SDK = os.path.join(
25 constants.PROGUARD_ROOT, 'lib', 'proguard.jar')
26 _PROGUARD_PATH_BUILT = (
27 os.path.join(os.environ['ANDROID_BUILD_TOP'], 'external', 'proguard',
28 'lib', 'proguard.jar')
29 if 'ANDROID_BUILD_TOP' in os.environ else None)
30 _PROGUARD_PATH = (
31 _PROGUARD_PATH_SDK if os.path.exists(_PROGUARD_PATH_SDK)
32 else _PROGUARD_PATH_BUILT)
35 def Dump(jar_path):
36 """Dumps class and method information from a JAR into a dict via proguard.
38 Args:
39 jar_path: An absolute path to the JAR file to dump.
40 Returns:
41 A dict in the following format:
43 'classes': [
45 'class': '',
46 'superclass': '',
47 'annotations': {},
48 'methods': [
50 'method': '',
51 'annotations': {},
53 ...
56 ...
59 """
61 with tempfile.NamedTemporaryFile() as proguard_output:
62 cmd_helper.RunCmd(['java', '-jar',
63 _PROGUARD_PATH,
64 '-injars', jar_path,
65 '-dontshrink',
66 '-dontoptimize',
67 '-dontobfuscate',
68 '-dontpreverify',
69 '-dump', proguard_output.name])
70 return Parse(proguard_output)
72 def Parse(proguard_output):
73 results = {
74 'classes': [],
77 annotation = None
78 annotation_has_value = False
79 class_result = None
80 method_result = None
82 for line in proguard_output:
83 line = line.strip('\r\n')
85 m = _PROGUARD_CLASS_RE.match(line)
86 if m:
87 class_result = {
88 'class': m.group(1).replace('/', '.'),
89 'superclass': '',
90 'annotations': {},
91 'methods': [],
93 results['classes'].append(class_result)
94 annotation = None
95 annotation_has_value = False
96 method_result = None
97 continue
99 if not class_result:
100 continue
102 m = _PROGUARD_SUPERCLASS_RE.match(line)
103 if m:
104 class_result['superclass'] = m.group(1).replace('/', '.')
105 continue
107 m = _PROGUARD_SECTION_RE.match(line)
108 if m:
109 annotation = None
110 annotation_has_value = False
111 method_result = None
112 continue
114 m = _PROGUARD_METHOD_RE.match(line)
115 if m:
116 method_result = {
117 'method': m.group(1),
118 'annotations': {},
120 class_result['methods'].append(method_result)
121 annotation = None
122 annotation_has_value = False
123 continue
125 m = _PROGUARD_ANNOTATION_RE.match(line)
126 if m:
127 # Ignore the annotation package.
128 annotation = m.group(1).split('/')[-1]
129 if method_result:
130 method_result['annotations'][annotation] = None
131 else:
132 class_result['annotations'][annotation] = None
133 continue
135 if annotation:
136 if not annotation_has_value:
137 m = _PROGUARD_ANNOTATION_CONST_RE.match(line)
138 annotation_has_value = bool(m)
139 else:
140 m = _PROGUARD_ANNOTATION_VALUE_RE.match(line)
141 if m:
142 if method_result:
143 method_result['annotations'][annotation] = m.group(1)
144 else:
145 class_result['annotations'][annotation] = m.group(1)
146 annotation_has_value = None
147 return results