-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsetup.py
139 lines (105 loc) · 4.24 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
import sys
import platform
import subprocess
import multiprocessing
from pathlib import Path
from typing import List
from setuptools import setup, Extension, find_packages
from setuptools.command.build_ext import build_ext
from setuptools.command.install import install
# Setuptools calls cmake, which takes care of further
# C++ dependency resolution and the extension build.
# Inspired by:
# https://gist.github.com/hovren/5b62175731433c741d07ee6f482e2936
# https://www.benjack.io/2018/02/02/python-cpp-revisited.html
# Thanks a lot, very helpful :)
binary_files: List[Path] = []
class Install(install):
def run(self):
if self.root:
# setup.py bdist_wheel
os.makedirs(self.root, exist_ok=True)
for f in binary_files:
self.copy_file(f, Path(self.root) / f.name)
else:
# setup.py install
pass
# Continue as normal
super().run()
class CMakeExtension(Extension):
def __init__(self, name):
Extension.__init__(self, name, sources=[])
class CMakeBuild(build_ext):
def run(self):
global binary_files
is_win = platform.system() == 'Windows'
try:
_ = subprocess.check_output(['cmake', '--version'])
except OSError:
raise RuntimeError(
"CMake must be installed to build the following extensions: " +
", ".join(e.name for e in self.extensions))
# preparing cmake arguments
cmake_args = [
('-DCMAKE_LIBRARY_OUTPUT_DIRECTORY='
+ os.path.abspath(self.build_temp)),
'-DPYTHON_EXECUTABLE=' + sys.executable,
'-DCMAKE_CXX_STANDARD=17',
]
# determining build type
cfg = 'Debug' if self.debug else 'Release'
self.build_args = ['--config', cfg]
cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
# parallelize compilation, if using make files
if not is_win:
cpu_count = multiprocessing.cpu_count()
self.build_args += ['--', '-j{}'.format(cpu_count)]
# additional flags
env = os.environ.copy()
env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(
env.get('CXXFLAGS', ''),
self.distribution.get_version())
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
# call cmake to configure and build
cmake_dir = os.path.abspath(os.path.dirname(__file__))
subprocess.check_call(['cmake', cmake_dir] + cmake_args,
cwd=self.build_temp,
env=env)
cmake_cmd = ['cmake', '--build', '.'] + self.build_args
subprocess.check_call(cmake_cmd, cwd=self.build_temp)
# Are we installing or building wheel?
is_wheel = 'bdist_wheel' in sys.argv
# Detect binary files to include in wheel
for ext in self.extensions:
build_temp = Path(self.build_temp).resolve()
if platform.system() == 'Windows':
build_temp = build_temp / 'Release'
suff = Path(self.get_ext_filename(ext.name)).suffix
fmts = set([suff, '.dll', '.pyd', '.so'])
matches = [p for p in build_temp.glob('*') if p.suffix in fmts]
dest_dir = Path(self.get_ext_fullpath(ext.name)).resolve().parent
for m in matches:
binary_files.append(m)
if not is_wheel:
self.copy_file(m, dest_dir / m.name) # tmp => lib
setup(name="imviz",
version="0.2.4",
description="Pythonic bindings for imgui/implot",
url="https://github.com/joruof/imviz",
author="Jona Ruof",
author_email="jona.ruof@uni-ulm.de",
license="MIT",
zip_safe=False,
long_description=Path('README.md').read_text('utf-8'),
long_description_content_type="text/markdown",
python_requires=">=3.8",
packages=find_packages(),
ext_modules=[CMakeExtension("cppimviz")],
cmdclass=dict(build_ext=CMakeBuild, install=Install),
include_package_data=True,
install_requires=[
"numpy", "Pillow>=9.0.1", "objtoolbox>=0.0.9", "minireload>=0.0.5"
]
)