-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsetup.py
More file actions
203 lines (152 loc) · 5.82 KB
/
setup.py
File metadata and controls
203 lines (152 loc) · 5.82 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
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
""" Custom build.py to enable building with CMake from Poetry """
import os
import sys
import re
import subprocess
from distutils.version import LooseVersion
from typing import Optional, List
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
if sys.version_info < (3, 6):
print("Python 3.6 or higher is required, please upgrade.")
sys.exit(-1)
REQUIREMENTS = ["numpy"]
BUILD_REQUIREMENTS = ["setuptools", "wheels", "cmake>=3.13", "ninja"]
class CMakeBuildType:
""" CMake build class"""
def __init__(self, build_type: str):
self.build_type = build_type
build_type: str
def __post_init__(self):
if self.build_type.upper() not in [
"DEBUG",
"RELEASE",
"MINSIZEREL",
"RELWITHDEBINFO",
]:
print("ERROR: invalid build type: '{}'".format(self.build_type))
sys.exit(-1)
self.build_type = self.build_type.upper()
def get_cmake_executable():
cmake3_version = get_cmake_version("cmake3")
cmake_version = get_cmake_version("cmake")
if cmake3_version is None:
return "cmake"
if cmake_version is None:
return "cmake3"
# Use newer cmake
if cmake3_version > cmake_version:
return "cmake3"
return "cmake"
def get_cmake_version(cmake_exe: str = "cmake"):
""" Returns the CMake version or None if CMake is not found """
try:
out = subprocess.check_output([cmake_exe, "--version"])
except OSError:
return None
version_string = re.search(r"version\s*([\d.]+)", out.decode()).group(1)
return LooseVersion(version_string)
class CMakeExtension(Extension):
""" Setuptools CMake Extension """
_build_type: Optional[CMakeBuildType]
_minimum_cmake_version: LooseVersion
_cmake_version: LooseVersion
_source_directory: str
_cmake_exe: str
def __init__(
self,
name,
source_directory="",
build_type: CMakeBuildType = None,
minimum_cmake_version: str = "3.13",
cmake_exe: Optional[str] = None,
):
# Set CMake executable
if cmake_exe is not None:
self._cmake_exe = cmake_exe
else:
self._cmake_exe = get_cmake_executable()
print("CMake executable {}".format(self._cmake_exe))
self._update_cmake_version(minimum_cmake_version)
self._set_source_directory(source_directory)
self._build_type = build_type
Extension.__init__(self, name, sources=[])
def _set_source_directory(self, source_directory):
self._source_directory = os.path.abspath(source_directory)
def _update_cmake_version(self, minimum_cmake_version):
""" Checks that minimum cmake version and sets version info """
cmake_version = get_cmake_version(self._cmake_exe)
# Ensure that CMake is found
if cmake_version is None:
print("ERROR: CMake executable not found.")
sys.exit(-1)
# Ensure that the minimum cmake version
if cmake_version < minimum_cmake_version:
print("ERROR: Found CMake is too old")
print(" found={}".format(cmake_version))
print(" required={}".format(minimum_cmake_version))
sys.exit(-1)
self._minimum_cmake_version = LooseVersion(minimum_cmake_version)
self._cmake_version = cmake_version
def get_build_type(self):
""" Get the cmake build type """
return self._build_type
def get_source_directory(self):
""" Get the cmake source directory """
return self._source_directory
class CMakeBuild(build_ext):
""" CMake Extension Builder """
def run(self):
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext: CMakeExtension):
if not isinstance(ext, CMakeExtension):
print("ERROR: CMakeBuild cannot build non CMakeExtension")
sys.exit(-1)
extension_directory = os.path.abspath(
os.path.dirname(self.get_ext_fullpath(ext.name))
)
cmake_args = [
ext.get_source_directory(),
self._get_build_type_flag(ext.get_build_type()),
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={}".format(extension_directory),
"-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON",
f"-DPYTHON_EXECUTABLE={sys.executable}"
]
self._make_build_dir()
self._cmake_configure(ext._cmake_exe, cmake_args)
self._cmake_build(ext._cmake_exe)
def _get_build_type_flag(self, build_type: Optional[CMakeBuildType]):
""" Get CMake build type flag """
if build_type is None:
build_type_str = "Debug" if self.debug else "Release"
build_type = CMakeBuildType(build_type_str)
return "-DCMAKE_BUILD_TYPE={}".format(build_type.build_type)
def _make_build_dir(self):
""" Makes the build directory if it doesn't exist """
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
def _cmake_configure(self, cmake_exe: str, cmake_args: List[str]):
""" Configures the project """
subprocess.check_call([cmake_exe] + cmake_args, cwd=self.build_temp)
def _cmake_build(self, cmake_exe: str, build_args: List[str] = None):
""" Build project """
if build_args is None:
build_args = []
subprocess.check_call(
[cmake_exe, "--build", "."] + build_args, cwd=self.build_temp
)
def _install(self):
pass
setup(
# Meta data
name="pyjosim",
version="2.3",
author="Paul le Roux",
author_email="pleroux0@outlook.com",
description="Python bindings for JoSIM",
# Package data
ext_modules=[CMakeExtension("pyjosim", minimum_cmake_version="3.13")],
cmdclass=dict(build_ext=CMakeBuild),
zip_safe=False,
)