shithub: aubio

Download patch

ref: a89ed31d00bd62ce7a9d48ca42822f60fa2eca3d
parent: 07867cd07ec04d24b226b6af55ddaeac56be5665
author: Paul Brossier <piem@piem.org>
date: Mon Apr 25 19:33:11 EDT 2016

python/setup.py: add command 'generate' with option '--enable-double'

--- a/python/ext/aubio-types.h
+++ b/python/ext/aubio-types.h
@@ -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
--- a/python/ext/aubiomodule.c
+++ b/python/ext/aubiomodule.c
@@ -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";
--- a/python/lib/gen_external.py
+++ b/python/lib/gen_external.py
@@ -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 @@
 
     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 @@
         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 );
--- /dev/null
+++ b/python/lib/moresetuptools.py
@@ -1,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)
--- a/python/setup.py
+++ b/python/setup.py
@@ -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,6 +13,9 @@
         ([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 = []
@@ -18,24 +22,13 @@
 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 @@
     "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 @@
     platforms = 'any',
     classifiers = classifiers,
     install_requires = ['numpy'],
+    cmdclass = {
+        'clean': CleanGenerated,
+        'generate': GenerateCommand,
+        }
     )