forked from makslevental/mmlir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
121 lines (99 loc) · 4.19 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
import glob
import os
import platform
import re
import shutil
import subprocess
import sys
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())
def get_cross_cmake_args():
cmake_args = {}
CIBW_ARCHS = os.environ.get("CIBW_ARCHS")
if CIBW_ARCHS in {"arm64", "aarch64", "ARM64"}:
ARCH = cmake_args["LLVM_TARGETS_TO_BUILD"] = "AArch64"
elif CIBW_ARCHS in {"x86_64", "AMD64"}:
ARCH = cmake_args["LLVM_TARGETS_TO_BUILD"] = "X86"
else:
raise ValueError(f"unknown CIBW_ARCHS={CIBW_ARCHS}")
if CIBW_ARCHS != platform.machine():
cmake_args["CMAKE_SYSTEM_NAME"] = platform.system()
if platform.system() == "Darwin":
if ARCH == "AArch64":
cmake_args["CMAKE_OSX_ARCHITECTURES"] = "arm64"
elif ARCH == "X86":
cmake_args["CMAKE_OSX_ARCHITECTURES"] = "x86_64"
return cmake_args
class CMakeBuild(build_ext):
def build_extension(self, ext: CMakeExtension) -> None:
ext_fullpath = Path.cwd() / self.get_ext_fullpath(ext.name)
cfg = "Release"
cmake_generator = os.environ.get("CMAKE_GENERATOR", "Ninja")
MLIR_INSTALL_ABS_PATH = Path(os.getenv("MLIR_DIR", "mlir"))
cmake_args = [
f"-G {cmake_generator}",
"-DLLVM_ENABLE_WARNINGS=OFF",
f"-DCMAKE_PREFIX_PATH={MLIR_INSTALL_ABS_PATH}",
f"-DCMAKE_INSTALL_PREFIX=install",
f"-DPython3_EXECUTABLE={sys.executable}",
# Disables generation of "version soname" (i.e. libFoo.so.<version>), which
# causes pure duplication of various shlibs for Python wheels.
"-DCMAKE_PLATFORM_NO_VERSIONED_SONAME=ON",
f"-DCMAKE_BUILD_TYPE={cfg}", # not used on MSVC, but no harm
]
cmake_args_dict = get_cross_cmake_args()
cmake_args += [f"-D{k}={v}" for k, v in cmake_args_dict.items()]
if "CMAKE_ARGS" in os.environ:
cmake_args += [item for item in os.environ["CMAKE_ARGS"].split(" ") if item]
build_args = []
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')}"]
print("ENV", pprint(os.environ), file=sys.stderr)
print("CMAKE_ARGS", cmake_args, file=sys.stderr)
build_temp = Path(self.build_temp)
if not build_temp.exists():
build_temp.mkdir(parents=True)
subprocess.run(
["cmake", ext.sourcedir, *cmake_args], cwd=build_temp, check=True
)
subprocess.run(
["cmake", "--build", ".", "--target", "install", *build_args],
cwd=build_temp,
check=True,
)
extdir = ext_fullpath.parent.resolve()
shutil.copytree(
Path(self.build_temp) / "install" / "python" / "mmlir",
extdir / "mmlir",
dirs_exist_ok=True,
)
for td in sorted(glob.glob(str(extdir / "mmlir" / "**" / "*.td"))):
os.remove(td)
setup(
author="Maksim Levental",
name="mmlir",
include_package_data=True,
author_email="[email protected]",
description=f"A minimal (really) out-of-tree MLIR example",
ext_modules=[CMakeExtension("mmlir", sourcedir=".")],
cmdclass={"build_ext": CMakeBuild},
zip_safe=False,
python_requires=">=3.8",
packages=["mmlir.dialects"],
package_dir={"mmlir.dialects": str(Path("python") / "mmlir" / "dialects")},
)