15f9996aaSopenharmony_ci#!/usr/bin/env python
25f9996aaSopenharmony_ci# -*- coding: utf-8 -*-
35f9996aaSopenharmony_ci# Copyright (c) 2021 Huawei Device Co., Ltd.
45f9996aaSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
55f9996aaSopenharmony_ci# you may not use this file except in compliance with the License.
65f9996aaSopenharmony_ci# You may obtain a copy of the License at
75f9996aaSopenharmony_ci#
85f9996aaSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
95f9996aaSopenharmony_ci#
105f9996aaSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
115f9996aaSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
125f9996aaSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135f9996aaSopenharmony_ci# See the License for the specific language governing permissions and
145f9996aaSopenharmony_ci# limitations under the License.
155f9996aaSopenharmony_ci
165f9996aaSopenharmony_ciimport sys
175f9996aaSopenharmony_ciimport os
185f9996aaSopenharmony_ciimport argparse
195f9996aaSopenharmony_ciimport ninja_rules_parser
205f9996aaSopenharmony_ci
215f9996aaSopenharmony_cisys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
225f9996aaSopenharmony_cifrom scripts.util.file_utils import read_json_file  # noqa E402
235f9996aaSopenharmony_ci
245f9996aaSopenharmony_ci
255f9996aaSopenharmony_cidef _read_parts_variants(build_configs_dir: str) -> str:
265f9996aaSopenharmony_ci    parts_variants_file = os.path.join(build_configs_dir, 'parts_info',
275f9996aaSopenharmony_ci                                       'parts_variants.json')
285f9996aaSopenharmony_ci    if not os.path.exists(parts_variants_file):
295f9996aaSopenharmony_ci        raise Exception("file '{}' doesn't exist.".format(parts_variants_file))
305f9996aaSopenharmony_ci    parts_variants = read_json_file(parts_variants_file)
315f9996aaSopenharmony_ci    if parts_variants is None:
325f9996aaSopenharmony_ci        raise Exception("read file '{}' failed.".format(parts_variants_file))
335f9996aaSopenharmony_ci    return parts_variants
345f9996aaSopenharmony_ci
355f9996aaSopenharmony_ci
365f9996aaSopenharmony_cidef get_build_target(build_dir: str, build_targets: list, target_platform: str) -> list:
375f9996aaSopenharmony_ci    if not target_platform or target_platform == 'all':
385f9996aaSopenharmony_ci        target_platform = 'phone'
395f9996aaSopenharmony_ci    real_targets = []
405f9996aaSopenharmony_ci    build_configs_dir = os.path.join(build_dir, 'build_configs')
415f9996aaSopenharmony_ci    parts_variants = _read_parts_variants(build_configs_dir)
425f9996aaSopenharmony_ci    for _target_name in build_targets:
435f9996aaSopenharmony_ci        if _target_name in parts_variants:
445f9996aaSopenharmony_ci            if target_platform == 'phone':
455f9996aaSopenharmony_ci                real_targets.append(_target_name)
465f9996aaSopenharmony_ci            else:
475f9996aaSopenharmony_ci                variants = parts_variants.get(_target_name)
485f9996aaSopenharmony_ci                if target_platform in variants:
495f9996aaSopenharmony_ci                    real_targets.append('{0}/{1}_{0}'.format(
505f9996aaSopenharmony_ci                        target_platform, _target_name))
515f9996aaSopenharmony_ci                else:
525f9996aaSopenharmony_ci                    real_targets.append(_target_name)
535f9996aaSopenharmony_ci        else:
545f9996aaSopenharmony_ci            if target_platform == 'phone':
555f9996aaSopenharmony_ci                real_targets.append(_target_name)
565f9996aaSopenharmony_ci            else:
575f9996aaSopenharmony_ci                real_targets.append('{0}/{1}'.format(target_platform,
585f9996aaSopenharmony_ci                                                     _target_name))
595f9996aaSopenharmony_ci    return real_targets
605f9996aaSopenharmony_ci
615f9996aaSopenharmony_ci
625f9996aaSopenharmony_cidef main() -> int:
635f9996aaSopenharmony_ci    parser = argparse.ArgumentParser()
645f9996aaSopenharmony_ci    parser.add_argument('--source-root-dir', required=True)
655f9996aaSopenharmony_ci    parser.add_argument('--root-build-dir', required=True)
665f9996aaSopenharmony_ci    parser.add_argument('--build-target-name', nargs='*')
675f9996aaSopenharmony_ci    parser.add_argument('--target-platform', required=False)
685f9996aaSopenharmony_ci    args = parser.parse_args()
695f9996aaSopenharmony_ci
705f9996aaSopenharmony_ci    source_root_dir = args.source_root_dir
715f9996aaSopenharmony_ci    if not os.path.exists(os.path.join(source_root_dir, '.gn')):
725f9996aaSopenharmony_ci        print('source root dir incorrect.')
735f9996aaSopenharmony_ci        return 1
745f9996aaSopenharmony_ci
755f9996aaSopenharmony_ci    build_dir = os.path.join(source_root_dir, args.root_build_dir)
765f9996aaSopenharmony_ci    # rewrite build.log
775f9996aaSopenharmony_ci    ninja_rules_parser.update(
785f9996aaSopenharmony_ci        build_dir, 'build_configs/platforms_info/toolchain_to_variant.json')
795f9996aaSopenharmony_ci
805f9996aaSopenharmony_ci    if args.target_platform is None or args.target_platform == 'all':
815f9996aaSopenharmony_ci        target_platform = None
825f9996aaSopenharmony_ci    else:
835f9996aaSopenharmony_ci        target_platform = args.target_platform
845f9996aaSopenharmony_ci    # process build targets
855f9996aaSopenharmony_ci    build_targets = get_build_target(build_dir, args.build_target_name,
865f9996aaSopenharmony_ci                                     target_platform)
875f9996aaSopenharmony_ci    # output build_targets string, be used on the ninja command line
885f9996aaSopenharmony_ci    print(' '.join(build_targets))
895f9996aaSopenharmony_ci    return 0
905f9996aaSopenharmony_ci
915f9996aaSopenharmony_ci
925f9996aaSopenharmony_ciif __name__ == '__main__':
935f9996aaSopenharmony_ci    sys.exit(main())
94