Skip to content

Instantly share code, notes, and snippets.

@mindw
Last active December 31, 2015 05:38
Show Gist options
  • Select an option

  • Save mindw/7941801 to your computer and use it in GitHub Desktop.

Select an option

Save mindw/7941801 to your computer and use it in GitHub Desktop.
Python distutils example for adding flags per the compiler that is **going** to be used (not the default).
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
copt = {
'msvc': ['/openmp', '/Ox', '/fp:fast','/favor:INTEL64','/Og'],
'mingw32' : ['-fopenmp','-O3','-ffast-math','-march=native']
}
lopt = {
'mingw32' : ['-fopenmp']
}
class build_ext_subclass(build_ext):
def build_extensions(self):
c = self.compiler.compiler_type
if copt.has_key(c):
for e in self.extensions:
e.extra_compile_args += copt[c]
if lopt.has_key(c):
for e in self.extensions:
e.extra_link_args += lopt[c]
build_ext.build_extensions(self)
mod = Extension(
'_wripaca',
sources=[
'../wripaca_wrap.c',
'../../src/wripaca.c'
],
include_dirs=['../../include']
)
setup(
name = 'wripaca',
ext_modules = [mod],
py_modules = ["wripaca"],
cmdclass = {'build_ext': build_ext_subclass}
)
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
from distutils.ccompiler import CCompiler
from distutils.unixccompiler import UnixCCompiler
from distutils.msvccompiler import MSVCCompiler
DEBUG = False
compiler_opts = {
CCompiler: {},
MSVCCompiler: {
'extra_compile_args': ['/EHsc'],
},
}
if DEBUG:
compiler_opts = {
CCompiler: {'define_macros': [('DEBUG_chardet', '1')]},
MSVCCompiler: {
'extra_compile_args': ['/EHsc', '/Z7'],
'extra_link_args': ['/DEBUG'],
},
UnixCCompiler: {
'extra_compile_args': ['-g'],
'extra_link_args': ['-g'],
}
}
class build_ext_subclass(build_ext):
def build_extensions(self):
c = self.compiler
opts = [v for k, v in compiler_opts.items() if isinstance(c, k)]
for e in self.extensions:
for o in opts:
for attrib, value in o.items():
getattr(e, attrib).extend(value)
build_ext.build_extensions(self)
mod = Extension(
'_wripaca',
sources=[
'../wripaca_wrap.c',
'../../src/wripaca.c'
],
include_dirs=['../../include']
)
setup(
name = 'wripaca',
ext_modules = [mod],
py_modules = ["wripaca"],
cmdclass = {'build_ext': build_ext_subclass}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment