• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
21import platform
22
23from exceptions.ohos_exception import OHOSException
24from services.interface.build_executor_interface import BuildExecutorInterface
25from resources.config import Config
26from util.system_util import SystemUtil, ExecEnviron
27from util.io_util import IoUtil
28from util.log_util import LogUtil
29
30
31class Ninja(BuildExecutorInterface):
32
33    def __init__(self):
34        super().__init__()
35        self.config = Config()
36        self._regist_ninja_path()
37
38    def run(self):
39        self._execute_ninja_cmd()
40
41    def _execute_ninja_cmd(self):
42        """execute ninja cmd using registed args
43        :raise OHOSException: when ninja failed
44        """
45        ninja_cmd = [self.exec, '-w', 'dupbuild=warn',
46                     '-C', self.config.out_path] + self._convert_args()
47        LogUtil.write_log(self.config.log_path,
48                          'Excuting ninja command: {}'.format(' '.join(ninja_cmd)), 'info')
49
50        ninja_env = ExecEnviron()
51        ninja_env_allowlist = IoUtil.read_json_file(
52            os.path.join(
53                self.config.root_path,
54                "out/preloader",
55                self.config.product,
56                "compile_env_allowlist.json",
57            )
58        ).get("ninja")
59
60        if ninja_env_allowlist:
61            ninja_env.initenv()
62            ninja_env.allow(ninja_env_allowlist)
63            LogUtil.write_log(
64                self.config.log_path,
65                f"run ninja with environ {ninja_env.allkeys}",
66                "info",
67            )
68
69        log_filter = os.getenv("LOG_FILTER", "True") == "True"
70        SystemUtil.exec_command(
71            ninja_cmd,
72            self.config.log_path,
73            exec_env=ninja_env.allenv,
74            log_filter=log_filter,
75            log_mode=self.config.log_mode
76        )
77
78    def _convert_args(self) -> list:
79        """convert all registed args into a list
80        """
81        args_list = []
82        for key, value in self._args_dict.items():
83            if key == 'build_target' and isinstance(value, list):
84                args_list += value
85            elif key == 'ninja_args' and isinstance(value, list):
86                args_list += value
87            else:
88                if value == '':
89                    args_list.insert(0, key)
90                else:
91                    args_list.insert(0, ' {}{} '.format(key, value))
92        return args_list
93
94    def _regist_ninja_path(self):
95        """find ninja executable
96        :raise OHOSException: when can't find the ninja excutable
97        """
98        if sys.platform == "linux" and platform.machine().lower() == "aarch64":
99            ninja_path = os.path.join(self.config.root_path, 'prebuilts/build-tools/{}-aarch64/bin/ninja'
100                .format(sys.platform))
101        else:
102            ninja_path = os.path.join(self.config.root_path, 'prebuilts/build-tools/{}-x86/bin/ninja'
103                .format(sys.platform))
104        if os.path.exists(ninja_path):
105            self.exec = ninja_path
106        else:
107            raise OHOSException(
108                'There is no gn executable file at {}'.format(ninja_path), '0001')
109