-
Notifications
You must be signed in to change notification settings - Fork 118
/
ohos-build
executable file
·79 lines (59 loc) · 2.65 KB
/
ohos-build
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import platform
import shutil
import subprocess
import sys
from typing import Dict, Optional
def get_target_from_args() -> Optional[str]:
for arg in sys.argv:
if arg.startswith("--target="):
return arg.replace("--target=", "")
return None
def set_toolchain_binaries_in_env(toolchain_dir: str, target_triple: str, env: Dict[str, str]):
toolchain_bin_dir = os.path.join(toolchain_dir, "bin")
cc = os.path.join(toolchain_bin_dir, f"{target_triple}-clang")
cxx = os.path.join(toolchain_bin_dir, f"{target_triple}-clang++")
ar = os.path.join(toolchain_bin_dir, "llvm-ar")
objcopy = os.path.join(toolchain_bin_dir, "llvm-objcopy")
ranlib = os.path.join(toolchain_bin_dir, "llvm-ranlib")
strip = os.path.join(toolchain_bin_dir, "llvm-strip")
host_cc = env.get('HOST_CC') or shutil.which("clang") or shutil.which("gcc")
host_cxx = env.get('HOST_CXX') or shutil.which("clang++") or shutil.which("g++")
assert host_cc
assert host_cxx
env["AR"] = ar
env["CC"] = cc
env["CPP"] = f"{cc} -E"
env["CXX"] = cxx
env["HOST_CC"] = host_cc
env["HOST_CXX"] = host_cxx
env["OBJCOPY"] = objcopy
env["RANLIB"] = ranlib
env["STRIP"] = strip
target_triple_underscore = target_triple.upper().replace('-', '_')
env[f'CARGO_TARGET_{target_triple_underscore}_LINKER'] = cc
# bindgen / libclang-sys
env["LIBCLANG_PATH"] = os.path.join(toolchain_dir, "lib")
env["CLANG_PATH"] = cxx
env[f'CXXSTDLIB_{target_triple_underscore}'] = "c++"
def create_environment_for_build() -> Dict[str, str]:
env = os.environ.copy()
if "OHOS_SDK_NATIVE" not in env:
raise Exception("Please set the OHOS_SDK_NATIVE environment variable.")
ndk_root = env["OHOS_SDK_NATIVE"]
target_triple = get_target_from_args()
assert target_triple is not None, "OpenHarmony build requires a target triple!"
os_type = platform.system().lower()
if os_type not in ["linux", "darwin"]:
raise Exception("OpenHarmony builds are currently only supported on Linux and macOS Hosts.")
llvm_toolchain = os.path.join(ndk_root, "llvm")
env['PATH'] = os.pathsep.join([os.path.join(llvm_toolchain, "bin"), env["PATH"]])
set_toolchain_binaries_in_env(llvm_toolchain, target_triple, env)
return env
if __name__ == "__main__":
completed_process = subprocess.run(sys.argv[1:], env=create_environment_for_build())
sys.exit(completed_process.returncode)