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