Python distutils, how to get a compiler that is going to be used?

后端 未结 6 2095
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 10:41

For example, I may use python setup.py build --compiler=msvc or python setup.py build --compiler=mingw32 or just python setup.py build

6条回答
  •  醉话见心
    2020-12-02 11:09

    This is an expanded version of Luper Rouch's answer that worked for me to get an openmp extension to compile using both mingw and msvc on windows. After subclassing build_ext you need to pass it to setup.py in the cmdclass arg. By subclassing build_extensions instead of finalize_options you'll have the actual compiler object to look into, so you can then get more detailed version information. You could eventually set compiler flags on a per-compiler, per-extension basis:

    from distutils.core import setup, Extension
    from distutils.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 } )
    

提交回复
热议问题