build.py 2.3 KB
Newer Older
Z
zhengweiwei2 已提交
1
#!/usr/bin/env python3
W
wenjun 已提交
2 3 4
# -*- coding: utf-8 -*-

#
Z
zhengweiwei2 已提交
5
# Copyright (c) 2022 Huawei Device Co., Ltd.
W
wenjun 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import os
M
mamingshuai 已提交
20
import sys
21
import importlib
M
mamingshuai 已提交
22
import subprocess
W
wenjun 已提交
23 24


Z
zhengweiwei2 已提交
25 26 27 28 29 30 31 32 33 34 35 36
def get_python():
    hb_main = importlib.import_module("hb.__main__")
    topdir = hb_main.find_top()
    python_base_dir = os.path.join(topdir, 'prebuilts/python')
    if os.path.exists(python_base_dir):
        python_dir = hb_main.search(python_base_dir, 'python3')
        return os.path.join(python_dir, 'python3')
    else:
        print("please execute preload_download.sh")
        sys.exit()


M
mamingshuai 已提交
37 38 39
def check_output(cmd, **kwargs):
    process = subprocess.Popen(cmd,
                               stdout=subprocess.PIPE,
P
pilipala195 已提交
40
                               stderr=subprocess.STDOUT,
M
mamingshuai 已提交
41 42 43 44 45
                               universal_newlines=True,
                               **kwargs)
    for line in iter(process.stdout.readline, ''):
        sys.stdout.write(line)
        sys.stdout.flush()
W
wenjun 已提交
46

M
mamingshuai 已提交
47 48
    process.wait()
    ret_code = process.returncode
W
wenjun 已提交
49

M
mamingshuai 已提交
50
    return ret_code
W
wenjun 已提交
51 52


M
mamingshuai 已提交
53
def set_root_path(path):
54 55 56
    sys.path.insert(0, os.path.join(path, 'build/lite'))
    module = importlib.import_module('hb_internal.set.set')
    return module.set_root_path(root_path=path)
W
wenjun 已提交
57 58


M
mamingshuai 已提交
59
def build(path, args_list):
Z
zhengweiwei2 已提交
60
    python_executable = get_python()
L
lubinglun 已提交
61 62 63 64 65
    cmd = [python_executable, 'build/hb/main.py', 'build'] + args_list
    for args in args_list:
        if "using_hb_new=false" in args:
            cmd = [python_executable, 'build/lite/hb/__main__.py', 'build'] + args_list
            break
M
mamingshuai 已提交
66 67 68 69 70 71 72 73 74
    return check_output(cmd, cwd=path)


def main():
    root_path = os.path.dirname(os.path.abspath(__file__))
    ret_code = set_root_path(root_path)
    if ret_code != 0:
        return ret_code
    return build(root_path, sys.argv[1:])
W
wenjun 已提交
75 76 77 78


if __name__ == "__main__":
    sys.exit(main())