setup.py, python/lib/: use sorted glob.glob to improve reproducibility
[aubio.git] / python / lib / moresetuptools.py
index 3c2e4cb..08f4592 100644 (file)
@@ -21,11 +21,11 @@ def add_packages(packages, ext=None, **kw):
              }
 
     for package in packages:
+        cmd = ['pkg-config', '--libs', '--cflags', package]
         try:
-            cmd = ['pkg-config', '--libs', '--cflags', package]
             tokens = subprocess.check_output(cmd)
-        except subprocess.CalledProcessError:
-            print("{:s} could not be found".format(package))
+        except Exception as e:
+            print("Running \"{:s}\" failed: {:s}".format(' '.join(cmd), repr(e)))
             continue
         tokens = tokens.decode('utf8').split()
         for token in tokens:
@@ -52,28 +52,36 @@ def add_local_aubio_lib(ext):
     ext.library_dirs += [os.path.join('build', 'src')]
     ext.libraries += ['aubio']
 
-def add_local_aubio_sources(ext):
+def add_local_aubio_sources(ext, usedouble = False):
     """ build aubio inside python module instead of linking against libaubio """
     print("Warning: libaubio was not built with waf, adding src/")
     # create an empty header, macros will be passed on the command line
     fake_config_header = os.path.join('python', 'ext', 'config.h')
     distutils.file_util.write_file(fake_config_header, "")
-    aubio_sources = glob.glob(os.path.join('src', '**.c'))
-    aubio_sources += glob.glob(os.path.join('src', '*', '**.c'))
+    aubio_sources = sorted(glob.glob(os.path.join('src', '**.c')))
+    aubio_sources += sorted(glob.glob(os.path.join('src', '*', '**.c')))
     ext.sources += aubio_sources
     # define macros (waf puts them in build/src/config.h)
     for define_macro in ['HAVE_STDLIB_H', 'HAVE_STDIO_H',
                          'HAVE_MATH_H', 'HAVE_STRING_H',
-                         'HAVE_LIMITS_H', 'HAVE_MEMCPY_HACKS']:
+                         'HAVE_C99_VARARGS_MACROS',
+                         'HAVE_LIMITS_H', 'HAVE_STDARG_H',
+                         'HAVE_MEMCPY_HACKS']:
         ext.define_macros += [(define_macro, 1)]
 
     # loof for additional packages
     print("Info: looking for *optional* additional packages")
     packages = ['libavcodec', 'libavformat', 'libavutil', 'libavresample',
                 'jack',
-                'sndfile', 'samplerate',
+                'jack',
+                'sndfile',
                 #'fftw3f',
                ]
+    # samplerate only works with float
+    if usedouble == False:
+        packages += ['samplerate']
+    else:
+        print("Info: not adding libsamplerate in double precision mode")
     add_packages(packages, ext=ext)
     if 'avcodec' in ext.libraries \
             and 'avformat' in ext.libraries \
@@ -92,8 +100,13 @@ def add_local_aubio_sources(ext):
 
     # add accelerate on darwin
     if sys.platform.startswith('darwin'):
-        ext.extra_link_args += ['-framework', 'accelerate']
+        ext.extra_link_args += ['-framework', 'Accelerate']
         ext.define_macros += [('HAVE_ACCELERATE', 1)]
+        ext.define_macros += [('HAVE_SOURCE_APPLE_AUDIO', 1)]
+        ext.define_macros += [('HAVE_SINK_APPLE_AUDIO', 1)]
+
+    if sys.platform.startswith('win'):
+        ext.define_macros += [('HAVE_WIN_HACKS', 1)]
 
     ext.define_macros += [('HAVE_WAVWRITE', 1)]
     ext.define_macros += [('HAVE_WAVREAD', 1)]
@@ -114,22 +127,42 @@ class CleanGenerated(distutils.command.clean.clean):
         distutils.dir_util.remove_tree(output_path)
         distutils.command.clean.clean.run(self)
 
-class GenerateCommand(distutils.cmd.Command):
-    description = 'generate gen/gen-*.c files from ../src/aubio.h'
-    user_options = [
+from distutils.command.build_ext import build_ext as _build_ext
+class build_ext(_build_ext):
+
+    user_options = _build_ext.user_options + [
             # The format is (long option, short option, description).
             ('enable-double', None, 'use HAVE_AUBIO_DOUBLE=1 (default: 0)'),
             ]
 
     def initialize_options(self):
+        _build_ext.initialize_options(self)
         self.enable_double = False
 
     def finalize_options(self):
+        _build_ext.finalize_options(self)
         if self.enable_double:
             self.announce(
                     'will generate code for aubio compiled with HAVE_AUBIO_DOUBLE=1',
                     level=distutils.log.INFO)
 
-    def run(self):
-        self.announce( 'Generating code', level=distutils.log.INFO)
-        generated_object_files = generate_external(header, output_path, usedouble=self.enable_double)
+    def build_extension(self, extension):
+        if self.enable_double:
+            extension.define_macros += [('HAVE_AUBIO_DOUBLE', 1)]
+        if os.path.isfile('src/aubio.h'):
+            # if aubio headers are found in this directory
+            add_local_aubio_header(extension)
+            # was waf used to build the shared lib?
+            if os.path.isdir(os.path.join('build','src')):
+                # link against build/src/libaubio, built with waf
+                add_local_aubio_lib(extension)
+            else:
+                # add libaubio sources and look for optional deps with pkg-config
+                add_local_aubio_sources(extension, usedouble=self.enable_double)
+        else:
+            # look for aubio headers and lib using pkg-config
+            add_system_aubio(extension)
+        # generate files python/gen/*.c, python/gen/aubio-generated.h
+        extension.sources += generate_external(header, output_path, overwrite = False,
+                usedouble=self.enable_double)
+        return _build_ext.build_extension(self, extension)