1616
1717import argparse
1818import os
19- import shutil
20- import subprocess
2119import sys
2220
21+ # add scripts dir to Python path so we can import _build_helper
22+ sys .path .insert (0 , os .path .abspath ("scripts" ))
2323
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 )
24+ from _build_helper import ( # noqa: E402
25+ build_extension ,
26+ clean_build_dir ,
27+ err ,
28+ install_editable ,
29+ make_cmake_args ,
30+ resolve_compilers ,
31+ warn ,
32+ )
3533
3634
3735def parse_args ():
@@ -142,81 +140,23 @@ def parse_args():
142140 return p .parse_args ()
143141
144142
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-
185143def main ():
186144 if sys .platform not in ["cygwin" , "win32" , "linux" ]:
187- _err (f"{ sys .platform } not supported" )
145+ err (f"{ sys .platform } not supported" )
188146 args = parse_args ()
189147 setup_dir = os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))
190148 build_dir = os .path .join (setup_dir , args .build_dir )
191149
192- resolve_compilers (args )
150+ c_compiler , cxx_compiler , compiler_root = resolve_compilers (
151+ args .oneapi , args .c_compiler , args .cxx_compiler , args .compiler_root
152+ )
193153
194154 # 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 )
198-
199- env = os .environ .copy ()
200-
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" ]
205-
206- cmake_defs = []
207-
208- # handle architecture conflicts
209- if args .target_hip is not None and not args .target_hip .strip ():
210- _err ("--target-hip requires an explicit architecture" )
155+ if args .clean :
156+ clean_build_dir (build_dir )
211157
212158 if args .no_level_zero and args .target_level_zero :
213- _err ("Cannot combine --no-level-zero and --target-level-zero" )
214-
215- # CUDA/HIP targets
216- if args .target_cuda :
217- cmake_defs .append (f"-DDPCTL_TARGET_CUDA={ args .target_cuda } " )
218- if args .target_hip :
219- cmake_defs .append (f"-DDPCTL_TARGET_HIP={ args .target_hip } " )
159+ err ("Cannot combine --no-level-zero and --target-level-zero" )
220160
221161 # Level Zero state (on unless explicitly disabled)
222162 if args .no_level_zero :
@@ -225,54 +165,48 @@ def main():
225165 level_zero_enabled = True
226166 else :
227167 level_zero_enabled = True
228- cmake_defs .append (
229- "-DDPCTL_ENABLE_L0_PROGRAM_CREATION="
230- f"{ 'ON' if level_zero_enabled else 'OFF' } "
168+
169+ cmake_args = make_cmake_args (
170+ build_type = args .build_type ,
171+ c_compiler = c_compiler ,
172+ cxx_compiler = cxx_compiler ,
173+ level_zero = level_zero_enabled ,
174+ glog = args .glog ,
175+ generator = args .generator ,
176+ verbose = args .verbose ,
177+ other_opts = args .cmake_opts ,
231178 )
232179
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 } " )
180+ # handle architecture conflicts
181+ if args .target_hip is not None and not args .target_hip .strip ():
182+ err ("--target-hip requires an explicit architecture" )
183+
184+ # CUDA/HIP targets
185+ if args .target_cuda :
186+ cmake_args += f" -DDPCTL_TARGET_CUDA={ args .target_cuda } "
187+ if args .target_hip :
188+ cmake_args += f" -DDPCTL_TARGET_HIP={ args .target_hip } "
240189
241- cmake_defs .append (
242- f"-DDPCTL_ENABLE_GLOG:BOOL={ 'ON' if args .glog else 'OFF' } "
190+ cmake_args += (
191+ " -DDPCTL_ENABLE_L0_PROGRAM_CREATION="
192+ f"{ 'ON' if level_zero_enabled else 'OFF' } "
243193 )
244- cmake_defs .append (f"-DCMAKE_BUILD_TYPE={ args .build_type } " )
245- if args .verbose :
246- cmake_defs .append ("-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON" )
247194
248- if args .cmake_opts :
249- cmake_defs .extend (args .cmake_opts .split ())
195+ env = os .environ .copy ()
250196
251- env ["CMAKE_ARGS" ] = " " .join (cmake_defs )
252- print (f"[build_locally] CMake args:\n { ' ' .join (cmake_defs )} " )
197+ # ignore pre-existing CMAKE_ARGS for determinism in build driver
198+ if "CMAKE_ARGS" in env and env ["CMAKE_ARGS" ].strip ():
199+ warn ("Ignoring pre-existing CMAKE_ARGS in environment" )
200+ del env ["CMAKE_ARGS" ]
201+
202+ env ["CMAKE_ARGS" ] = cmake_args
203+ print (f"[build_locally] CMake args:\n { cmake_args } " )
253204
254205 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- )
260206
207+ build_extension (setup_dir , env )
261208 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- )
209+ install_editable (setup_dir , env )
276210 else :
277211 print ("[build_locally] Skipping editable install (--skip-editable)" )
278212
0 commit comments