Skip to content

Commit c47e1f9

Browse files
scripts/build_backend.py reorg
The logic to build the backend is moved inside a function, allowing to pass argument controlling the build. Currently this is used to specify whether to use L0 support (hardcoded to True), but opens a door to specify custom compiler to use to build SyclInterface library. setup.py no longer uses subprocess to invoke build_backed, but uses importlib to import the function defined there and calls the function with appropriate arguments. scipts/build_backend.py retains the function of the script, calling the build_backend function with default arguments (no L0 support)
1 parent 5846fb6 commit c47e1f9

File tree

2 files changed

+141
-116
lines changed

2 files changed

+141
-116
lines changed

scripts/build_backend.py

Lines changed: 132 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -18,129 +18,148 @@
1818
"""
1919

2020

21-
import glob
22-
import os
23-
import shutil
24-
import subprocess
25-
import sys
26-
27-
IS_WIN = False
28-
IS_LIN = False
29-
30-
if "linux" in sys.platform:
31-
IS_LIN = True
32-
elif sys.platform in ["win32", "cygwin"]:
33-
IS_WIN = True
34-
else:
35-
assert False, sys.platform + " not supported"
36-
37-
ONEAPI_ROOT = os.environ.get("ONEAPI_ROOT")
38-
CODE_COVERAGE = os.environ.get("CODE_COVERAGE")
39-
40-
if IS_LIN:
41-
DPCPP_ROOT = os.path.join(ONEAPI_ROOT, r"compiler/latest/linux")
42-
if IS_WIN:
43-
DPCPP_ROOT = os.path.join(ONEAPI_ROOT, r"compiler\latest\windows")
44-
45-
dpctl_dir = os.getcwd()
46-
build_cmake_dir = os.path.join(dpctl_dir, "build_cmake")
47-
if os.path.exists(build_cmake_dir):
48-
for f in os.listdir(build_cmake_dir):
49-
f_path = os.path.join(build_cmake_dir, f)
50-
if os.path.isdir(f_path):
51-
if (f == "level-zero") and os.path.isdir(
52-
os.path.join(f_path, ".git")
53-
):
54-
# do not delete Git checkout of level zero headers
55-
pass
21+
def build_backend(l0_support=False):
22+
import glob
23+
import os
24+
import shutil
25+
import subprocess
26+
import sys
27+
28+
IS_WIN = False
29+
IS_LIN = False
30+
31+
if "linux" in sys.platform:
32+
IS_LIN = True
33+
elif sys.platform in ["win32", "cygwin"]:
34+
IS_WIN = True
35+
else:
36+
assert False, sys.platform + " not supported"
37+
38+
CODE_COVERAGE = os.environ.get("CODE_COVERAGE")
39+
ONEAPI_ROOT = os.environ.get("ONEAPI_ROOT")
40+
41+
if IS_LIN:
42+
DPCPP_ROOT = os.path.join(ONEAPI_ROOT, r"compiler/latest/linux")
43+
elif IS_WIN:
44+
DPCPP_ROOT = os.path.join(ONEAPI_ROOT, r"compiler\latest\windows")
45+
46+
dpctl_dir = os.getcwd()
47+
build_cmake_dir = os.path.join(dpctl_dir, "build_cmake")
48+
if os.path.exists(build_cmake_dir):
49+
for f in os.listdir(build_cmake_dir):
50+
f_path = os.path.join(build_cmake_dir, f)
51+
if os.path.isdir(f_path):
52+
if (f == "level-zero") and os.path.isdir(
53+
os.path.join(f_path, ".git")
54+
):
55+
# do not delete Git checkout of level zero headers
56+
pass
57+
else:
58+
shutil.rmtree(f_path)
5659
else:
57-
shutil.rmtree(f_path)
58-
else:
59-
os.remove(f_path)
60-
else:
61-
os.mkdir(build_cmake_dir)
62-
os.chdir(build_cmake_dir)
63-
64-
INSTALL_PREFIX = os.path.join(dpctl_dir, "install")
65-
if os.path.exists(INSTALL_PREFIX):
66-
shutil.rmtree(INSTALL_PREFIX)
67-
68-
backends = os.path.join(dpctl_dir, "dpctl-capi")
69-
70-
if IS_LIN:
71-
if CODE_COVERAGE:
72-
cmake_args = [
73-
"cmake",
74-
"-DCMAKE_BUILD_TYPE=Debug",
75-
"-DCMAKE_INSTALL_PREFIX=" + INSTALL_PREFIX,
76-
"-DCMAKE_PREFIX_PATH=" + INSTALL_PREFIX,
77-
"-DDPCPP_INSTALL_DIR=" + DPCPP_ROOT,
78-
"-DCMAKE_C_COMPILER:PATH="
79-
+ os.path.join(DPCPP_ROOT, "bin", "clang"),
80-
"-DCMAKE_CXX_COMPILER:PATH="
81-
+ os.path.join(DPCPP_ROOT, "bin", "dpcpp"),
82-
"-DDPCTL_ENABLE_LO_PROGRAM_CREATION=ON",
83-
"-DDPCTL_BUILD_CAPI_TESTS=ON",
84-
"-DDPCTL_GENERATE_COVERAGE=ON",
85-
"-DDPCTL_COVERAGE_REPORT_OUTPUT_DIR=" + dpctl_dir,
86-
backends,
87-
]
88-
subprocess.check_call(cmake_args, stderr=subprocess.STDOUT, shell=False)
89-
subprocess.check_call(["make", "V=1", "-j", "4"])
90-
subprocess.check_call(["make", "install"])
91-
subprocess.check_call(["make", "lcov-genhtml"])
92-
60+
os.remove(f_path)
9361
else:
62+
os.mkdir(build_cmake_dir)
63+
os.chdir(build_cmake_dir)
64+
65+
INSTALL_PREFIX = os.path.join(dpctl_dir, "install")
66+
if os.path.exists(INSTALL_PREFIX):
67+
shutil.rmtree(INSTALL_PREFIX)
68+
69+
backends = os.path.join(dpctl_dir, "dpctl-capi")
70+
71+
ENABLE_LO_PROGRAM_CREATION = "ON" if l0_support else "OFF"
72+
73+
if IS_LIN:
74+
if CODE_COVERAGE:
75+
cmake_args = [
76+
"cmake",
77+
"-DCMAKE_BUILD_TYPE=Debug",
78+
"-DCMAKE_INSTALL_PREFIX=" + INSTALL_PREFIX,
79+
"-DCMAKE_PREFIX_PATH=" + INSTALL_PREFIX,
80+
"-DDPCPP_INSTALL_DIR=" + DPCPP_ROOT,
81+
"-DCMAKE_C_COMPILER:PATH="
82+
+ os.path.join(DPCPP_ROOT, "bin", "clang"),
83+
"-DCMAKE_CXX_COMPILER:PATH="
84+
+ os.path.join(DPCPP_ROOT, "bin", "dpcpp"),
85+
"-DDPCTL_ENABLE_LO_PROGRAM_CREATION="
86+
+ ENABLE_LO_PROGRAM_CREATION,
87+
"-DDPCTL_BUILD_CAPI_TESTS=ON",
88+
"-DDPCTL_GENERATE_COVERAGE=ON",
89+
"-DDPCTL_COVERAGE_REPORT_OUTPUT_DIR=" + dpctl_dir,
90+
backends,
91+
]
92+
subprocess.check_call(
93+
cmake_args, stderr=subprocess.STDOUT, shell=False
94+
)
95+
subprocess.check_call(["make", "V=1", "-j", "4"])
96+
subprocess.check_call(["make", "install"])
97+
subprocess.check_call(["make", "lcov-genhtml"])
98+
else:
99+
cmake_args = [
100+
"cmake",
101+
"-DCMAKE_BUILD_TYPE=Release",
102+
"-DCMAKE_INSTALL_PREFIX=" + INSTALL_PREFIX,
103+
"-DCMAKE_PREFIX_PATH=" + INSTALL_PREFIX,
104+
"-DDPCPP_INSTALL_DIR=" + DPCPP_ROOT,
105+
"-DCMAKE_C_COMPILER:PATH="
106+
+ os.path.join(DPCPP_ROOT, "bin", "clang"),
107+
"-DCMAKE_CXX_COMPILER:PATH="
108+
+ os.path.join(DPCPP_ROOT, "bin", "dpcpp"),
109+
"-DDPCTL_ENABLE_LO_PROGRAM_CREATION="
110+
+ ENABLE_LO_PROGRAM_CREATION,
111+
backends,
112+
]
113+
subprocess.check_call(
114+
cmake_args, stderr=subprocess.STDOUT, shell=False
115+
)
116+
subprocess.check_call(["make", "V=1", "-j", "4"])
117+
subprocess.check_call(["make", "install"])
118+
119+
os.chdir(dpctl_dir)
120+
for file in glob.glob(
121+
os.path.join(dpctl_dir, "install", "lib", "*.so*")
122+
):
123+
shutil.copy(file, os.path.join(dpctl_dir, "dpctl"))
124+
elif IS_WIN:
94125
cmake_args = [
95126
"cmake",
127+
"-G",
128+
"Ninja",
96129
"-DCMAKE_BUILD_TYPE=Release",
97130
"-DCMAKE_INSTALL_PREFIX=" + INSTALL_PREFIX,
98131
"-DCMAKE_PREFIX_PATH=" + INSTALL_PREFIX,
99132
"-DDPCPP_INSTALL_DIR=" + DPCPP_ROOT,
100133
"-DCMAKE_C_COMPILER:PATH="
101-
+ os.path.join(DPCPP_ROOT, "bin", "clang"),
134+
+ os.path.join(DPCPP_ROOT, "bin", "clang-cl.exe"),
102135
"-DCMAKE_CXX_COMPILER:PATH="
103-
+ os.path.join(DPCPP_ROOT, "bin", "dpcpp"),
104-
"-DDPCTL_ENABLE_LO_PROGRAM_CREATION=ON",
136+
+ os.path.join(DPCPP_ROOT, "bin", "dpcpp.exe"),
137+
"-DDPCTL_ENABLE_LO_PROGRAM_CREATION=" + ENABLE_LO_PROGRAM_CREATION,
105138
backends,
106139
]
107140
subprocess.check_call(cmake_args, stderr=subprocess.STDOUT, shell=False)
108-
subprocess.check_call(["make", "V=1", "-j", "4"])
109-
subprocess.check_call(["make", "install"])
110-
111-
os.chdir(dpctl_dir)
112-
for file in glob.glob(os.path.join(dpctl_dir, "install", "lib", "*.so*")):
113-
shutil.copy(file, os.path.join(dpctl_dir, "dpctl"))
114-
115-
if IS_WIN:
116-
cmake_args = [
117-
"cmake",
118-
"-G",
119-
"Ninja",
120-
"-DCMAKE_BUILD_TYPE=Release",
121-
"-DCMAKE_INSTALL_PREFIX=" + INSTALL_PREFIX,
122-
"-DCMAKE_PREFIX_PATH=" + INSTALL_PREFIX,
123-
"-DDPCPP_INSTALL_DIR=" + DPCPP_ROOT,
124-
"-DCMAKE_C_COMPILER:PATH="
125-
+ os.path.join(DPCPP_ROOT, "bin", "clang-cl.exe"),
126-
"-DCMAKE_CXX_COMPILER:PATH="
127-
+ os.path.join(DPCPP_ROOT, "bin", "dpcpp.exe"),
128-
"-DDPCTL_ENABLE_LO_PROGRAM_CREATION=ON",
129-
backends,
130-
]
131-
subprocess.check_call(cmake_args, stderr=subprocess.STDOUT, shell=False)
132-
subprocess.check_call(["ninja", "-n"])
133-
subprocess.check_call(["ninja", "install"])
134-
135-
os.chdir(dpctl_dir)
136-
for file in glob.glob(os.path.join(dpctl_dir, "install", "lib", "*.lib")):
137-
shutil.copy(file, os.path.join(dpctl_dir, "dpctl"))
138-
139-
for file in glob.glob(os.path.join(dpctl_dir, "install", "bin", "*.dll")):
140-
shutil.copy(file, os.path.join(dpctl_dir, "dpctl"))
141-
142-
include_dir = os.path.join(dpctl_dir, "dpctl", "include")
143-
if os.path.exists(include_dir):
144-
shutil.rmtree(include_dir)
145-
146-
shutil.copytree(os.path.join(dpctl_dir, "dpctl-capi", "include"), include_dir)
141+
subprocess.check_call(["ninja", "-n"])
142+
subprocess.check_call(["ninja", "install"])
143+
144+
os.chdir(dpctl_dir)
145+
for file in glob.glob(
146+
os.path.join(dpctl_dir, "install", "lib", "*.lib")
147+
):
148+
shutil.copy(file, os.path.join(dpctl_dir, "dpctl"))
149+
150+
for file in glob.glob(
151+
os.path.join(dpctl_dir, "install", "bin", "*.dll")
152+
):
153+
shutil.copy(file, os.path.join(dpctl_dir, "dpctl"))
154+
155+
include_dir = os.path.join(dpctl_dir, "dpctl", "include")
156+
if os.path.exists(include_dir):
157+
shutil.rmtree(include_dir)
158+
159+
shutil.copytree(
160+
os.path.join(dpctl_dir, "dpctl-capi", "include"), include_dir
161+
)
162+
163+
164+
if __name__ == "__main__":
165+
build_backend()

setup.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import os
1818
import os.path
19-
import subprocess
2019
import sys
2120

2221
import numpy as np
@@ -103,8 +102,15 @@ def get_suppressed_warning_flags():
103102

104103

105104
def build_backend():
106-
build_script = os.path.join(os.getcwd(), "scripts", "build_backend.py")
107-
subprocess.check_call([sys.executable, build_script])
105+
import os.path
106+
from importlib.util import module_from_spec, spec_from_file_location
107+
108+
spec = spec_from_file_location(
109+
"build_backend", os.path.join("scripts", "build_backend.py")
110+
)
111+
builder_module = module_from_spec(spec)
112+
spec.loader.exec_module(builder_module)
113+
builder_module.build_backend(l0_support=True)
108114

109115

110116
def extensions():

0 commit comments

Comments
 (0)