Skip to content

Commit fd45fec

Browse files
committed
refactor common build functionality out into separate file
to be reused in gen_docs and gen_coverage
1 parent 32c7b9f commit fd45fec

File tree

2 files changed

+166
-116
lines changed

2 files changed

+166
-116
lines changed

scripts/_build_helper.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2020-2025 Intel Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import os
18+
import shutil
19+
import subprocess
20+
import sys
21+
22+
23+
def run(cmd, env=None, cwd=None):
24+
print("+", " ".join(cmd))
25+
subprocess.check_call(
26+
cmd, env=env or os.environ.copy(), cwd=cwd or os.getcwd()
27+
)
28+
29+
30+
def warn(msg: str):
31+
print(f"[build_locally][error] {msg}", file=sys.stderr)
32+
33+
34+
def err(msg: str):
35+
print(f"[build_locally][error] {msg}", file=sys.stderr)
36+
37+
38+
def resolve_compilers(
39+
oneapi: bool, c_compiler: str, cxx_compiler: str, compiler_root: str
40+
):
41+
is_linux = "linux" in sys.platform
42+
43+
if oneapi or (
44+
c_compiler is None and cxx_compiler is None and compiler_root is None
45+
):
46+
return "icx", ("icpx" if is_linux else "icx"), None
47+
48+
if not compiler_root or not os.path.exists(compiler_root):
49+
raise RuntimeError(
50+
"--compiler-root option must be set when using non-default DPC++ "
51+
"layout"
52+
)
53+
54+
# default values
55+
if c_compiler is None:
56+
c_compiler = "icx"
57+
if cxx_compiler is None:
58+
cxx_compiler = "icpx" if is_linux else "icx"
59+
60+
for name, opt_name in (
61+
(c_compiler, "--c-compiler"),
62+
(cxx_compiler, "--cxx-compiler"),
63+
):
64+
path = (
65+
name if os.path.exists(name) else os.path.join(compiler_root, name)
66+
)
67+
if not os.path.exists(path):
68+
raise RuntimeError(f"{opt_name} value {name} not found")
69+
return c_compiler, cxx_compiler, compiler_root
70+
71+
72+
def make_cmake_args(
73+
build_type="Release",
74+
c_compiler=None,
75+
cxx_compiler=None,
76+
level_zero=True,
77+
glog=False,
78+
generator=None,
79+
verbose=False,
80+
other_opts="",
81+
):
82+
args = [
83+
f"-DCMAKE_BUILD_TYPE={build_type}",
84+
f"-DCMAKE_C_COMPILER:PATH={c_compiler}" if c_compiler else "",
85+
f"-DCMAKE_CXX_COMPILER:PATH={cxx_compiler}" if cxx_compiler else "",
86+
f"-DDPCTL_ENABLE_L0_PROGRAM_CREATION={'ON' if level_zero else 'OFF'}",
87+
f"-DDPTL_ENABLE_GLOG:BOOL={'ON' if glog else 'OFF'}",
88+
]
89+
90+
if generator:
91+
args.append(f"-G{generator}")
92+
if verbose:
93+
args.append("-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON")
94+
if other_opts:
95+
args.extend(other_opts.split())
96+
97+
return " ".join(filter(None, args))
98+
99+
100+
def build_extension(setup_dir, env):
101+
run(
102+
[sys.executable, "setup.py", "build_ext", "--inplace"],
103+
env=env,
104+
cwd=setup_dir,
105+
)
106+
107+
108+
def install_editable(setup_dir, env):
109+
run(
110+
[
111+
sys.executable,
112+
"-m",
113+
"pip",
114+
"install",
115+
"-e",
116+
".",
117+
"--no-build-isolation",
118+
],
119+
env=env,
120+
cwd=setup_dir,
121+
)
122+
123+
124+
def clean_build_dir(build_dir):
125+
if os.path.exists(build_dir):
126+
print(f"Cleaning build directory: {build_dir}")
127+
shutil.rmtree(build_dir)

scripts/build_locally.py

Lines changed: 39 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,9 @@
1616

1717
import argparse
1818
import os
19-
import shutil
20-
import subprocess
2119
import sys
2220

23-
24-
def run(cmd, env=None, cwd=None):
25-
print("+", " ".join(cmd))
26-
subprocess.check_call(cmd, env=env, cwd=cwd or os.getcwd())
27-
28-
29-
def _warn(msg: str):
30-
print(f"[build_locally][error] {msg}", file=sys.stderr)
31-
32-
33-
def _err(msg: str):
34-
print(f"[build_locally][error] {msg}", file=sys.stderr)
21+
from . import build_helper as bh
3522

3623

3724
def parse_args():
@@ -142,137 +129,73 @@ def parse_args():
142129
return p.parse_args()
143130

144131

145-
def resolve_compilers(args):
146-
is_linux = "linux" in sys.platform
147-
148-
if args.oneapi or (
149-
args.c_compiler is None
150-
and args.cxx_compiler is None
151-
and args.compiler_root is None
152-
):
153-
args.c_compiler = "icx"
154-
args.cxx_compiler = "icpx" if is_linux else "icx"
155-
args.compiler_root = None
156-
return
157-
158-
cr = args.compiler_root
159-
if isinstance(cr, str) and os.path.exists(cr):
160-
if args.c_compiler is None:
161-
args.c_compiler = "icx"
162-
if args.cxx_compiler is None:
163-
args.cxx_compiler = "icpx" if is_linux else "icx"
164-
else:
165-
raise RuntimeError(
166-
"'compiler-root' option must be set when using non-default DPC++ "
167-
"layout"
168-
)
169-
170-
for opt_name in ("c_compiler", "cxx_compiler"):
171-
arg = getattr(args, opt_name)
172-
if not arg:
173-
continue
174-
if not os.path.exists(arg):
175-
probe = os.path.join(cr, arg)
176-
if os.path.exists(probe):
177-
setattr(args, opt_name, probe)
178-
continue
179-
if not os.path.exists(getattr(args, opt_name)):
180-
raise RuntimeError(
181-
f"{opt_name.replace('_', '-')} value {arg} not found"
182-
)
183-
184-
185132
def main():
186133
if sys.platform not in ["cygwin", "win32", "linux"]:
187-
_err(f"{sys.platform} not supported")
134+
bh.err(f"{sys.platform} not supported")
188135
args = parse_args()
189136
setup_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
190137
build_dir = os.path.join(setup_dir, args.build_dir)
191138

192-
resolve_compilers(args)
139+
c_compiler, cxx_compiler, compiler_root = bh.resolve_compilers(
140+
args.oneapi, args.c_compiler, args.cxx_compiler, args.compiler_root
141+
)
193142

194143
# clean build dir if --clean set
195-
if args.clean and os.path.exists(build_dir):
196-
print(f"[build_locally] Cleaning build directory: {build_dir}")
197-
shutil.rmtree(build_dir)
144+
if args.clean:
145+
bh.clean_build_dir(build_dir)
198146

199-
env = os.environ.copy()
147+
if args.no_level_zero and args.target_level_zero:
148+
bh.err("Cannot combine --no-level-zero and --target-level-zero")
200149

201-
# ignore pre-existing CMAKE_ARGS for determinism in build driver
202-
if "CMAKE_ARGS" in env and env["CMAKE_ARGS"].strip():
203-
_warn("Ignoring pre-existing CMAKE_ARGS in environment")
204-
del env["CMAKE_ARGS"]
150+
# Level Zero state (on unless explicitly disabled)
151+
if args.no_level_zero:
152+
level_zero_enabled = False
153+
elif args.target_level_zero:
154+
level_zero_enabled = True
155+
else:
156+
level_zero_enabled = True
205157

206-
cmake_defs = []
158+
cmake_args = bh.make_cmake_args(
159+
build_type=args.build_type,
160+
c_compiler=c_compiler,
161+
cxx_compiler=cxx_compiler,
162+
level_zero=level_zero_enabled,
163+
glogs=args.glog,
164+
generator=args.generator,
165+
verbose=args.verbose,
166+
other_opts=args.cmake_opts,
167+
)
207168

208169
# handle architecture conflicts
209170
if args.target_hip is not None and not args.target_hip.strip():
210-
_err("--target-hip requires an explicit architecture")
211-
212-
if args.no_level_zero and args.target_level_zero:
213-
_err("Cannot combine --no-level-zero and --target-level-zero")
171+
bh.err("--target-hip requires an explicit architecture")
214172

215173
# CUDA/HIP targets
216174
if args.target_cuda:
217-
cmake_defs.append(f"-DDPCTL_TARGET_CUDA={args.target_cuda}")
175+
cmake_args.append(f"-DDPCTL_TARGET_CUDA={args.target_cuda}")
218176
if args.target_hip:
219-
cmake_defs.append(f"-DDPCTL_TARGET_HIP={args.target_hip}")
177+
cmake_args.append(f"-DDPCTL_TARGET_HIP={args.target_hip}")
220178

221-
# Level Zero state (on unless explicitly disabled)
222-
if args.no_level_zero:
223-
level_zero_enabled = False
224-
elif args.target_level_zero:
225-
level_zero_enabled = True
226-
else:
227-
level_zero_enabled = True
228-
cmake_defs.append(
179+
cmake_args.append(
229180
"-DDPCTL_ENABLE_L0_PROGRAM_CREATION="
230181
f"{'ON' if level_zero_enabled else 'OFF'}"
231182
)
232183

233-
# compilers and generator
234-
if args.c_compiler:
235-
cmake_defs.append(f"-DCMAKE_C_COMPILER:PATH={args.c_compiler}")
236-
if args.cxx_compiler:
237-
cmake_defs.append(f"-DCMAKE_CXX_COMPILER:PATH={args.cxx_compiler}")
238-
if args.generator:
239-
cmake_defs.append(f"-G{args.generator}")
240-
241-
cmake_defs.append(
242-
f"-DDPCTL_ENABLE_GLOG:BOOL={'ON' if args.glog else 'OFF'}"
243-
)
244-
cmake_defs.append(f"-DCMAKE_BUILD_TYPE={args.build_type}")
245-
if args.verbose:
246-
cmake_defs.append("-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON")
184+
env = os.environ.copy()
247185

248-
if args.cmake_opts:
249-
cmake_defs.extend(args.cmake_opts.split())
186+
# ignore pre-existing CMAKE_ARGS for determinism in build driver
187+
if "CMAKE_ARGS" in env and env["CMAKE_ARGS"].strip():
188+
bh.warn("Ignoring pre-existing CMAKE_ARGS in environment")
189+
del env["CMAKE_ARGS"]
250190

251-
env["CMAKE_ARGS"] = " ".join(cmake_defs)
252-
print(f"[build_locally] CMake args:\n {' '.join(cmake_defs)}")
191+
env["CMAKE_ARGS"] = " ".join(cmake_args)
192+
print(f"[build_locally] CMake args:\n {' '.join(cmake_args)}")
253193

254194
print("[build_locally] Building extensions in-place...")
255-
run(
256-
[sys.executable, "setup.py", "build_ext", "--inplace"],
257-
env=env,
258-
cwd=setup_dir,
259-
)
260195

196+
bh.build_extensions(setup_dir, env)
261197
if not args.skip_editable:
262-
print("[build_locally] Installing dpctl in editable mode")
263-
run(
264-
[
265-
sys.executable,
266-
"-m",
267-
"pip",
268-
"install",
269-
"-e",
270-
".",
271-
"--no-build-isolation",
272-
],
273-
env=env,
274-
cwd=setup_dir,
275-
)
198+
bh.install_editable(setup_dir, env)
276199
else:
277200
print("[build_locally] Skipping editable install (--skip-editable)")
278201

0 commit comments

Comments
 (0)