1#!/usr/bin/env python3
2#
3# Copyright (C) 2020 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17"""Installs vendor snapshot under prebuilts/vendor/v{version}."""
18
19import argparse
20import glob
21import logging
22import os
23import re
24import shutil
25import subprocess
26import sys
27import tempfile
28import textwrap
29import json
30
31INDENT = ' ' * 4
32
33def get_notice_path(module_name):
34    return os.path.join('NOTICE_FILES', module_name+'.txt')
35
36def get_target_arch(json_rel_path):
37    return json_rel_path.split('/')[0]
38
39def get_arch(json_rel_path):
40    return json_rel_path.split('/')[1].split('-')[1]
41
42def get_variation(json_rel_path):
43    return json_rel_path.split('/')[2]
44
45# convert .bp prop dictionary to .bp prop string
46def gen_bp_prop(prop, ind):
47    bp = ''
48    for key in prop:
49        val = prop[key]
50
51        # Skip empty list or dict, rather than printing empty prop like
52        # "key: []," or "key: {},"
53        if type(val) == list or type(val) == dict:
54            if len(val) == 0:
55                continue
56
57        bp += ind + key + ": "
58        if type(val) == bool:
59            bp += "true,\n" if val else "false,\n"
60        elif type(val) == str:
61            bp += '"%s",\n' % val
62        elif type(val) == list:
63            bp += '[\n'
64            for elem in val:
65                bp += ind + INDENT + '"%s",\n' % elem
66            bp += ind + '],\n'
67        elif type(val) == dict:
68            bp += '{\n'
69            bp += gen_bp_prop(val, ind + INDENT)
70            bp += ind + '},\n'
71        else:
72            raise TypeError('unsupported type %s for gen_bp_prop' % type(val))
73    return bp
74
75# Remove non-existent dirs from given list. Emits warning for such dirs.
76def remove_invalid_dirs(paths, bp_dir, module_name):
77    ret = []
78    for path in paths:
79        if os.path.isdir(os.path.join(bp_dir, path)):
80            ret.append(path)
81        else:
82            logging.warning(
83                'Dir "%s" of module "%s" does not exist' % (path, module_name))
84    return ret
85
86JSON_TO_BP = {
87    'ModuleName':          'name',
88    'RelativeInstallPath': 'relative_install_path',
89    'ExportedDirs':        'export_include_dirs',
90    'ExportedSystemDirs':  'export_system_include_dirs',
91    'ExportedFlags':       'export_flags',
92    'SanitizeMinimalDep':  'sanitize_minimal_dep',
93    'SanitizeUbsanDep':    'sanitize_ubsan_dep',
94    'Symlinks':            'symlinks',
95    'InitRc':              'init_rc',
96    'VintfFragments':      'vintf_fragments',
97    'SharedLibs':          'shared_libs',
98    'RuntimeLibs':         'runtime_libs',
99    'Required':            'required',
100}
101
102# Converts parsed json dictionary (which is intermediate) to Android.bp prop
103# dictionary. This validates paths such as include directories and init_rc
104# files while converting.
105def convert_json_to_bp_prop(json_path, bp_dir):
106    prop = json.load(json_path)
107    ret = {}
108
109    module_name = prop['ModuleName']
110    ret['name'] = module_name
111
112    # Soong will complain about non-existing paths on Android.bp. There might
113    # be missing files among generated header files, so check all exported
114    # directories and filter out invalid ones. Emits warning for such dirs.
115    # TODO: fix soong to track all generated headers correctly
116    for key in {'ExportedDirs', 'ExportedSystemDirs'}:
117        if key in prop:
118            prop[key] = remove_invalid_dirs(prop[key], bp_dir, module_name)
119
120    for key in prop:
121        if key in JSON_TO_BP:
122            ret[JSON_TO_BP[key]] = prop[key]
123        else:
124            logging.warning(
125                'Unknown prop "%s" of module "%s"' % (key, module_name))
126
127    return ret
128
129def gen_bp_module(variation, name, version, target_arch, arch_props, bp_dir):
130    prop = {
131        # These three are common for all snapshot modules.
132        'version': str(version),
133        'target_arch': target_arch,
134        'vendor': True,
135        'arch': {},
136    }
137
138    # Factor out common prop among architectures to minimize Android.bp.
139    common_prop = None
140    for arch in arch_props:
141        if common_prop is None:
142            common_prop = dict()
143            for k in arch_props[arch]:
144                common_prop[k] = arch_props[arch][k]
145            continue
146        for k in list(common_prop.keys()):
147            if not k in arch_props[arch] or common_prop[k] != arch_props[arch][k]:
148                del common_prop[k]
149
150    # Forcing src to be arch_props prevents 32-bit only modules to be used as
151    # 64-bit modules, and vice versa.
152    if 'src' in common_prop:
153        del common_prop['src']
154    prop.update(common_prop)
155
156    for arch in arch_props:
157        for k in common_prop:
158            if k in arch_props[arch]:
159                del arch_props[arch][k]
160        prop['arch'][arch] = arch_props[arch]
161
162    bp = 'vendor_snapshot_%s {\n' % variation
163    bp += gen_bp_prop(prop, INDENT)
164    bp += '}\n\n'
165    return bp
166
167def get_args():
168    parser = argparse.ArgumentParser()
169    parser.add_argument(
170        'snapshot_version',
171        type=int,
172        help='Vendor snapshot version to install, e.g. "30".')
173    parser.add_argument(
174        '-v',
175        '--verbose',
176        action='count',
177        default=0,
178        help='Increase output verbosity, e.g. "-v", "-vv".')
179    return parser.parse_args()
180
181def main():
182    """Program entry point."""
183    args = get_args()
184    verbose_map = (logging.WARNING, logging.INFO, logging.DEBUG)
185    verbosity = min(args.verbose, 2)
186    logging.basicConfig(
187        format='%(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
188        level=verbose_map[verbosity])
189    install_dir = os.path.join('prebuilts', 'vendor', 'v'+str(args.snapshot_version))
190
191    # props[target_arch]["static"|"shared"|"binary"|"header"][name][arch] : json
192    props = dict()
193
194    # {target_arch}/{arch}/{variation}/{module}.json
195    for root, _, files in os.walk(install_dir):
196        for file_name in sorted(files):
197            if not file_name.endswith('.json'):
198                continue
199            full_path = os.path.join(root, file_name)
200            rel_path = os.path.relpath(full_path, install_dir)
201
202            target_arch = get_target_arch(rel_path)
203            arch = get_arch(rel_path)
204            variation = get_variation(rel_path)
205            bp_dir = os.path.join(install_dir, target_arch)
206
207            if not target_arch in props:
208                props[target_arch] = dict()
209            if not variation in props[target_arch]:
210                props[target_arch][variation] = dict()
211
212            with open(full_path, 'r') as f:
213                prop = convert_json_to_bp_prop(f, bp_dir)
214                # Remove .json after parsing?
215                # os.unlink(full_path)
216
217            if variation != 'header':
218                prop['src'] = os.path.relpath(
219                    rel_path[:-5], # removing .json
220                    target_arch)
221
222            module_name = prop['name']
223            notice_path = 'NOTICE_FILES/' + module_name + ".txt"
224            if os.path.exists(os.path.join(bp_dir, notice_path)):
225                prop['notice'] = notice_path
226
227            variation_dict = props[target_arch][variation]
228            if not module_name in variation_dict:
229                variation_dict[module_name] = dict()
230            variation_dict[module_name][arch] = prop
231
232    for target_arch in props:
233        androidbp = ''
234        bp_dir = os.path.join(install_dir, target_arch)
235        for variation in props[target_arch]:
236            for name in props[target_arch][variation]:
237                androidbp += gen_bp_module(
238                    variation,
239                    name,
240                    args.snapshot_version,
241                    target_arch,
242                    props[target_arch][variation][name],
243                    bp_dir)
244        with open(os.path.join(bp_dir, 'Android.bp'), 'w') as f:
245            f.write(androidbp)
246
247if __name__ == '__main__':
248    main()
249