-
Notifications
You must be signed in to change notification settings - Fork 28
/
builder.py
182 lines (152 loc) · 6.1 KB
/
builder.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
"""
Adapted from https://github.com/pybind/cmake_example
"""
import fileinput
import os
import platform
import re
import subprocess
import sys
import sysconfig
from distutils.version import LooseVersion
from pathlib import Path
from typing import Any, Dict, List
from setuptools.command.build_ext import build_ext
from setuptools.extension import Extension
class CMakeExtension(Extension):
def __init__(
self,
name: str,
install_prefix: str,
disable_editable: bool = False,
cmake_configure_options: List[str] = (),
source_dir: str = str(Path(".").absolute()),
cmake_build_type: str = "Release",
cmake_component: str = None,
cmake_depends_on: List[str] = (),
) -> None:
"""
Custom setuptools extension that configures a CMake project.
Args:
name: The name of the extension.
install_prefix: The path relative to the site-package directory where the CMake
project is installed (typically the name of the Python package).
disable_editable: Skip this extension in editable mode.
source_dir: The location of the main CMakeLists.txt.
cmake_build_type: The default build type of the CMake project.
cmake_component: The name of component to install. Defaults to all
components.
cmake_depends_on: List of dependency packages containing required CMake projects.
"""
super().__init__(name=name, sources=[])
if not Path(source_dir).is_absolute():
source_dir = str(Path(".").absolute() / source_dir)
if not Path(source_dir).absolute().is_dir():
raise ValueError(f"Directory '{source_dir}' does not exist")
self.install_prefix = install_prefix
self.cmake_build_type = cmake_build_type
self.disable_editable = disable_editable
self.cmake_depends_on = cmake_depends_on
self.source_dir = str(Path(source_dir).absolute())
self.cmake_configure_options = cmake_configure_options
self.cmake_component = cmake_component
class ExtensionBuilder(build_ext):
def run(self) -> None:
self.validate_cmake()
super().run()
def build_extension(self, ext: Extension) -> None:
if isinstance(ext, CMakeExtension):
self.build_cmake_extension(ext)
else:
super().build_extension(ext)
def validate_cmake(self) -> None:
cmake_extensions = [x for x in self.extensions if isinstance(x, CMakeExtension)]
if len(cmake_extensions) > 0:
try:
out = subprocess.check_output(["cmake", "--version"])
except OSError:
raise RuntimeError(
"CMake must be installed to build the following extensions: "
+ ", ".join(e.name for e in cmake_extensions)
)
if platform.system() == "Windows":
cmake_version = LooseVersion(re.search(r"version\s*([\d.]+)", out.decode()).group(1)) # type: ignore
if cmake_version < "3.1.0":
raise RuntimeError("CMake >= 3.1.0 is required on Windows")
def build_cmake_extension(self, ext: CMakeExtension) -> None:
ext_dir = Path(self.get_ext_fullpath(ext.name)).parent.absolute()
cmake_install_prefix = ext_dir / ext.install_prefix
configure_args = [
f"-DCMAKE_BUILD_TYPE={ext.cmake_build_type}",
f"-DCMAKE_INSTALL_PREFIX:PATH={cmake_install_prefix}",
]
# Extend configure arguments with those from the extension
configure_args += ext.cmake_configure_options
# Set build arguments
build_args = ["--config", ext.cmake_build_type]
if platform.system() == "Windows":
if sys.maxsize > 2**32:
configure_args += ["-A", "x64"]
build_args += ["--", "/m"]
else:
build_args += ["--", "-j4"]
"""
env = os.environ.copy()
env["CXXFLAGS"] = '{} -DVERSION_INFO=\\"{}\\"'.format(
env.get("CXXFLAGS", ""), self.distribution.get_version()
)
"""
# Get the absolute path to the build folder
build_folder = str(Path(".").absolute() / f"{self.build_temp}_{ext.name}")
# Make sure the build folder exists
Path(build_folder).mkdir(exist_ok=True, parents=True)
configure_command = [
"cmake",
"-S",
ext.source_dir,
"-B",
build_folder,
] + configure_args
build_command = ["cmake", "--build", build_folder] + build_args
install_command = ["cmake", "--install", build_folder]
if ext.cmake_component is not None:
install_command.extend(["--component", ext.cmake_component])
print(f"$ {' '.join(configure_command)}")
print(f"$ {' '.join(build_command)}")
print(f"$ {' '.join(install_command)}")
# Generate the project
subprocess.check_call(configure_command)
# Build the project
subprocess.check_call(build_command)
# Install the project
subprocess.check_call(install_command)
def copy_extensions_to_source(self):
original_extensions = list(self.extensions)
self.extensions = [
ext
for ext in self.extensions
if not isinstance(ext, CMakeExtension) or not ext.disable_editable
]
super().copy_extensions_to_source()
self.extensions = original_extensions
def build(setup_kwargs: Dict[str, Any]) -> None:
cython_modules = []
cmake_modules = [
CMakeExtension(
name="skdecide/hub/__skdecide_hub_cpp",
source_dir="cpp",
install_prefix="",
cmake_configure_options=[
f"-DPYTHON_EXECUTABLE={Path(sys.executable)}",
f"-DONLY_PYTHON=ON",
],
),
]
ext_modules = cython_modules + cmake_modules
setup_kwargs.update(
{
"ext_modules": ext_modules,
"cmdclass": dict(build_ext=ExtensionBuilder),
"zip_safe": False,
}
)