import sys from distutils.core import setup, Extension from distutils.command.build_ext import build_ext def read_parameters(argument_name): out_list = [] try: while True: argument_idx = sys.argv.index(argument_name) out_list.extend(sys.argv.pop(argument_idx + 1).split(";")) sys.argv.pop(argument_idx) except ValueError: pass finally: return out_list def read_include_dirs(): # compiler might not handle empty include dirs properly, # so we need to manually remove them if they exist return list(filter(None, read_parameters("--include-dirs"))) def compiler_has_flag(compiler, flag): import tempfile with tempfile.NamedTemporaryFile('w', suffix='.cpp') as f: f.write('int main (int argc, char **argv) { return 0; }') try: compiler.compile([f.name], extra_postargs=[flag]) except setuptools.distutils.errors.CompileError: return False return True class HTBuildExt(build_ext): def build_extensions(self): compiler_type = self.compiler.compiler_type opts = [] if compiler_type == 'unix' and compiler_has_flag(self.compiler, '--std=c++11'): opts.append('--std=c++11') for ext in self.extensions: ext.extra_compile_args = opts build_ext.build_extensions(self) module = Extension('@PY_MODULE_NAME@', sources = read_parameters("--sources"), libraries = read_parameters("--libraries"), library_dirs = read_parameters("--lib-dirs"), include_dirs = read_include_dirs(), runtime_library_dirs = ['@CMAKE_INSTALL_PREFIX@/lib']) setup(name = '@PY_MODULE_NAME@', version = '@PROJECT_VERSION@', description = 'HawkTracer Client', ext_modules = [module], cmdclass = {'build_ext': HTBuildExt}, author = 'Marcin Kolny', author_email='marcin.kolny@gmail.com', url = 'https://www.hawktracer.org')