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
110import sys
211import sysconfig
312
4- from setuptools import setup , Extension
5-
613from distutils .ccompiler import get_default_compiler
14+ from setuptools import setup , Extension
15+ from setuptools .command .sdist import sdist
716
817
918SYS_LIBSOXR = False
1019
11- # python -m build -C=--global -option=--use-system-libsoxr
20+ # python -m build -C=--build -option=--use-system-libsoxr
1221if '--use-system-libsoxr' in sys .argv :
1322 sys .argv .remove ('--use-system-libsoxr' )
1423 SYS_LIBSOXR = True
@@ -28,7 +37,37 @@ def include_dirs(self):
2837 def include_dirs (self , dirs ):
2938 self ._include = dirs
3039
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 = [
3271 'libsoxr/src/soxr.c' ,
3372 'libsoxr/src/data-io.c' ,
3473 'libsoxr/src/dbesi0.c' ,
@@ -55,11 +94,16 @@ def include_dirs(self, dirs):
5594 # 'libsoxr/src/cr64s.c',
5695 # 'libsoxr/src/pffft64s.c',
5796 # 'libsoxr/src/util64s.c',
97+
98+ # Cython wrapper
99+ 'src/soxr/cysoxr.pyx' ,
100+ 'src/soxr/_csoxr_version.c' , # csoxr libsoxr_version()
58101]
59102
60- src = [
103+ src_dynamic = [
61104 # Cython wrapper
62- 'src/soxr/cysoxr.pyx'
105+ 'src/soxr/cysoxr.pyx' ,
106+ 'src/soxr/csoxr_version.c' , # libsoxr soxr_version()
63107]
64108
65109compile_args = ['-DSOXR_LIB' ]
@@ -76,25 +120,28 @@ def include_dirs(self, dirs):
76120extensions = [
77121 CySoxrExtension (
78122 "soxr.cysoxr" ,
79- src_libsoxr + src ,
123+ src_static ,
80124 include_dirs = ['libsoxr/src' , 'src/soxr' ],
81125 language = "c" ,
82126 extra_compile_args = compile_args )
83127]
84128
85129extensions_dynamic = [
86- CySoxrExtension ('soxr.cysoxr' , src , language = 'c' , libraries = ['soxr' ])
130+ CySoxrExtension ('soxr.cysoxr' , src_dynamic , language = 'c' , libraries = ['soxr' ])
87131]
88132
89133
90134if __name__ == "__main__" :
91135 from Cython .Build import cythonize
92136
93137 if SYS_LIBSOXR :
138+ logging .info ('Building Python-SoXR using system libsoxr...' )
94139 setup (
95140 ext_modules = cythonize (extensions_dynamic , language_level = '3' ),
96141 )
97142 else :
143+ logging .info ('Building Python-SoXR using bundled libsoxr...' )
98144 setup (
145+ cmdclass = {'sdist' : SDistBundledCommand },
99146 ext_modules = cythonize (extensions , language_level = '3' ),
100147 )
0 commit comments