|
| 1 | +# Data Parallel Control (dpctl) |
| 2 | +# |
| 3 | +# Copyright 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 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): |
| 66 | + print("+", " ".join(cmd)) |
| 67 | + subprocess.check_call( |
| 68 | + cmd, env=env or os.environ.copy(), cwd=cwd or os.getcwd() |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +def capture_cmd_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 | + |
| 81 | +def err(msg: str, script: str): |
| 82 | + raise RuntimeError(f"[{script}] error: {msg}") |
| 83 | + |
| 84 | + |
| 85 | +def log_cmake_args(cmake_args: list[str], script: str): |
| 86 | + print(f"[{script}] Using CMake args:\n{' '.join(cmake_args)}") |
| 87 | + |
| 88 | + |
| 89 | +def make_cmake_args( |
| 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, |
| 96 | +): |
| 97 | + args = [ |
| 98 | + f"-DCMAKE_C_COMPILER:PATH={c_compiler}" if c_compiler else "", |
| 99 | + f"-DCMAKE_CXX_COMPILER:PATH={cxx_compiler}" if cxx_compiler else "", |
| 100 | + f"-DDPCTL_ENABLE_L0_PROGRAM_CREATION={'ON' if level_zero else 'OFF'}", |
| 101 | + f"-DDPCTL_ENABLE_GLOG:BOOL={'ON' if glog else 'OFF'}", |
| 102 | + ] |
| 103 | + |
| 104 | + if verbose: |
| 105 | + args.append("-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON") |
| 106 | + if other_opts: |
| 107 | + args.extend(other_opts.split()) |
| 108 | + |
| 109 | + return args |
| 110 | + |
| 111 | + |
| 112 | +def build_extension( |
| 113 | + setup_dir: str, |
| 114 | + env: dict[str, str], |
| 115 | + cmake_args: list[str], |
| 116 | + cmake_executable: str = None, |
| 117 | + generator: str = None, |
| 118 | + build_type: str = None, |
| 119 | +): |
| 120 | + cmd = [sys.executable, "setup.py", "build_ext", "--inplace"] |
| 121 | + if cmake_executable: |
| 122 | + cmd.append(f"--cmake-executable={cmake_executable}") |
| 123 | + if generator: |
| 124 | + cmd.append(f"--generator={generator}") |
| 125 | + if build_type: |
| 126 | + cmd.append(f"--build-type={build_type}") |
| 127 | + if cmake_args: |
| 128 | + cmd.append("--") |
| 129 | + cmd += cmake_args |
| 130 | + run( |
| 131 | + cmd, |
| 132 | + env=env, |
| 133 | + cwd=setup_dir, |
| 134 | + ) |
| 135 | + |
| 136 | + |
| 137 | +def install_editable(setup_dir: str, env: dict[str, str]): |
| 138 | + run( |
| 139 | + [ |
| 140 | + sys.executable, |
| 141 | + "-m", |
| 142 | + "pip", |
| 143 | + "install", |
| 144 | + "-e", |
| 145 | + ".", |
| 146 | + "--no-build-isolation", |
| 147 | + ], |
| 148 | + env=env, |
| 149 | + cwd=setup_dir, |
| 150 | + ) |
| 151 | + |
| 152 | + |
| 153 | +def clean_build_dir(setup_dir: str): |
| 154 | + if ( |
| 155 | + not isinstance(setup_dir, str) |
| 156 | + or not setup_dir |
| 157 | + or not os.path.isdir(setup_dir) |
| 158 | + ): |
| 159 | + raise RuntimeError(f"Invalid setup directory provided: '{setup_dir}'") |
| 160 | + target = os.path.join(setup_dir, "_skbuild") |
| 161 | + if os.path.exists(target): |
| 162 | + print(f"Cleaning build directory: {target}") |
| 163 | + try: |
| 164 | + shutil.rmtree(target) |
| 165 | + except Exception as e: |
| 166 | + print(f"Failed to remove build directory: '{target}'") |
| 167 | + raise e |
0 commit comments