-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.py
More file actions
106 lines (86 loc) · 3.1 KB
/
setup.py
File metadata and controls
106 lines (86 loc) · 3.1 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
96
97
98
99
100
101
102
103
104
105
106
"""
Compile the python reader only.
Based on https://github.com/pybind/cmake_example/blob/master/setup.py
"""
import os
import subprocess
import sys
from pathlib import Path
from shutil import copytree, move
import ninja
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
class CMakeExtension(Extension):
"""CMake Specific Extension that records source dir"""
def __init__(self, name: str, source_dir: str = "") -> None:
super().__init__(name, sources=[])
self.source_dir = Path(source_dir).resolve()
class CMakeBuild(build_ext):
"""Build class for CMake Extension"""
def get_outputs(self) -> list[str]:
return [*super().get_outputs(), "_sc2_serializer.pyi"]
def build_extension(self, ext: CMakeExtension) -> None:
ext_fullpath = Path.cwd() / self.get_ext_fullpath(ext.name)
extdir = ext_fullpath.parent.resolve()
cmake_args = [
f"-DPYTHON_EXECUTABLE={sys.executable}",
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}{os.sep}",
"-DSC2_TESTS=OFF",
"-DSC2_CONVERTER=OFF",
]
if os.name == "nt":
cmake_args.extend(["-GVisual Studio 17 2022", "-A", "x64"])
else:
cmake_args.extend(
[
f"-DCMAKE_BUILD_TYPE={'Debug' if self.debug else 'Release'}",
f"-DCMAKE_MAKE_PROGRAM:FILEPATH={Path(ninja.BIN_DIR) / 'ninja'}",
"-GNinja",
]
)
build_args = ["--parallel"]
if os.name == "nt":
build_args.extend(["--config", "Release"])
build_temp = Path(self.build_temp) / ext.name
if not build_temp.exists():
build_temp.mkdir(parents=True)
# Copy data structures to include with package
copytree(
ext.source_dir / "include",
extdir / "include" / "sc2_serializer",
dirs_exist_ok=True,
)
subprocess.run(
["cmake", str(ext.source_dir), *cmake_args], cwd=build_temp, check=True
)
subprocess.run(
["cmake", "--build", ".", *build_args], cwd=build_temp, check=True
)
if os.name == "nt": # Clean up silly MSVC directory structure
for lib in (extdir / "Release").iterdir():
if lib.name in {"zlib.dll", ext_fullpath.name}:
move(lib, extdir / lib.name)
else:
lib.unlink()
(extdir / "Release").rmdir()
subprocess.run(
[
"pybind11-stubgen",
"_sc2_serializer",
f"-o={extdir}{os.sep}",
f"--module-path={ext_fullpath}",
],
cwd=build_temp,
check=True,
)
subprocess.run(
["black", f"{extdir}{os.sep}_sc2_serializer.pyi"],
cwd=build_temp,
check=True,
)
if __name__ == "__main__":
setup(
ext_modules=[CMakeExtension("sc2_serializer._sc2_serializer")],
cmdclass={"build_ext": CMakeBuild},
zip_safe=False,
)