Skip to content

Commit 6c33bf0

Browse files
committed
use common resolve_compilers utility
1 parent f327741 commit 6c33bf0

File tree

3 files changed

+89
-97
lines changed

3 files changed

+89
-97
lines changed

scripts/_build_helper.py

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,64 @@
2020
import sys
2121

2222

23-
def run(cmd, env=None, cwd=None):
23+
def resolve_compilers(
24+
oneapi: bool,
25+
c_compiler: str,
26+
cxx_compiler: str,
27+
compiler_root: str,
28+
):
29+
is_linux = "linux" in sys.platform
30+
31+
if oneapi or (
32+
c_compiler is None and cxx_compiler is None and compiler_root is None
33+
):
34+
return "icx", ("icpx" if is_linux else "icx")
35+
36+
if (
37+
(c_compiler is None or not os.path.isabs(c_compiler))
38+
and (cxx_compiler is None or not os.path.isabs(cxx_compiler))
39+
and (not compiler_root or not os.path.exists(compiler_root))
40+
):
41+
raise RuntimeError(
42+
"--compiler-root option must be set when using non-default DPC++ "
43+
"layout unless absolute paths are provided for both compilers"
44+
)
45+
46+
# default values
47+
if c_compiler is None:
48+
c_compiler = "icx"
49+
if cxx_compiler is None:
50+
cxx_compiler = "icpx" if is_linux else "icx"
51+
52+
for name, opt_name in (
53+
(c_compiler, "--c-compiler"),
54+
(cxx_compiler, "--cxx-compiler"),
55+
):
56+
if os.path.isabs(name):
57+
path = name
58+
else:
59+
path = os.path.join(compiler_root, name)
60+
if not os.path.exists(path):
61+
raise RuntimeError(f"{opt_name} value {name} not found")
62+
return c_compiler, cxx_compiler
63+
64+
65+
def run(cmd: list[str], env: dict[str, str] = None, cwd: str = None):
2466
print("+", " ".join(cmd))
2567
subprocess.check_call(
2668
cmd, env=env or os.environ.copy(), cwd=cwd or os.getcwd()
2769
)
2870

2971

72+
def get_output(cmd: list[str], cwd: str = None):
73+
print("+", " ".join(cmd))
74+
return (
75+
subprocess.check_output(cmd, cwd=cwd or os.getcwd())
76+
.decode("utf-8")
77+
.strip("\n")
78+
)
79+
80+
3081
def warn(msg: str, script: str):
3182
print(f"[{script}][warning] {msg}", file=sys.stderr)
3283

@@ -36,12 +87,12 @@ def err(msg: str, script: str):
3687

3788

3889
def make_cmake_args(
39-
c_compiler=None,
40-
cxx_compiler=None,
41-
level_zero=True,
42-
glog=False,
43-
verbose=False,
44-
other_opts="",
90+
c_compiler: str = None,
91+
cxx_compiler: str = None,
92+
level_zero: bool = True,
93+
glog: bool = False,
94+
verbose: bool = False,
95+
other_opts: str = None,
4596
):
4697
args = [
4798
f"-DCMAKE_C_COMPILER:PATH={c_compiler}" if c_compiler else "",
@@ -59,7 +110,11 @@ def make_cmake_args(
59110

60111

61112
def build_extension(
62-
setup_dir, env, cmake_executable=None, generator=None, build_type=None
113+
setup_dir: str,
114+
env: dict[str, str],
115+
cmake_executable: str = None,
116+
generator: str = None,
117+
build_type: str = None,
63118
):
64119
cmd = [sys.executable, "setup.py", "build_ext", "--inplace"]
65120
if cmake_executable:
@@ -75,7 +130,7 @@ def build_extension(
75130
)
76131

77132

78-
def install_editable(setup_dir, env):
133+
def install_editable(setup_dir: str, env: dict[str, str]):
79134
run(
80135
[
81136
sys.executable,
@@ -91,7 +146,7 @@ def install_editable(setup_dir, env):
91146
)
92147

93148

94-
def clean_build_dir(setup_dir):
149+
def clean_build_dir(setup_dir: str = None):
95150
target = os.path.join(setup_dir or os.getcwd(), "_skbuild")
96151
if os.path.exists(target):
97152
print(f"Cleaning build directory: {target}")

scripts/build_locally.py

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,44 +27,11 @@
2727
err,
2828
install_editable,
2929
make_cmake_args,
30+
resolve_compilers,
3031
warn,
3132
)
3233

3334

34-
def resolve_compilers(
35-
oneapi: bool, c_compiler: str, cxx_compiler: str, compiler_root: str
36-
):
37-
is_linux = "linux" in sys.platform
38-
39-
if oneapi or (
40-
c_compiler is None and cxx_compiler is None and compiler_root is None
41-
):
42-
return "icx", ("icpx" if is_linux else "icx")
43-
44-
if not compiler_root or not os.path.exists(compiler_root):
45-
raise RuntimeError(
46-
"--compiler-root option must be set when using non-default DPC++ "
47-
"layout"
48-
)
49-
50-
# default values
51-
if c_compiler is None:
52-
c_compiler = "icx"
53-
if cxx_compiler is None:
54-
cxx_compiler = "icpx" if is_linux else "icx"
55-
56-
for name, opt_name in (
57-
(c_compiler, "--c-compiler"),
58-
(cxx_compiler, "--cxx-compiler"),
59-
):
60-
path = (
61-
name if os.path.exists(name) else os.path.join(compiler_root, name)
62-
)
63-
if not os.path.exists(path):
64-
raise RuntimeError(f"{opt_name} value {name} not found")
65-
return c_compiler, cxx_compiler
66-
67-
6835
def parse_args():
6936
p = argparse.ArgumentParser(description="Local dpctl build driver")
7037

@@ -157,7 +124,7 @@ def parse_args():
157124
p.add_argument(
158125
"--clean",
159126
action="store_true",
160-
help="Remove build dir before rebuild (default: False)",
127+
help="Remove build dir before rebuild",
161128
)
162129
p.add_argument(
163130
"--skip-editable",

scripts/gen_coverage.py

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -28,70 +28,34 @@
2828
build_extension,
2929
clean_build_dir,
3030
err,
31+
get_output,
3132
install_editable,
3233
make_cmake_args,
34+
resolve_compilers,
3335
run,
3436
warn,
3537
)
3638

3739

38-
def find_bin_llvm(compiler_name):
39-
icx_path = subprocess.check_output(["which", compiler_name])
40-
bin_dir = os.path.dirname(icx_path)
41-
compiler_dir = os.path.join(bin_dir.decode("utf-8"), "compiler")
40+
def find_bin_llvm(compiler):
41+
if os.path.isabs(compiler):
42+
bin_dir = os.path.dirname(compiler)
43+
else:
44+
compiler_path = get_output(["which", compiler])
45+
if not compiler_path:
46+
raise RuntimeError(f"Compiler {compiler} not found in PATH")
47+
bin_dir = os.path.dirname(compiler_path)
48+
compiler_dir = os.path.join(bin_dir, "compiler")
4249
if os.path.exists(compiler_dir):
4350
bin_llvm = compiler_dir
4451
else:
4552
bin_dir = os.path.dirname(bin_dir)
46-
bin_llvm = os.path.join(bin_dir.decode("utf-8"), "bin-llvm")
47-
assert os.path.exists(bin_llvm)
53+
bin_llvm = os.path.join(bin_dir, "bin-llvm")
54+
if not os.path.exists(bin_llvm):
55+
raise RuntimeError(f"--bin-llvm value {bin_llvm} not found")
4856
return bin_llvm
4957

5058

51-
def resolve_compilers(
52-
oneapi: bool,
53-
c_compiler: str,
54-
cxx_compiler: str,
55-
compiler_root: str,
56-
bin_llvm: str = None,
57-
):
58-
is_linux = "linux" in sys.platform
59-
60-
if oneapi or (
61-
c_compiler is None
62-
and cxx_compiler is None
63-
and compiler_root is None
64-
and bin_llvm is None
65-
):
66-
return "icx", ("icpx" if is_linux else "icx"), find_bin_llvm("icx")
67-
68-
if not compiler_root or not os.path.exists(compiler_root):
69-
raise RuntimeError(
70-
"--compiler-root option must be set when using non-default DPC++ "
71-
"layout"
72-
)
73-
74-
# default values
75-
if c_compiler is None:
76-
c_compiler = "icx"
77-
if cxx_compiler is None:
78-
cxx_compiler = "icpx" if is_linux else "icx"
79-
if bin_llvm is None:
80-
bin_llvm = find_bin_llvm(c_compiler)
81-
82-
for name, opt_name in (
83-
(c_compiler, "--c-compiler"),
84-
(cxx_compiler, "--cxx-compiler"),
85-
(bin_llvm, "--bin-llvm"),
86-
):
87-
path = (
88-
name if os.path.exists(name) else os.path.join(compiler_root, name)
89-
)
90-
if not os.path.exists(path):
91-
raise RuntimeError(f"{opt_name} value {name} not found")
92-
return c_compiler, cxx_compiler, bin_llvm
93-
94-
9559
def parse_args():
9660
p = argparse.ArgumentParser(description="Build dpctl and generate coverage")
9761

@@ -177,13 +141,13 @@ def main():
177141
args = parse_args()
178142
setup_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
179143

180-
c_compiler, cxx_compiler, bin_llvm = resolve_compilers(
144+
c_compiler, cxx_compiler = resolve_compilers(
181145
args.oneapi,
182146
args.c_compiler,
183147
args.cxx_compiler,
184148
args.compiler_root,
185-
args.bin_llvm,
186149
)
150+
bin_llvm = find_bin_llvm(c_compiler)
187151

188152
if args.clean:
189153
clean_build_dir(setup_dir)
@@ -246,6 +210,12 @@ def main():
246210
.decode("utf-8")
247211
.strip("\n")
248212
)
213+
214+
cmake_build_dir = get_output(
215+
["find", "_skbuild", "-name", "cmake-build"],
216+
cwd=setup_dir,
217+
)
218+
249219
print(f"[gen_coverage] Found CMake build dir: {cmake_build_dir}")
250220

251221
run(

0 commit comments

Comments
 (0)