1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2021 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import sys 17import os 18import argparse 19import ninja_rules_parser 20 21sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 22from scripts.util.file_utils import read_json_file # noqa E402 23 24 25def _read_parts_variants(build_configs_dir: str) -> str: 26 parts_variants_file = os.path.join(build_configs_dir, 'parts_info', 27 'parts_variants.json') 28 if not os.path.exists(parts_variants_file): 29 raise Exception("file '{}' doesn't exist.".format(parts_variants_file)) 30 parts_variants = read_json_file(parts_variants_file) 31 if parts_variants is None: 32 raise Exception("read file '{}' failed.".format(parts_variants_file)) 33 return parts_variants 34 35 36def get_build_target(build_dir: str, build_targets: list, target_platform: str) -> list: 37 if not target_platform or target_platform == 'all': 38 target_platform = 'phone' 39 real_targets = [] 40 build_configs_dir = os.path.join(build_dir, 'build_configs') 41 parts_variants = _read_parts_variants(build_configs_dir) 42 for _target_name in build_targets: 43 if _target_name in parts_variants: 44 if target_platform == 'phone': 45 real_targets.append(_target_name) 46 else: 47 variants = parts_variants.get(_target_name) 48 if target_platform in variants: 49 real_targets.append('{0}/{1}_{0}'.format( 50 target_platform, _target_name)) 51 else: 52 real_targets.append(_target_name) 53 else: 54 if target_platform == 'phone': 55 real_targets.append(_target_name) 56 else: 57 real_targets.append('{0}/{1}'.format(target_platform, 58 _target_name)) 59 return real_targets 60 61 62def main() -> int: 63 parser = argparse.ArgumentParser() 64 parser.add_argument('--source-root-dir', required=True) 65 parser.add_argument('--root-build-dir', required=True) 66 parser.add_argument('--build-target-name', nargs='*') 67 parser.add_argument('--target-platform', required=False) 68 args = parser.parse_args() 69 70 source_root_dir = args.source_root_dir 71 if not os.path.exists(os.path.join(source_root_dir, '.gn')): 72 print('source root dir incorrect.') 73 return 1 74 75 build_dir = os.path.join(source_root_dir, args.root_build_dir) 76 # rewrite build.log 77 ninja_rules_parser.update( 78 build_dir, 'build_configs/platforms_info/toolchain_to_variant.json') 79 80 if args.target_platform is None or args.target_platform == 'all': 81 target_platform = None 82 else: 83 target_platform = args.target_platform 84 # process build targets 85 build_targets = get_build_target(build_dir, args.build_target_name, 86 target_platform) 87 # output build_targets string, be used on the ninja command line 88 print(' '.join(build_targets)) 89 return 0 90 91 92if __name__ == '__main__': 93 sys.exit(main()) 94