• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4#
5# Copyright (c) 2022 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 importlib
22import subprocess
23
24
25def search(findir, target):
26    for root, dirs, files in os.walk(findir):
27        if target in files:
28            return root
29    return False
30
31
32def find_top():
33    cur_dir = os.getcwd()
34    while cur_dir != "/":
35        build_config_file = os.path.join(
36            cur_dir, 'build/config/BUILDCONFIG.gn')
37        if os.path.exists(build_config_file):
38            return cur_dir
39        cur_dir = os.path.dirname(cur_dir)
40
41
42def get_python():
43    topdir = find_top()
44    python_base_dir = os.path.join(topdir, 'prebuilts/python')
45    if os.path.exists(python_base_dir):
46        python_dir = search(python_base_dir, 'python3')
47        return os.path.join(python_dir, 'python3')
48    else:
49        print("please execute build/prebuilts_download.sh")
50        sys.exit()
51
52
53def check_output(cmd, **kwargs):
54    process = subprocess.Popen(cmd,
55                               stdout=subprocess.PIPE,
56                               stderr=subprocess.STDOUT,
57                               universal_newlines=True,
58                               **kwargs)
59    for line in iter(process.stdout.readline, ''):
60        sys.stdout.write(line)
61        sys.stdout.flush()
62
63    process.wait()
64    ret_code = process.returncode
65
66    return ret_code
67
68
69def build(path, args_list):
70    python_executable = get_python()
71    cmd = [python_executable, 'build/hb/main.py', 'build'] + args_list
72    return check_output(cmd, cwd=path)
73
74
75def main():
76    root_path = find_top()
77    return build(root_path, sys.argv[1:])
78
79
80if __name__ == "__main__":
81    sys.exit(main())
82