forked from dominicprice/endplay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_ext.py
More file actions
95 lines (78 loc) · 2.85 KB
/
build_ext.py
File metadata and controls
95 lines (78 loc) · 2.85 KB
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
import os
import pathlib
from setuptools import Extension
from setuptools.command.build_ext import build_ext
class CMakeExtension(Extension):
"""
Stub class to distinguish between default extensions and CMake
extensions (which contain no sources as these are listed in the
CMakeLists.txt file)
"""
def __init__(self, name):
# don't invoke the original build_ext for this special extension
super().__init__(name, sources=[])
class cmakeable_build_ext(build_ext):
"""
build_ext compatible class which detects if the extension it is to
build is a CMakeExtension in which case it delegates building to
the CMake executable.
"""
def run(self):
for ext in self.extensions:
if isinstance(ext, CMakeExtension):
self.build_cmake(ext)
super().run()
def copy_extensions_to_source(self):
# install is handled by cmake
pass
def build_cmake(self, ext):
cwd = pathlib.Path().absolute()
# Create directory structure
build_temp = pathlib.Path(self.build_temp)
if not build_temp.exists():
build_temp.mkdir(parents=True, exist_ok=True)
extdir = pathlib.Path(self.get_ext_fullpath(ext.name))
if not extdir.exists():
extdir.mkdir(parents=True, exist_ok=True)
# Check which architecture we should be building for
import struct
bits = struct.calcsize("P") * 8
# Setup args passed to cmake
config = "Debug" if self.debug else "Release"
cmake_config_args = [
"-DCMAKE_INSTALL_PREFIX=" + str(extdir.parent.absolute()),
"-DCMAKE_BUILD_TYPE=" + config,
"-DSETUPTOOLS_BUILD=1",
]
if os.name == "nt":
if bits == 64:
cmake_config_args.append("-A x64")
elif bits == 32:
cmake_config_args.append("-A Win32")
else:
raise RuntimeError(f"Unknown computer architecture with {bits} bits")
else:
if bits == 32:
cmake_config_args.append("-DCOMPILE_32_BITS=1")
# Disable warning MSB8029 (https://stackoverflow.com/a/60301902/5194459)
os.environ["IgnoreWarnIntDirInTempDetected"] = "true"
os.chdir(str(build_temp))
self.spawn(["cmake", str(cwd)] + cmake_config_args)
if not self.dry_run: # pyright: ignore
cmake_build_args = [
"--build",
".",
"--target",
"install",
"--config",
config,
]
self.spawn(["cmake"] + cmake_build_args)
os.chdir(str(cwd))
def build(setup_kwargs):
setup_kwargs.update(
{
"ext_modules": [CMakeExtension("endplay")],
"cmdclass": {"build_ext": cmakeable_build_ext},
}
)