1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4#
5# Copyright (c) 2023 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19import os
20import sys
21
22from containers.arg import Arg
23from containers.arg import ModuleType
24from resolver.interface.args_resolver_interface import ArgsResolverInterface
25from modules.interface.indep_build_module_interface import IndepBuildModuleInterface
26from util.component_util import ComponentUtil
27from exceptions.ohos_exception import OHOSException
28
29
30class IndepBuildArgsResolver(ArgsResolverInterface):
31
32    def __init__(self, args_dict: dict):
33        super().__init__(args_dict)
34
35    @staticmethod
36    def resolve_target_cpu(target_arg: Arg, indep_build_module: IndepBuildModuleInterface):
37        build_executor = indep_build_module.hpm
38        if target_arg.arg_value:
39            build_executor.regist_flag('cpu', target_arg.arg_value)
40        else:
41            args_dict = Arg.read_args_file(ModuleType.ENV)
42            arg = args_dict.get("target_cpu")
43            build_executor.regist_flag('cpu', arg.get("argDefault"))
44
45    @staticmethod
46    def resolve_target_os(target_arg: Arg, indep_build_module: IndepBuildModuleInterface):
47        build_executor = indep_build_module.hpm
48        if target_arg.arg_value:
49            build_executor.regist_flag('os', target_arg.arg_value)
50        else:
51            args_dict = Arg.read_args_file(ModuleType.ENV)
52            arg = args_dict.get("target_os")
53            build_executor.regist_flag('os', arg.get("argDefault"))
54
55    @staticmethod
56    def resolve_part(target_arg: Arg, indep_build_module: IndepBuildModuleInterface):
57        '''
58        编译部件名获取优先级: hb build 指定的部件名参数  > hb build 在部件源码仓运行时通过找到bundle.json获取到的部件名 > hb env 设置的部件名参数
59        '''
60        build_executor = indep_build_module.hpm
61        if len(sys.argv) > 2 and not sys.argv[2].startswith("-"):  # 第一个部件名参数
62            target_arg.arg_value = sys.argv[2]
63
64        if target_arg.arg_value:
65            try:
66                bundle_path = ComponentUtil.search_bundle_file(target_arg.arg_value)
67            except Exception as e:
68                raise OHOSException('Please check the bundle.json file of {} : {}'.format(target_arg.arg_value, e))
69            if not bundle_path:
70                raise OHOSException(
71                    'ERROR argument "hb build <part_name>": Invalid part_name "{}". '.format(target_arg.arg_value))
72            build_executor.regist_flag('path', bundle_path)
73        elif ComponentUtil.is_in_component_dir(os.getcwd()):
74            part_name, bundle_path = ComponentUtil.get_component(os.getcwd())
75            if part_name:
76                target_arg.arg_value = part_name
77                build_executor.regist_flag('path', bundle_path)
78            else:
79                raise OHOSException('ERROR argument "no bundle.json": Invalid directory "{}". '.format(os.getcwd()))
80        else:
81            args_dict = Arg.read_args_file(ModuleType.ENV)
82            arg = args_dict.get("part")
83            if arg.get("argDefault"):
84                bundle_path = ComponentUtil.search_bundle_file(arg.get("argDefault"))
85                if not bundle_path:
86                    raise OHOSException('ERROR argument "hb env --part <part_name>": Invalid part_name "{}". '.format(
87                        target_arg.arg_value))
88                build_executor.regist_flag('path', bundle_path)
89            else:
90                raise OHOSException('ERROR argument "hb build <part_name>": no part_name . ')
91
92    @staticmethod
93    def resolve_variant(target_arg: Arg, indep_build_module: IndepBuildModuleInterface):
94        build_executor = indep_build_module.hpm
95        if target_arg.arg_value:
96            build_executor.regist_flag('defaultDeps', ComponentUtil.get_default_deps(target_arg.arg_value))
97        else:
98            build_executor.regist_flag('defaultDeps', ComponentUtil.get_default_deps("default"))
99