From: Paul Brossier Date: Mon, 25 Apr 2016 21:33:11 +0000 (+0200) Subject: python/setup.py: add command 'generate' with option '--enable-double' X-Git-Tag: 0.4.4~300^2~205 X-Git-Url: https://git.aubio.org/?p=aubio.git;a=commitdiff_plain;h=a89ed31d00bd62ce7a9d48ca42822f60fa2eca3d python/setup.py: add command 'generate' with option '--enable-double' --- diff --git a/python/ext/aubio-types.h b/python/ext/aubio-types.h index b856ba4f..edcf1752 100644 --- a/python/ext/aubio-types.h +++ b/python/ext/aubio-types.h @@ -1,6 +1,8 @@ #include #include +#include "aubio-generated.h" + #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION // define numpy unique symbols for aubio diff --git a/python/ext/aubiomodule.c b/python/ext/aubiomodule.c index 2ab15fa6..49068eb3 100644 --- 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"; diff --git a/python/lib/gen_external.py b/python/lib/gen_external.py index 51100922..e52f0f9b 100644 --- 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 @@ 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 + out = """// generated list of objects created with gen_external.py +#include +""" + 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 index 00000000..aff02f74 --- /dev/null +++ b/python/lib/moresetuptools.py @@ -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) diff --git a/python/setup.py b/python/setup.py index e4a32f36..6361506e 100755 --- 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,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, + } )