Repository navigation
Expand file tree
/
Copy path_build_helper.py
More file actions
185 lines (159 loc) · 5.23 KB
/
Copy path_build_helper.py
File metadata and controls
185 lines (159 loc) · 5.23 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# Data Parallel Control (dpctl)
#
# Copyright 2025 Intel Corporation
#
# 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
import shutil
import subprocess
import sys
def resolve_compilers(
oneapi: bool,
c_compiler: str,
cxx_compiler: str,
compiler_root: str,
sycl_provider: str = "Intel",
):
is_linux = "linux" in sys.platform
if sycl_provider == "AdaptiveCpp":
if oneapi:
raise RuntimeError(
"--oneapi is specific to DPC++ and can not be used with the "
"AdaptiveCpp SYCL provider"
)
default_c_compiler, default_cxx_compiler = "clang", "acpp"
else:
default_c_compiler = "icx"
default_cxx_compiler = "icpx" if is_linux else "icx"
if oneapi or (
c_compiler is None and cxx_compiler is None and compiler_root is None
):
return default_c_compiler, default_cxx_compiler
if (
(c_compiler is None or not os.path.isabs(c_compiler))
and (cxx_compiler is None or not os.path.isabs(cxx_compiler))
and (not compiler_root or not os.path.exists(compiler_root))
):
raise RuntimeError(
"--compiler-root option must be set when using non-default DPC++ "
"layout unless absolute paths are provided for both compilers"
)
# default values
if c_compiler is None:
c_compiler = default_c_compiler
if cxx_compiler is None:
cxx_compiler = default_cxx_compiler
for name, opt_name in (
(c_compiler, "--c-compiler"),
(cxx_compiler, "--cxx-compiler"),
):
if os.path.isabs(name):
path = name
elif compiler_root:
path = os.path.join(compiler_root, name)
else:
# a default name with no compiler root is looked up in PATH
path = shutil.which(name)
if not path or not os.path.exists(path):
raise RuntimeError(f"{opt_name} value {name} not found")
return c_compiler, cxx_compiler
def run(cmd: list[str], env: dict[str, str] = None, cwd: str = None):
print("+", " ".join(cmd))
subprocess.check_call(
cmd, env=env or os.environ.copy(), cwd=cwd or os.getcwd()
)
def capture_cmd_output(cmd: list[str], cwd: str = None):
print("+", " ".join(cmd))
return (
subprocess.check_output(cmd, cwd=cwd or os.getcwd())
.decode("utf-8")
.strip("\n")
)
def err(msg: str, script: str):
raise RuntimeError(f"[{script}] error: {msg}")
def log_cmake_args(cmake_args: list[str], script: str):
print(f"[{script}] Using CMake args:\n{' '.join(cmake_args)}")
def make_cmake_args(
c_compiler: str = None,
cxx_compiler: str = None,
level_zero: bool = True,
glog: bool = False,
verbose: bool = False,
sycl_provider: str = "Intel",
other_opts: str = None,
):
args = [
f"-DCMAKE_C_COMPILER:PATH={c_compiler}" if c_compiler else "",
f"-DCMAKE_CXX_COMPILER:PATH={cxx_compiler}" if cxx_compiler else "",
"-DDPCTL_ENABLE_L0_KERNEL_BUNDLE_CREATION="
f"{'ON' if level_zero else 'OFF'}",
f"-DDPCTL_ENABLE_GLOG:BOOL={'ON' if glog else 'OFF'}",
f"-DDPCTL_SYCL_PROVIDER={sycl_provider}",
]
if verbose:
args.append("-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON")
if other_opts:
args.extend(other_opts.split())
return args
def build_extension(
setup_dir: str,
env: dict[str, str],
cmake_args: list[str],
cmake_executable: str = None,
generator: str = None,
build_type: str = None,
):
cmd = [sys.executable, "setup.py", "build_ext", "--inplace"]
if cmake_executable:
cmd.append(f"--cmake-executable={cmake_executable}")
if generator:
cmd.append(f"--generator={generator}")
if build_type:
cmd.append(f"--build-type={build_type}")
if cmake_args:
cmd.append("--")
cmd += cmake_args
run(
cmd,
env=env,
cwd=setup_dir,
)
def install_editable(setup_dir: str, env: dict[str, str]):
run(
[
sys.executable,
"-m",
"pip",
"install",
"-e",
".",
"--no-build-isolation",
],
env=env,
cwd=setup_dir,
)
def clean_build_dir(setup_dir: str):
if (
not isinstance(setup_dir, str)
or not setup_dir
or not os.path.isdir(setup_dir)
):
raise RuntimeError(f"Invalid setup directory provided: '{setup_dir}'")
target = os.path.join(setup_dir, "_skbuild")
if os.path.exists(target):
print(f"Cleaning build directory: {target}")
try:
shutil.rmtree(target)
except Exception as e:
print(f"Failed to remove build directory: '{target}'")
raise e