blob: 019af6a04184b061a93a42a0ea1651664964c16d [file] [log] [blame]
Po-Chien Hsuehf86af512017-01-20 17:02:57 +08001#!/usr/bin/env python3
2
3import argparse
4import fnmatch
5import json
6import os
7import re
8import sys
9
10HELP_MSG = '''
11This script analyses the usage of build-time variables which are defined in BoardConfig*.mk
12and used by framework modules (installed in system.img). Please 'lunch' and 'make' before
13running it.
14'''
15
16TOP = os.environ.get('ANDROID_BUILD_TOP')
17OUT = os.environ.get('OUT')
18
19white_list = [
20'TARGET_ARCH',
21'TARGET_ARCH_VARIANT',
22'TARGET_CPU_VARIANT',
23'TARGET_CPU_ABI',
24'TARGET_CPU_ABI2',
25
26'TARGET_2ND_ARCH',
27'TARGET_2ND_ARCH_VARIANT',
28'TARGET_2ND_CPU_VARIANT',
29'TARGET_2ND_CPU_ABI',
30'TARGET_2ND_CPU_ABI2',
31
32'TARGET_NO_BOOTLOADER',
33'TARGET_NO_KERNEL',
34'TARGET_NO_RADIOIMAGE',
35'TARGET_NO_RECOVERY',
36
37'TARGET_BOARD_PLATFORM',
38
39'ARCH_ARM_HAVE_ARMV7A',
40'ARCH_ARM_HAVE_NEON',
41'ARCH_ARM_HAVE_VFP',
42'ARCH_ARM_HAVE_VFP_D32',
43
44'BUILD_NUMBER'
45]
46
47
48# used by find_board_configs_mks() and find_makefiles()
49def find_files(folders, filter):
50ret = []
51
52for folder in folders:
53for root, dirs, files in os.walk(os.path.join(TOP, folder), topdown=True):
54dirs[:] = [d for d in dirs if not d[0] == '.']
55for file in files:
56if filter(file):
57ret.append(os.path.join(root, file))
58
59return ret
60
61# find board configs (BoardConfig*.mk)
62def find_board_config_mks(folders = ['build', 'device', 'vendor', 'hardware']):
63return find_files(folders, lambda x:
64fnmatch.fnmatch(x, 'BoardConfig*.mk'))
65
66# find makefiles (*.mk or Makefile) under specific folders
67def find_makefiles(folders = ['system', 'frameworks', 'external']):
68return find_files(folders, lambda x:
69fnmatch.fnmatch(x, '*.mk') or fnmatch.fnmatch(x, 'Makefile'))
70
71# read module-info.json and find makefiles of modules in system image
72def find_system_module_makefiles():
73makefiles = []
74out_system_path = os.path.join(OUT[len(TOP) + 1:], 'system')
75
76with open(os.path.join(OUT, 'module-info.json')) as module_info_json:
77module_info = json.load(module_info_json)
78for module in module_info:
79installs = module_info[module]['installed']
80paths = module_info[module]['path']
81
82installed_in_system = False
83
84for install in installs:
85if install.startswith(out_system_path):
86installed_in_system = True
87break
88
89if installed_in_system:
90for path in paths:
91makefile = os.path.join(TOP, path, 'Android.mk')
92makefiles.append(makefile)
93
94return makefiles
95
96# find variables defined in board_config_mks
97def find_defined_variables(board_config_mks):
98re_def = re.compile('^[\s]*([\w\d_]*)[\s]*:=')
99variables = dict()
100
101for board_config_mk in board_config_mks:
102for line in open(board_config_mk, encoding='latin1'):
103mo = re_def.search(line)
104if mo is None:
105continue
106
107variable = mo.group(1)
108if variable in white_list:
109continue
110
111if variable not in variables:
112variables[variable] = set()
113
114variables[variable].add(board_config_mk[len(TOP) + 1:])
115
116return variables
117
118# count variable usage in makefiles
119def find_usage(variable, makefiles):
120re_usage = re.compile('\$\(' + variable + '\)')
121usage = set()
122
123for makefile in makefiles:
124if not os.path.isfile(makefile):
125# TODO: support bp
126continue
127
128with open(makefile, encoding='latin1') as mk_file:
129mk_str = mk_file.read()
130
131if re_usage.search(mk_str) is not None:
132usage.add(makefile[len(TOP) + 1:])
133
134return usage
135
136def main():
137parser = argparse.ArgumentParser(description=HELP_MSG)
138parser.add_argument("-v", "--verbose",
139help="print definition and usage locations",
140action="store_true")
141args = parser.parse_args()
142
143print('TOP : ' + TOP)
144print('OUT : ' + OUT)
145print()
146
147sfe_makefiles = find_makefiles()
148system_module_makefiles = find_system_module_makefiles()
149board_config_mks = find_board_config_mks()
150variables = find_defined_variables(board_config_mks)
151
152if args.verbose:
153print('sfe_makefiles', len(sfe_makefiles))
154print('system_module_makefiles', len(system_module_makefiles))
155print('board_config_mks', len(board_config_mks))
156print('variables', len(variables))
157print()
158
159glossary = (
160'*Output in CSV format\n\n'
161
162'*definition count :'
163' This variable is defined in how many BoardConfig*.mk\'s\n'
164
165'*usage in SFE :'
166' This variable is used by how many makefiles under system/, frameworks/ and external/ folders\n'
167
168'*usage in system image :'
169' This variable is used by how many system image modules\n')
170
171csv_string = (
172'variable name,definition count,usage in SFE,usage in system image\n')
173
174for variable, locations in sorted(variables.items()):
175usage_in_sfe = find_usage(variable, sfe_makefiles)
176usage_of_system_modules = find_usage(variable, system_module_makefiles)
177usage = usage_in_sfe | usage_of_system_modules
178
179if len(usage) == 0:
180continue
181
182csv_string += ','.join([variable,
183str(len(locations)),
184str(len(usage_in_sfe)),
185str(len(usage_of_system_modules))]) + '\n'
186
187if args.verbose:
188print((variable + ' ').ljust(80, '='))
189
190print('Defined in (' + str(len(locations)) + ') :')
191for location in sorted(locations):
192print(' ' + location)
193
194print('Used in (' + str(len(usage)) + ') :')
195for location in sorted(usage):
196print(' ' + location)
197
198print()
199
200if args.verbose:
201print('\n')
202
203print(glossary)
204print(csv_string)
205
206if __name__ == '__main__':
207if TOP is None:
208sys.exit('$ANDROID_BUILD_TOP is undefined, please lunch and make before running this script')
209
210if OUT is None:
211sys.exit('$OUT is undefined, please lunch and make before running this script')
212
213if not os.path.isfile(os.path.join(OUT, 'module-info.json')):
214sys.exit('module-info.json is missing, please lunch and make before running this script')
215
216main()
217