-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.py
152 lines (123 loc) · 5.54 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
140
141
142
143
144
145
146
147
148
149
150
151
152
import io
import os
import re
import subprocess
import sys
import shutil
import platform
from pathlib import Path
from distutils.command.install_data import install_data
from setuptools import find_packages, setup, Extension
from setuptools.command.build_ext import build_ext
from setuptools.command.install_lib import install_lib
from setuptools.command.install_scripts import install_scripts
class CMakeExtension(Extension):
"""An extension to run the cmake build"""
def __init__(self, name, sourcedir=''):
super().__init__(name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
class BuildCMakeExt(build_ext):
"""Builds using cmake instead of the python setuptools implicit build"""
def run(self):
"""Perform build_cmake before doing the 'normal' stuff"""
for ext in self.extensions:
if isinstance(ext, CMakeExtension):
self.build_cmake(ext)
super().run()
def is_in_cibuildwheel(self):
# maybe other env
return ('AUDITWHEEL_PLAT' in os.environ)
def build_cmake(self, ext: Extension):
"""The steps required to build the extension"""
self.announce("Preparing the build environment", level=3)
extpath = os.path.abspath(self.get_ext_fullpath(ext.name))
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
# required for auto-detection of auxiliary "native" libs
if not extdir.endswith(os.path.sep):
extdir += os.path.sep
bin_dir = os.path.abspath(os.path.join(self.build_temp, 'install'))
try:
if os.path.exists(bin_dir):
shutil.rmtree(bin_dir)
except OSError as e:
print("Error: %s - %s." % (e.filename, e.strerror))
cmake_args = []
# cmake_args += [f"-DVERSION_INFO={self.distribution.get_version()}"]
cmake_args += ['-DPython3_ROOT_DIR=' + os.path.dirname(sys.executable)]
cmake_args += ['-DIS_CIBUILDWHEEL=' + ('ON' if self.is_in_cibuildwheel() else "OFF")]
cfg = 'Debug' if self.debug else 'Release'
build_args = ['--config', cfg]
cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
install_args = ['--prefix', bin_dir]
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
self.announce("Configuring cmake project", level=3)
self.spawn(['cmake', '-S' + ext.sourcedir, '-B' + self.build_temp] + cmake_args)
self.announce("Building binaries", level=3)
self.spawn(["cmake", "--build", self.build_temp] + build_args)
self.spawn(["cmake", "--install", self.build_temp] + install_args)
self.announce("Moving built python module", level=3)
self.distribution.bin_dir = bin_dir
pyd_path = [os.path.join(root, _pyd) for root, _, files in
os.walk(bin_dir) for _pyd in files if
os.path.isfile(os.path.join(root, _pyd)) and
os.path.splitext(_pyd)[0].startswith('_kburn') and
os.path.splitext(_pyd)[-1] in [".pyd", ".so"]][0]
shutil.move(pyd_path, extpath)
class InstallCMakeLibs(install_lib):
"""Get the libraries from the parent distribution, use those as the outfiles"""
def run(self):
"""Copy libraries from the bin directory and place them as appropriate"""
self.announce("Moving library files", level=3)
self.skip_build = True
bin_dir = self.distribution.bin_dir
libs = [os.path.join(root, _lib) for root, _, files in
os.walk(bin_dir) for _lib in files if
os.path.isfile(os.path.join(root, _lib)) and
(os.path.splitext(_lib)[-1] in [".dll", ".so", ".dylib"] or
_lib.startswith("lib"))
and not (_lib.startswith("python") or _lib.startswith("_kburn"))]
for lib in libs:
shutil.copy(lib, os.path.join(self.build_dir, os.path.basename(lib)))
data_files = [os.path.join(self.install_dir, os.path.basename(lib)) for lib in libs]
self.distribution.data_files = data_files
self.distribution.run_command("install_data")
super().run()
class InstallCMakeLibsData(install_data):
"""Just a wrapper to get the install data into the egg-info"""
def run(self):
"""Outfiles are the libraries that were built using cmake"""
self.outfiles = self.distribution.data_files
def find_version():
with io.open("src/kburn/CMakeLists.txt", encoding="utf8") as f:
version_file = f.read()
version_major = re.findall(r"K230_FLASH_VERSION_MAJOR (.+?)", version_file)
version_minor = re.findall(r"K230_FLASH_VERSION_MINOR (.+?)", version_file)
version_patch = re.findall(r"K230_FLASH_VERSION_PATCH (.+?)", version_file)
if version_major and version_minor and version_patch:
return version_major[0] + "." + version_minor[0] + "." + version_patch[0]
raise RuntimeError("Unable to find version string.")
setup(
name="k230_flash",
version=find_version(),
author="kendryte747",
author_email="[email protected]",
description="K230 Burning Tool",
long_description="",
packages=['kburn'],
package_dir={'': 'src/python'},
ext_modules=[CMakeExtension("_kburn")],
cmdclass={
'build_ext': BuildCMakeExt,
'install_lib': InstallCMakeLibs,
'install_data': InstallCMakeLibsData,
},
entry_points={
'console_scripts': [
'k230_flash = kburn.k230_flash:main',
],
},
zip_safe=False,
extras_require={},
python_requires=">=3.7",
)