-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathsetup.py
More file actions
329 lines (276 loc) · 12.2 KB
/
Copy pathsetup.py
File metadata and controls
329 lines (276 loc) · 12.2 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#from distutils.core import setup, Extension
from setuptools import setup, Extension,find_packages
from setuptools.command.build_ext import build_ext
import io
import glob
import numpy
import sys
import os
import os.path
import re
import pathlib
import platform
import shutil
import subprocess
here = pathlib.Path(__file__).parent.resolve()
ROOT = here
ROOT=""
BUILD_ROOT = here / "build" / "pythonwrapper"
PYTHON_MOD = os.path.join(ROOT,"cmsisdsp")
version_path = os.path.join(PYTHON_MOD,"version.py")
__version__ = re.search(
r'__version__\s*=\s*[\'"]([^\'"]*)[\'"]', # It excludes inline comment too
io.open(version_path, encoding='utf_8_sig').read()
).group(1)
includes = [os.path.join(ROOT,"Include"),os.path.join(ROOT,"PrivateInclude"),os.path.join("PythonWrapper","cmsisdsp_pkg","src")]
linkargs=[]
machine = platform.machine().lower()
ARM = False
if machine == "aarch64" or machine == "arm64":
ARM = True
if sys.platform == 'win32':
cflags = ["-DWIN",
"-DCMSISDSP",
"-DUNALIGNED_SUPPORT_DISABLE"]
elif sys.platform == 'darwin':
if ARM: # aarch64
os.environ["ARCHFLAGS"] = "-arch arm64"
linkargs = ["-arch", "arm64"]
cflags = ["-DARM_MATH_NEON",
"-Wno-attributes",
"-Wno-unused-function",
"-Wno-unused-variable",
"-Wno-implicit-function-declaration",
"-DCMSISDSP",
"-arch", "arm64",
"-D__GNUC_PYTHON__"]
else: # x86
cflags = ["-Wno-attributes",
"-Wno-unused-function",
"-Wno-unused-variable",
"-Wno-implicit-function-declaration",
"-DCMSISDSP",
"-D__GNUC_PYTHON__"]
else: # linux
if ARM: # aarch64
cflags = ["-DARM_MATH_NEON",
"-Wno-attributes",
"-Wno-unused-function",
"-Wno-unused-variable",
"-Wno-implicit-function-declaration",
"-DCMSISDSP",
"-D__GNUC_PYTHON__"]
else: # x86
cflags = ["-Wno-attributes",
"-Wno-unused-function",
"-Wno-unused-variable",
"-Wno-implicit-function-declaration",
"-DCMSISDSP",
"-D__GNUC_PYTHON__"]
linkargs = ["-lmvec", "-lm"]
# Add dependencies
transformMod = [] # transform + common + basic + complexf + fastmath + matrix + statistics
statisticsMod = [] # statistics + common + fastmath + basic
interpolationMod = [] # interpolation + common
filteringMod = [] # filtering + common + support + fastmath + basic
controllerMod = [] # controller + common
matrixMod = [] # matrix + basic
supportMod = [] # support
complexfMod = [] # complexf + fastmath + common + basic
basicMod = [] # basic
quaternionMod = [] # quaternion
fastmathMod = [] # basic + fastmath + common
distanceMod = [] # distance + common + basic + statistics + fastmath
bayesMod = [] # bayes + fastmath + common + statistics + basic
svmMod = [] # svm + fastmath + common + basic
windowMod = [] # window
filteringMod.append(os.path.join("PythonWrapper","cmsisdsp_pkg","src","cmsisdsp_filtering.c"))
matrixMod.append(os.path.join("PythonWrapper","cmsisdsp_pkg","src","cmsisdsp_matrix.c"))
supportMod.append(os.path.join("PythonWrapper","cmsisdsp_pkg","src","cmsisdsp_support.c"))
statisticsMod.append(os.path.join("PythonWrapper","cmsisdsp_pkg","src","cmsisdsp_statistics.c"))
complexfMod.append(os.path.join("PythonWrapper","cmsisdsp_pkg","src","cmsisdsp_complexf.c"))
basicMod.append(os.path.join("PythonWrapper","cmsisdsp_pkg","src","cmsisdsp_basic.c"))
controllerMod.append(os.path.join("PythonWrapper","cmsisdsp_pkg","src","cmsisdsp_controller.c"))
transformMod.append(os.path.join("PythonWrapper","cmsisdsp_pkg","src","cmsisdsp_transform.c"))
interpolationMod.append(os.path.join("PythonWrapper","cmsisdsp_pkg","src","cmsisdsp_interpolation.c"))
quaternionMod.append(os.path.join("PythonWrapper","cmsisdsp_pkg","src","cmsisdsp_quaternion.c"))
fastmathMod.append(os.path.join("PythonWrapper","cmsisdsp_pkg","src","cmsisdsp_fastmath.c"))
distanceMod.append(os.path.join("PythonWrapper","cmsisdsp_pkg","src","cmsisdsp_distance.c"))
bayesMod.append(os.path.join("PythonWrapper","cmsisdsp_pkg","src","cmsisdsp_bayes.c"))
svmMod.append(os.path.join("PythonWrapper","cmsisdsp_pkg","src","cmsisdsp_svm.c"))
windowMod.append(os.path.join("PythonWrapper","cmsisdsp_pkg","src","cmsisdsp_window.c"))
missing=set([
])
def notf16(number):
if re.search(r'f16',number):
return(False)
if re.search(r'F16',number):
return(False)
return(True)
def isnotmissing(src):
name=os.path.splitext(os.path.basename(src))[0]
return(not (name in missing))
# If there are too many files, the linker command is failing on Windows.
# So f16 functions are removed since they are not currently available in the wrapper.
# A next version will have to structure this wrapper more cleanly so that the
# build can work even with more functions
filtering = list(filter(isnotmissing,list(filter(notf16, filteringMod))))
matrix = list(filter(isnotmissing,list(filter(notf16, matrixMod))))
support = list(filter(isnotmissing,list(filter(notf16, supportMod))))
statistics = list(filter(isnotmissing,list(filter(notf16, statisticsMod))))
complexf = list(filter(isnotmissing,list(filter(notf16, complexfMod))))
basic = list(filter(isnotmissing,list(filter(notf16, basicMod))))
controller = list(filter(isnotmissing,list(filter(notf16, controllerMod))))
transform = list(filter(isnotmissing,list(filter(notf16, transformMod))))
interpolation = list(filter(isnotmissing,list(filter(notf16, interpolationMod))))
quaternion = list(filter(isnotmissing,list(filter(notf16, quaternionMod))))
fastmath = list(filter(isnotmissing,list(filter(notf16, fastmathMod))))
distance = list(filter(isnotmissing,list(filter(notf16, distanceMod))))
bayes = list(filter(isnotmissing,list(filter(notf16, bayesMod))))
svm = list(filter(isnotmissing,list(filter(notf16, svmMod))))
window = list(filter(isnotmissing,list(filter(notf16, windowMod))))
#for l in filtering:
# print(os.path.basename(l))
#quit()
def cmsisdsp_library_path():
if sys.platform == 'win32':
return(BUILD_ROOT / "bin_dsp" / "Release" / "CMSISDSP.lib")
return(BUILD_ROOT / "bin_dsp" / "libCMSISDSP.a")
def built_cmsisdsp_library_candidates():
if sys.platform == 'win32':
return([
BUILD_ROOT / "bin_dsp" / "Release" / "CMSISDSP.lib",
BUILD_ROOT / "bin_dsp" / "CMSISDSP.lib",
])
return([BUILD_ROOT / "bin_dsp" / "libCMSISDSP.a"])
class BuildCMSISDSPExtensions(build_ext):
def run(self):
self.build_cmsisdsp_library()
super().run()
def build_cmsisdsp_library(self):
library_path = cmsisdsp_library_path()
candidates = built_cmsisdsp_library_candidates()
existing = next((path for path in candidates if path.exists()), None)
if existing is not None:
return
BUILD_ROOT.mkdir(parents=True, exist_ok=True)
configure = [
"cmake",
"-S", str(here / "PythonWrapper"),
"-B", str(BUILD_ROOT),
f"-DCMSISDSP={here}",
"-DHOST=YES",
"-DWRAPPER=YES",
"-DLOOPUNROLL=ON",
"-DCMAKE_BUILD_TYPE=Release",
]
if sys.platform != 'win32':
configure.append("-DCMAKE_POSITION_INDEPENDENT_CODE=YES")
configure.append("-DCMAKE_C_FLAGS_RELEASE=-std=c11 -O3 -ffast-math -DNDEBUG -Wall -Wextra")
configure.append("-DCMAKE_CXX_FLAGS_RELEASE=-std=c++11 -O3 -ffast-math -DNDEBUG -Wall -Wextra -Wno-unused-parameter")
if sys.platform == 'darwin' and ARM:
configure.extend([
"-DNEON=YES",
"-DCMAKE_OSX_ARCHITECTURES=arm64",
"-DCMAKE_OSX_DEPLOYMENT_TARGET=10.9"
])
if sys.platform == 'linux' and ARM:
configure.extend(["-DNEON=YES"])
if shutil.which("ninja"):
configure.extend(["-G", "Ninja"])
subprocess.run(configure, check=True)
subprocess.run([
"cmake",
"--build", str(BUILD_ROOT),
"--config", "Release",
], check=True)
existing = next((path for path in candidates if path.exists()), None)
if existing is None:
raise RuntimeError(f"CMSIS-DSP wrapper library was not generated in any expected location under {BUILD_ROOT / 'bin_dsp'}")
if existing != library_path:
library_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copyfile(existing, library_path)
def mkModule(name,srcs,funcDir):
localinc = os.path.join(ROOT,"Source",funcDir)
return(Extension(name,
sources = (srcs
)
,
include_dirs = [localinc] + includes + [numpy.get_include()],
extra_compile_args = cflags,
extra_link_args = linkargs,
libraries=[],
extra_objects=[str(cmsisdsp_library_path())]
))
flagsForCommonWithoutFFT=[]
moduleFiltering = mkModule('cmsisdsp_filtering',filtering,"FilteringFunctions")
moduleMatrix = mkModule('cmsisdsp_matrix',matrix,"MatrixFunctions")
moduleSupport = mkModule('cmsisdsp_support',support,"SupportFunctions")
moduleStatistics = mkModule('cmsisdsp_statistics',statistics,"StatisticsFunctions")
moduleComplexf= mkModule('cmsisdsp_complexf',complexf,"ComplexMathFunctions")
moduleBasic = mkModule('cmsisdsp_basic',basic,"BasicMathFunctions")
moduleController = mkModule('cmsisdsp_controller',controller,"ControllerFunctions")
moduleTransform = mkModule('cmsisdsp_transform',transform,"TransformFunctions")
moduleInterpolation = mkModule('cmsisdsp_interpolation',interpolation,"InterpolationFunctions")
moduleQuaternion = mkModule('cmsisdsp_quaternion',quaternion,"QuaternionMathFunctions")
moduleFastmath = mkModule('cmsisdsp_fastmath',fastmath,"FastMathFunctions")
moduleDistance = mkModule('cmsisdsp_distance',distance,"DistanceFunctions")
moduleBayes = mkModule('cmsisdsp_bayes',bayes,"BayesFunctions")
moduleSVM = mkModule('cmsisdsp_svm',svm,"SVMFunctions")
moduleWindow = mkModule('cmsisdsp_window',window,"WindowFunctions")
def build():
if sys.version_info.major < 3:
print('setup.py: Error: This package only supports Python 3.', file=sys.stderr)
sys.exit(1)
setup (name = 'cmsisdsp',
version = __version__,
packages=["cmsisdsp"],
description = 'CMSIS-DSP Python API',
long_description=open("PythonWrapper_README.md").read(),
long_description_content_type='text/markdown',
ext_modules = [moduleFiltering ,
moduleMatrix,
moduleSupport,
moduleStatistics,
moduleComplexf,
moduleBasic,
moduleController,
moduleTransform,
moduleInterpolation,
moduleQuaternion,
moduleFastmath,
moduleDistance,
moduleBayes,
moduleSVM,
moduleWindow
],
include_package_data=True,
author = 'Copyright (C) 2010-2026 ARM Limited or its affiliates. All rights reserved.',
author_email = 'christophe.favergeon@arm.com',
url="https://github.com/ARM-software/CMSIS-DSP",
python_requires='>=3.9',
license="Apache-2.0",
platforms=['any'],
classifiers=[
"Programming Language :: Python :: 3",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: C",
"Operating System :: OS Independent",
"Development Status :: 5 - Production/Stable",
"Topic :: Software Development :: Embedded Systems",
"Topic :: Scientific/Engineering :: Mathematics",
"Environment :: Console",
"Intended Audience :: Developers",
],
keywords=['development','dsp','cmsis','cmsis-dsp','Arm','signal processing','maths','ml','cortex-m','cortex-a'],
install_requires=[
'numpy>=1.23.5',
],
project_urls={ # Optional
'Bug Reports': 'https://github.com/ARM-software/CMSIS-DSP/issues',
'Source': 'https://github.com/ARM-software/CMSIS-DSP',
}
,
cmdclass={'build_ext': BuildCMSISDSPExtensions}
)
build()