generated from pybind/cmake_example
-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.py
232 lines (198 loc) · 7.92 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import os
import platform
import re
import shutil
import subprocess
import sys
from datetime import datetime
from distutils.command.install_data import install_data
from pathlib import Path
from pprint import pprint
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
class CMakeExtension(Extension):
def __init__(self, name: str, sourcedir: str = "") -> None:
super().__init__(name, sources=[])
self.sourcedir = os.fspath(Path(sourcedir).resolve())
class MyInstallData(install_data):
def run(self):
self.mkpath(self.install_dir)
for f in self.data_files:
print(f)
print(type(self.distribution.data_files))
for f in self.distribution.data_files:
print(f)
class CMakeBuild(build_ext):
def build_extension(self, ext: CMakeExtension) -> None:
ext_fullpath = Path.cwd() / self.get_ext_fullpath(ext.name)
extdir = ext_fullpath.parent.resolve()
install_dir = extdir / "mlir"
cfg = "Release"
cmake_generator = os.environ.get("CMAKE_GENERATOR", "Ninja")
RUN_TESTS = "ON" if check_env("RUN_TESTS") else "OFF"
# make windows happy
PYTHON_EXECUTABLE = str(Path(sys.executable))
if platform.system() == "Windows":
PYTHON_EXECUTABLE = PYTHON_EXECUTABLE.replace("\\", "\\\\")
cmake_args = [
f"-B{build_temp}",
f"-G {cmake_generator}",
f"-DCMAKE_BUILD_TYPE={cfg}", # not used on MSVC, but no harm
f"-DCMAKE_INSTALL_PREFIX={install_dir}",
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}{os.sep}",
f"-DCMAKE_SYSTEM_NAME={platform.system()}",
f"-DPython3_EXECUTABLE={PYTHON_EXECUTABLE}",
# custom
f"-DBUILD_CUDA={BUILD_CUDA}",
f"-DBUILD_AMDGPU={BUILD_AMDGPU}",
f"-DBUILD_OPENMP={BUILD_OPENMP}",
f"-DBUILD_VULKAN={BUILD_VULKAN}",
f"-DCIBW_ARCHS={os.getenv('CIBW_ARCHS')}",
f"-DRUN_TESTS={RUN_TESTS}",
]
cmake_args += []
if "CMAKE_ARGS" in os.environ:
cmake_args += [os.environ["CMAKE_ARGS"]]
build_args = []
if self.compiler.compiler_type != "msvc":
if not cmake_generator or cmake_generator == "Ninja":
try:
import ninja
ninja_executable_path = Path(ninja.BIN_DIR) / "ninja"
cmake_args += [
"-GNinja",
f"-DCMAKE_MAKE_PROGRAM:FILEPATH={ninja_executable_path}",
]
except ImportError:
pass
else:
single_config = any(x in cmake_generator for x in {"NMake", "Ninja"})
contains_arch = any(x in cmake_generator for x in {"ARM", "Win64"})
if not single_config and not contains_arch:
PLAT_TO_CMAKE = {
"win32": "Win32",
"win-amd64": "x64",
"win-arm32": "ARM",
"win-arm64": "ARM64",
}
cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]]
if not single_config:
cmake_args += [
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}"
]
build_args += ["--config", cfg]
if sys.platform.startswith("darwin"):
macosx_deployment_target = os.getenv("MACOSX_DEPLOYMENT_TARGET", "11.6")
cmake_args += [f"-DCMAKE_OSX_DEPLOYMENT_TARGET={macosx_deployment_target}"]
# Cross-compile support for macOS - respect ARCHFLAGS if set
archs = re.findall(r"-arch (\S+)", os.environ.get("ARCHFLAGS", ""))
if archs:
cmake_args += ["-DCMAKE_OSX_ARCHITECTURES={}".format(";".join(archs))]
if "PARALLEL_LEVEL" not in os.environ:
build_args += [f"-j{str(2 * os.cpu_count())}"]
else:
build_args += [f"-j{os.environ.get('PARALLEL_LEVEL')}"]
config_cmake = Path(__file__).parent.resolve() / "config.cmake"
assert config_cmake.exists()
cmake_args.append(f"-C {config_cmake}")
print("ENV", pprint(os.environ), file=sys.stderr)
print("CMAKE_ARGS", cmake_args, file=sys.stderr)
subprocess.run(
["cmake", ext.sourcedir, *cmake_args], cwd=build_temp, check=True
)
if check_env("DEBUG_CI_FAST_BUILD"):
subprocess.run(
["cmake", "--build", ".", "--target", "llvm-tblgen", *build_args],
cwd=build_temp,
check=True,
)
shutil.rmtree(install_dir / "bin", ignore_errors=True)
shutil.copytree(build_temp / "bin", install_dir / "bin")
else:
subprocess.run(
["cmake", "--build", ".", "--target", "install", *build_args],
cwd=build_temp,
check=True,
)
if RUN_TESTS:
env = os.environ.copy()
# PYTHONPATH needs to be set to find build deps like numpy
# https://github.com/llvm/llvm-project/pull/89296
env["MLIR_LIT_PYTHONPATH"] = os.pathsep.join(sys.path)
subprocess.run(
["cmake", "--build", ".", "--target", "check-all", *build_args],
cwd=build_temp,
env=env,
check=False,
)
shutil.rmtree(install_dir / "python_packages", ignore_errors=True)
subprocess.run(
[
"find",
".",
"-exec",
"touch",
"-a",
"-m",
"-t",
"197001010000",
"{}",
";",
],
cwd=install_dir,
check=False,
)
def check_env(build):
return os.environ.get(build, 0) in {"1", "true", "True", "ON", "YES"}
cmake_version_path = Path("llvm-project/cmake/Modules/LLVMVersion.cmake")
if not cmake_version_path.exists():
cmake_version_path = Path("llvm-project/llvm/CMakeLists.txt")
cmake_txt = open(cmake_version_path).read()
llvm_version = []
for v in ["LLVM_VERSION_MAJOR", "LLVM_VERSION_MINOR", "LLVM_VERSION_PATCH"]:
vn = re.findall(rf"set\({v} (\d+)\)", cmake_txt)
assert vn, f"couldn't find {v} in cmake txt"
llvm_version.append(vn[0])
commit_hash = os.environ.get("LLVM_PROJECT_COMMIT", "DEADBEEF")
now = datetime.now()
llvm_datetime = os.environ.get(
"DATETIME", f"{now.year}{now.month:02}{now.day:02}{now.hour:02}"
)
version = f"{llvm_version[0]}.{llvm_version[1]}.{llvm_version[2]}.{llvm_datetime}+"
local_version = []
BUILD_CUDA = check_env("BUILD_CUDA")
if BUILD_CUDA:
local_version += ["cuda"]
BUILD_AMDGPU = check_env("BUILD_AMDGPU")
if BUILD_AMDGPU:
local_version += ["amdgpu"]
BUILD_VULKAN = check_env("BUILD_VULKAN")
if BUILD_VULKAN:
local_version += ["vulkan"]
BUILD_OPENMP = check_env("BUILD_OPENMP")
if BUILD_OPENMP:
local_version += ["openmp"]
if local_version:
version += ".".join(local_version + [commit_hash])
else:
version += commit_hash
if len(sys.argv) > 1 and sys.argv[1] == "--mlir-version":
print(version)
exit()
llvm_url = f"https://github.com/llvm/llvm-project/commit/{commit_hash}"
build_temp = Path.cwd() / "build" / "temp"
if not build_temp.exists():
build_temp.mkdir(parents=True)
setup(
name="mlir",
version=version,
author="Maksim Levental",
author_email="[email protected]",
description=f"MLIR distribution as wheel. Created at {now} build of {llvm_url}",
long_description=f"MLIR distribution as wheel. Created at {now} build of [llvm/llvm-project/{commit_hash}]({llvm_url})",
long_description_content_type="text/markdown",
ext_modules=[CMakeExtension("mlir", sourcedir="llvm-project/llvm")],
cmdclass={"build_ext": CMakeBuild},
zip_safe=False,
download_url=llvm_url
)