[py] define HAVE_ERRNO_H in add_local_macros
[aubio.git] / python / lib / moresetuptools.py
index f5eaf39..299d1f9 100644 (file)
@@ -2,92 +2,9 @@
 #
 import sys, os, glob, subprocess
 import distutils, distutils.command.clean, distutils.dir_util
-from .gen_external import generate_external, header, output_path
+from gen_external import generate_external, header, output_path
 
-def get_aubio_version():
-    # read from VERSION
-    this_file_dir = os.path.dirname(os.path.abspath(__file__))
-    version_file = os.path.join(this_file_dir, '..', '..', 'VERSION')
-
-    if not os.path.isfile(version_file):
-        raise SystemError("VERSION file not found.")
-
-    for l in open(version_file).readlines():
-        #exec (l.strip())
-        if l.startswith('AUBIO_MAJOR_VERSION'):
-            AUBIO_MAJOR_VERSION = int(l.split('=')[1])
-        if l.startswith('AUBIO_MINOR_VERSION'):
-            AUBIO_MINOR_VERSION = int(l.split('=')[1])
-        if l.startswith('AUBIO_PATCH_VERSION'):
-            AUBIO_PATCH_VERSION = int(l.split('=')[1])
-        if l.startswith('AUBIO_VERSION_STATUS'):
-            AUBIO_VERSION_STATUS = l.split('=')[1].strip()[1:-1]
-
-    if AUBIO_MAJOR_VERSION is None or AUBIO_MINOR_VERSION is None \
-            or AUBIO_PATCH_VERSION is None:
-        raise SystemError("Failed parsing VERSION file.")
-
-    verstr = '.'.join(map(str, [AUBIO_MAJOR_VERSION,
-                                     AUBIO_MINOR_VERSION,
-                                     AUBIO_PATCH_VERSION]))
-
-    AUBIO_GIT_SHA = get_git_revision_hash()
-    # append sha to version in alpha release
-    # MAJ.MIN.PATCH.{~git<sha> , ''}
-    if '~alpha' in AUBIO_VERSION_STATUS :
-        if AUBIO_GIT_SHA:
-            AUBIO_VERSION_STATUS = '~git'+AUBIO_GIT_SHA
-    if AUBIO_VERSION_STATUS is not None :
-        verstr += AUBIO_VERSION_STATUS
-    return verstr
-
-def get_aubio_pyversion():
-    # convert to version for python according to pep 440
-    # see https://www.python.org/dev/peps/pep-0440/
-    verstr = get_aubio_version()
-    if '~alpha' in verstr or '~git' in verstr:
-        verstr = verstr.split('~')[0] + '+a1'
-        gitsha = get_git_revision_hash(short=True)
-        if gitsha:
-            verstr+='.git.'+gitsha
-    # TODO: add rc, .dev, and .post suffixes, add numbering
-    return verstr
-
-
-
-def get_git_revision_hash( short=True):
-    def which(program):
-    
-        def is_exe(fpath):
-            return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
-
-        fpath, fname = os.path.split(program)
-        if fpath:
-            if is_exe(program):
-                return program
-        else:
-            for path in os.environ["PATH"].split(os.pathsep):
-                path = path.strip('"')
-                exe_file = os.path.join(path, program)
-                if is_exe(exe_file):
-                    return exe_file
-
-        return None
-    if not which('git'):
-        print 'no git found on this system : can\'t get sha'
-        return ""
-
-    import subprocess
-    this_file_dir = os.path.dirname(os.path.abspath(__file__))
-    aubio_dir = os.path.join(this_file_dir, '..', '..')
-    aubio_dir = os.path.abspath(aubio_dir)
-    if not os.path.exists(aubio_dir):
-        raise SystemError("git / root folder not found")
-    gitcmd = ['git','-C',aubio_dir ,'rev-parse']
-    if short:
-      gitcmd.append('--short')
-    gitcmd.append('HEAD')
-    return subprocess.check_output(gitcmd).strip()
+from this_version import get_aubio_version
 
 # inspired from https://gist.github.com/abergmeier/9488990
 def add_packages(packages, ext=None, **kw):
@@ -138,7 +55,7 @@ def add_local_aubio_lib(ext):
     ext.library_dirs += [os.path.join('build', 'src')]
     ext.libraries += ['aubio']
 
-def add_local_aubio_sources(ext, usedouble = False):
+def add_local_aubio_sources(ext):
     """ build aubio inside python module instead of linking against libaubio """
     print("Info: libaubio was not installed or built locally with waf, adding src/")
     aubio_sources = sorted(glob.glob(os.path.join('src', '**.c')))
@@ -146,10 +63,12 @@ def add_local_aubio_sources(ext, usedouble = False):
     ext.sources += aubio_sources
 
 def add_local_macros(ext, usedouble = False):
+    if usedouble:
+        ext.define_macros += [('HAVE_AUBIO_DOUBLE', 1)]
     # 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_C99_VARARGS_MACROS',
+                         'HAVE_ERRNO_H', 'HAVE_C99_VARARGS_MACROS',
                          'HAVE_LIMITS_H', 'HAVE_STDARG_H',
                          'HAVE_MEMCPY_HACKS']:
         ext.define_macros += [(define_macro, 1)]
@@ -157,9 +76,8 @@ def add_local_macros(ext, usedouble = False):
 def add_external_deps(ext, usedouble = False):
     # loof for additional packages
     print("Info: looking for *optional* additional packages")
-    packages = ['libavcodec', 'libavformat', 'libavutil', 'libavresample',
-                'jack',
-                'jack',
+    packages = ['libavcodec', 'libavformat', 'libavutil',
+                'libswresample', 'libavresample',
                 'sndfile',
                 #'fftw3f',
                ]
@@ -171,11 +89,13 @@ def add_external_deps(ext, usedouble = False):
     add_packages(packages, ext=ext)
     if 'avcodec' in ext.libraries \
             and 'avformat' in ext.libraries \
-            and 'avutil' in ext.libraries \
-            and 'avresample' in ext.libraries:
-        ext.define_macros += [('HAVE_LIBAV', 1)]
-    if 'jack' in ext.libraries:
-        ext.define_macros += [('HAVE_JACK', 1)]
+            and 'avutil' in ext.libraries:
+        if 'swresample' in ext.libraries:
+            ext.define_macros += [('HAVE_SWRESAMPLE', 1)]
+        elif 'avresample' in ext.libraries:
+            ext.define_macros += [('HAVE_AVRESAMPLE', 1)]
+        if 'swresample' in ext.libraries or 'avresample' in ext.libraries:
+            ext.define_macros += [('HAVE_LIBAV', 1)]
     if 'sndfile' in ext.libraries:
         ext.define_macros += [('HAVE_SNDFILE', 1)]
     if 'samplerate' in ext.libraries:
@@ -196,8 +116,8 @@ def add_external_deps(ext, usedouble = False):
 
     ext.define_macros += [('HAVE_WAVWRITE', 1)]
     ext.define_macros += [('HAVE_WAVREAD', 1)]
-    # TODO:
-    # add cblas
+
+    # TODO: add cblas
     if 0:
         ext.libraries += ['cblas']
         ext.define_macros += [('HAVE_ATLAS_CBLAS_H', 1)]
@@ -211,6 +131,12 @@ def add_system_aubio(ext):
     else:
         print("Info: using system aubio " + aubio_version + " found in " + ' '.join(ext.library_dirs))
 
+def add_libav_on_win(ext):
+    """ no pkg-config on windows, simply assume these libs are available """
+    ext.libraries += ['avformat', 'avutil', 'avcodec', 'swresample']
+    for define_macro in ['HAVE_LIBAV', 'HAVE_SWRESAMPLE']:
+        ext.define_macros += [(define_macro, 1)]
+
 class CleanGenerated(distutils.command.clean.clean):
     def run(self):
         if os.path.isdir(output_path):
@@ -237,7 +163,6 @@ class build_ext(_build_ext):
 
     def build_extension(self, extension):
         if self.enable_double or 'HAVE_AUBIO_DOUBLE' in os.environ:
-            extension.define_macros += [('HAVE_AUBIO_DOUBLE', 1)]
             enable_double = True
         else:
             enable_double = False
@@ -248,16 +173,21 @@ class build_ext(_build_ext):
             # use local src/aubio.h
             if os.path.isfile(os.path.join('src', 'aubio.h')):
                 add_local_aubio_header(extension)
-            add_local_macros(extension)
+            add_local_macros(extension, usedouble=enable_double)
             # look for a local waf build
             if os.path.isfile(os.path.join('build','src', 'fvec.c.1.o')):
                 add_local_aubio_lib(extension)
             else:
                 # check for external dependencies
                 add_external_deps(extension, usedouble=enable_double)
+                # force adding libav on windows
+                if os.name == 'nt' and ('WITH_LIBAV' in os.environ \
+                        or 'CONDA_PREFIX' in os.environ):
+                    add_libav_on_win(extension)
                 # add libaubio sources and look for optional deps with pkg-config
-                add_local_aubio_sources(extension, usedouble=enable_double)
+                add_local_aubio_sources(extension)
         # generate files python/gen/*.c, python/gen/aubio-generated.h
+        extension.include_dirs += [ output_path ]
         extension.sources += generate_external(header, output_path, overwrite = False,
                 usedouble=enable_double)
         return _build_ext.build_extension(self, extension)