forked from hw-native-sys/simpler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoolchain.py
More file actions
168 lines (137 loc) · 6.01 KB
/
toolchain.py
File metadata and controls
168 lines (137 loc) · 6.01 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import os
from typing import List
class AICoreToolchain:
"""
AICore toolchain for compiling AICore kernels.
No path validation needed - caller ensures paths are valid.
"""
def __init__(self, cc: str, ld: str, aicore_dir: str, aicore_binary: str = "aicore_kernel.o"):
"""
Initialize the AICore toolchain.
Args:
cc: Path to the ccec C compiler (e.g., /opt/ascend/bin/ccec)
ld: Path to the ld.lld linker (e.g., /opt/ascend/bin/ld.lld)
aicore_dir: Path to the AICore source directory
aicore_binary: Name of the AICore binary output, default is "aicore_kernel.o"
"""
self.cc = cc
self.ld = ld
self.aicore_dir = os.path.abspath(aicore_dir)
self.aicore_binary = aicore_binary
def get_root_dir(self) -> str:
"""Get the AICore source root directory."""
return self.aicore_dir
def get_binary_name(self) -> str:
"""Get the output binary name."""
return self.aicore_binary
def gen_cmake_args(self, include_dirs: List[str], source_dirs: List[str]) -> str:
"""
Generate CMake arguments for the AICore toolchain.
Args:
include_dirs: List of include directory paths
source_dirs: List of source directory paths
Returns:
String of CMake command-line arguments
"""
include_dirs = [os.path.abspath(d) for d in include_dirs]
source_dirs = [os.path.abspath(d) for d in source_dirs]
include_dirs_list = ";".join(include_dirs)
source_dirs_list = ";".join(source_dirs)
return " ".join([
f"-DBISHENG_CC={self.cc}",
f"-DBISHENG_LD={self.ld}",
f"-DCUSTOM_INCLUDE_DIRS={include_dirs_list}",
f"-DCUSTOM_SOURCE_DIRS={source_dirs_list}",
])
class AICPUToolchain:
"""
AICPU toolchain for compiling AICPU kernels (ARM64 device task scheduler).
No path validation needed - caller ensures paths are valid.
"""
def __init__(self, cc: str, cxx: str, aicpu_dir: str, aicpu_binary: str = "libaicpu_kernel.so", ascend_home_path: str = None):
"""
Initialize the AICPU toolchain.
Args:
cc: Path to the cross-compiler C compiler (e.g., aarch64-target-linux-gnu-gcc)
cxx: Path to the cross-compiler C++ compiler (e.g., aarch64-target-linux-gnu-g++)
aicpu_dir: Path to the AICPU source directory
aicpu_binary: Name of the AICPU binary output, default is "libaicpu_kernel.so"
ascend_home_path: Path to Ascend home directory, defaults to ASCEND_HOME_PATH environment variable
"""
self.cc = cc
self.cxx = cxx
self.aicpu_dir = os.path.abspath(aicpu_dir)
self.aicpu_binary = aicpu_binary
self.ascend_home_path = ascend_home_path
def get_root_dir(self) -> str:
"""Get the AICPU source root directory."""
return self.aicpu_dir
def get_binary_name(self) -> str:
"""Get the output binary name."""
return self.aicpu_binary
def gen_cmake_args(self, include_dirs: List[str], source_dirs: List[str]) -> str:
"""
Generate CMake arguments for the AICPU toolchain.
Args:
include_dirs: List of include directory paths
source_dirs: List of source directory paths
Returns:
String of CMake command-line arguments
"""
include_dirs = [os.path.abspath(d) for d in include_dirs]
source_dirs = [os.path.abspath(d) for d in source_dirs]
include_dirs_list = ";".join(include_dirs)
source_dirs_list = ";".join(source_dirs)
return " ".join([
f"-DCMAKE_C_COMPILER={self.cc}",
f"-DCMAKE_CXX_COMPILER={self.cxx}",
f"-DASCEND_HOME_PATH={self.ascend_home_path}",
f"-DCUSTOM_INCLUDE_DIRS={include_dirs_list}",
f"-DCUSTOM_SOURCE_DIRS={source_dirs_list}",
])
class HostToolchain:
"""
Host toolchain for compiling host runtime library (ARM64 CPU shared library).
No path validation needed - caller ensures paths are valid.
"""
def __init__(self, cc: str, cxx: str, host_dir: str, binary_name: str = "libhost_runtime.so", ascend_home_path: str = None):
"""
Initialize the Host toolchain.
Args:
cc: Path to the C compiler (e.g., gcc, arm-linux-gcc)
cxx: Path to the C++ compiler (e.g., g++, arm-linux-g++)
host_dir: Path to the host source directory
binary_name: Name of the shared library output, default is "libhost_runtime.so"
ascend_home_path: Path to Ascend home directory, defaults to ASCEND_HOME_PATH environment variable
"""
self.cc = cc
self.cxx = cxx
self.host_dir = os.path.abspath(host_dir)
self.binary_name = binary_name
self.ascend_home_path = ascend_home_path or os.getenv("ASCEND_HOME_PATH", "")
def get_root_dir(self) -> str:
"""Get the host source root directory."""
return self.host_dir
def get_binary_name(self) -> str:
"""Get the output binary name."""
return self.binary_name
def gen_cmake_args(self, include_dirs: List[str], source_dirs: List[str]) -> str:
"""
Generate CMake arguments for the Host toolchain.
Args:
include_dirs: List of include directory paths
source_dirs: List of source directory paths
Returns:
String of CMake command-line arguments
"""
include_dirs = [os.path.abspath(d) for d in include_dirs]
source_dirs = [os.path.abspath(d) for d in source_dirs]
include_dirs_list = ";".join(include_dirs)
source_dirs_list = ";".join(source_dirs)
return " ".join([
f"-DCMAKE_C_COMPILER={self.cc}",
f"-DCMAKE_CXX_COMPILER={self.cxx}",
f"-DASCEND_HOME_PATH={self.ascend_home_path}",
f"-DCUSTOM_INCLUDE_DIRS={include_dirs_list}",
f"-DCUSTOM_SOURCE_DIRS={source_dirs_list}",
])