1
+ """
2
+ Python-SoXR setup.py for Cython build
3
+
4
+ Please refer to BUILDING.md for building instructions.
5
+ Do not run setup.py directly for the build process.
6
+ """
7
+
8
+ import logging
9
+ import subprocess
1
10
import sys
2
11
import sysconfig
3
12
4
- from setuptools import setup , Extension
5
-
6
13
from distutils .ccompiler import get_default_compiler
14
+ from setuptools import setup , Extension
15
+ from setuptools .command .sdist import sdist
7
16
8
17
9
18
SYS_LIBSOXR = False
10
19
11
- # python -m build -C=--global -option=--use-system-libsoxr
20
+ # python -m build -C=--build -option=--use-system-libsoxr
12
21
if '--use-system-libsoxr' in sys .argv :
13
22
sys .argv .remove ('--use-system-libsoxr' )
14
23
SYS_LIBSOXR = True
@@ -28,7 +37,37 @@ def include_dirs(self):
28
37
def include_dirs (self , dirs ):
29
38
self ._include = dirs
30
39
31
- src_libsoxr = [
40
+
41
+ def get_git_version (cwd = '' ):
42
+ try :
43
+ result = subprocess .run (
44
+ ['git' , 'describe' , '--tags' , '--always' , '--dirty' ],
45
+ cwd = cwd , capture_output = True , check = True , text = True )
46
+
47
+ ver = result .stdout .strip ()
48
+ return ver
49
+ except Exception as e :
50
+ logging .warning (f'Error retrieving submodule version: { e } ' )
51
+ return 'unknown'
52
+
53
+
54
+ CSOXR_VERSION_C = '''
55
+ #include "csoxr_version.h"
56
+ const char * libsoxr_version() { return "%s"; }
57
+ '''
58
+
59
+
60
+ class SDistBundledCommand (sdist ):
61
+ def run (self ):
62
+ ver = get_git_version ('libsoxr' )
63
+ with open (f'src/soxr/_csoxr_version.c' , 'wt' ) as f :
64
+ f .write (CSOXR_VERSION_C % (ver ))
65
+ logging .info (f'libsoxr version: { ver } ' )
66
+
67
+ super ().run ()
68
+
69
+
70
+ src_static = [
32
71
'libsoxr/src/soxr.c' ,
33
72
'libsoxr/src/data-io.c' ,
34
73
'libsoxr/src/dbesi0.c' ,
@@ -55,11 +94,16 @@ def include_dirs(self, dirs):
55
94
# 'libsoxr/src/cr64s.c',
56
95
# 'libsoxr/src/pffft64s.c',
57
96
# 'libsoxr/src/util64s.c',
97
+
98
+ # Cython wrapper
99
+ 'src/soxr/cysoxr.pyx' ,
100
+ 'src/soxr/_csoxr_version.c' , # csoxr libsoxr_version()
58
101
]
59
102
60
- src = [
103
+ src_dynamic = [
61
104
# Cython wrapper
62
- 'src/soxr/cysoxr.pyx'
105
+ 'src/soxr/cysoxr.pyx' ,
106
+ 'src/soxr/csoxr_version.c' , # libsoxr soxr_version()
63
107
]
64
108
65
109
compile_args = ['-DSOXR_LIB' ]
@@ -76,25 +120,28 @@ def include_dirs(self, dirs):
76
120
extensions = [
77
121
CySoxrExtension (
78
122
"soxr.cysoxr" ,
79
- src_libsoxr + src ,
123
+ src_static ,
80
124
include_dirs = ['libsoxr/src' , 'src/soxr' ],
81
125
language = "c" ,
82
126
extra_compile_args = compile_args )
83
127
]
84
128
85
129
extensions_dynamic = [
86
- CySoxrExtension ('soxr.cysoxr' , src , language = 'c' , libraries = ['soxr' ])
130
+ CySoxrExtension ('soxr.cysoxr' , src_dynamic , language = 'c' , libraries = ['soxr' ])
87
131
]
88
132
89
133
90
134
if __name__ == "__main__" :
91
135
from Cython .Build import cythonize
92
136
93
137
if SYS_LIBSOXR :
138
+ logging .info ('Building Python-SoXR using system libsoxr...' )
94
139
setup (
95
140
ext_modules = cythonize (extensions_dynamic , language_level = '3' ),
96
141
)
97
142
else :
143
+ logging .info ('Building Python-SoXR using bundled libsoxr...' )
98
144
setup (
145
+ cmdclass = {'sdist' : SDistBundledCommand },
99
146
ext_modules = cythonize (extensions , language_level = '3' ),
100
147
)
0 commit comments