python/setup.py: add command 'generate' with option '--enable-double'
authorPaul Brossier <piem@piem.org>
Mon, 25 Apr 2016 21:33:11 +0000 (23:33 +0200)
committerPaul Brossier <piem@piem.org>
Mon, 25 Apr 2016 21:33:11 +0000 (23:33 +0200)
python/ext/aubio-types.h
python/ext/aubiomodule.c
python/lib/gen_external.py
python/lib/moresetuptools.py [new file with mode: 0644]
python/setup.py

index b856ba4..edcf175 100644 (file)
@@ -1,6 +1,8 @@
 #include <Python.h>
 #include <structmember.h>
 
+#include "aubio-generated.h"
+
 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
 
 // define numpy unique symbols for aubio
index 2ab15fa..49068eb 100644 (file)
@@ -1,6 +1,5 @@
 #define PY_AUBIO_MODULE_MAIN
 #include "aubio-types.h"
-#include "aubio-generated.h"
 #include "py-musicutils.h"
 
 static char aubio_module_doc[] = "Python module for the aubio library";
index 5110092..e52f0f9 100644 (file)
@@ -1,4 +1,4 @@
-import os
+import os, glob
 
 header = """// this file is generated! do not modify
 #include "aubio-types.h"
@@ -66,8 +66,9 @@ def get_cpp_objects():
 
     return cpp_output, cpp_objects
 
-def generate_external(output_path, usedouble = False):
+def generate_external(output_path, usedouble = False, overwrite = True):
     if not os.path.isdir(output_path): os.mkdir(output_path)
+    elif overwrite == False: return glob.glob(os.path.join(output_path, '*.c'))
     sources_list = []
     cpp_output, cpp_objects = get_cpp_objects()
     lib = {}
@@ -160,10 +161,17 @@ void add_generated_objects ( PyObject *m )
         sources_list.append(output_file)
 
     objlist = "".join(["extern PyTypeObject Py_%sType;\n" % p for p in lib])
-    out = """
-// generated list of objects created with gen_external.py
-#include <Python.h>
+    out = """// generated list of objects created with gen_external.py
 
+#include <Python.h>
+"""
+    if usedouble:
+        out += """
+#ifndef HAVE_AUBIO_DOUBLE
+#define HAVE_AUBIO_DOUBLE 1
+#endif
+"""
+    out += """
 {objlist}
 int generated_objects ( void );
 void add_generated_objects( PyObject *m );
diff --git a/python/lib/moresetuptools.py b/python/lib/moresetuptools.py
new file mode 100644 (file)
index 0000000..aff02f7
--- /dev/null
@@ -0,0 +1,30 @@
+import distutils, os, subprocess
+import setuptools.command.build_py
+import distutils.command.clean
+from distutils.dir_util import remove_tree
+
+class CleanGenerated(distutils.command.clean.clean):
+    def run(self):
+        remove_tree('gen')
+        distutils.command.clean.clean.run(self)
+
+class GenerateCommand(distutils.cmd.Command):
+    description = 'generate gen/gen-*.c files from ../src/aubio.h'
+    user_options = [
+            # The format is (long option, short option, description).
+            ('enable-double', None, 'use HAVE_AUBIO_DOUBLE=1 (default: 0)'),
+            ]
+
+    def initialize_options(self):
+        self.enable_double = False
+
+    def 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)
+        from .gen_external import generate_external
+        generated_object_files = generate_external('gen', usedouble = self.enable_double)
index e4a32f3..6361506 100755 (executable)
@@ -1,6 +1,7 @@
 #! /usr/bin/env python
 
-from setuptools import setup, Extension
+from setuptools import setup, Extension, Command
+from lib.moresetuptools import CleanGenerated, GenerateCommand
 
 import sys
 import os.path
@@ -12,30 +13,22 @@ __version__ = '.'.join \
         ([str(x) for x in [AUBIO_MAJOR_VERSION, AUBIO_MINOR_VERSION, AUBIO_PATCH_VERSION]]) \
         + AUBIO_VERSION_STATUS
 
+# function to generate gen/*.{c,h}
+from lib.gen_external import generate_external
+output_path = 'gen'
 
 include_dirs = []
 library_dirs = []
 define_macros = []
 extra_link_args = []
 
-include_dirs += ['ext']
+include_dirs += [ 'ext' ]
+include_dirs += [ output_path ] # aubio-generated.h
 include_dirs += [ numpy.get_include() ]
 
 if sys.platform.startswith('darwin'):
     extra_link_args += ['-framework','CoreFoundation', '-framework','AudioToolbox']
 
-output_path = 'gen'
-generated_object_files = []
-
-if not os.path.isdir(output_path):
-    from lib.gen_external import generate_external
-    generated_object_files = generate_external(output_path)
-    # define include dirs
-else:
-    import glob
-    generated_object_files = glob.glob(os.path.join(output_path, '*.c'))
-include_dirs += [output_path]
-
 if os.path.isfile('../src/aubio.h'):
     define_macros += [('USE_LOCAL_AUBIO', 1)]
     include_dirs += ['../src'] # aubio.h
@@ -56,8 +49,8 @@ aubio_extension = Extension("aubio._aubio", [
     "ext/py-phasevoc.c",
     "ext/py-source.c",
     "ext/py-sink.c",
-    # generated files
-    ] + generated_object_files,
+    # generate files if they don't exit
+    ] + generate_external(output_path, overwrite = False),
     include_dirs = include_dirs,
     library_dirs = library_dirs,
     extra_link_args = extra_link_args,
@@ -96,4 +89,8 @@ distrib = setup(name='aubio',
     platforms = 'any',
     classifiers = classifiers,
     install_requires = ['numpy'],
+    cmdclass = {
+        'clean': CleanGenerated,
+        'generate': GenerateCommand,
+        }
     )