Python distutils,如何获得将要使用的编译器?

Hea*_*rab 27 python compiler-construction installation distutils

例如,我可以使用python setup.py build --compiler=msvc或者python setup.py build --compiler=mingw32仅使用python setup.py build,在这种情况下将使用默认编译器(例如bcpp).如何在setup.py中获取编译器名称(例如msvc,mingw32bcpp,分别)?

UPD.:我不需要默认的编译器,我需要实际使用的那个,这不一定是默认的.到目前为止,我还没有找到比解析更好的方法sys.argv来查看那里是否有--compiler...字符串.

Jon*_*Jon 29

这是Luper Rouch的答案的扩展版本,这对我来说是一个openmp扩展,可​​以在windows上使用mingw和msvc进行编译.在继承build_ext之后,您需要将它传递给cmdclass arg中的setup.py. 通过继承build_extensions而不是finalize_options,您将拥有要查看的实际编译器对象,以便您可以获得更详细的版本信息.您最终可以在每个编译器的每个扩展基础上设置编译器标志:

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 } )
Run Code Online (Sandbox Code Playgroud)


Lup*_*uch 7

您可以继承该distutils.command.build_ext.build_ext命令.

一旦build_ext.finalize_options()调用了方法,编译器类型就会self.compiler.compiler_type以字符串形式存储(与传递给build_ext's --compiler选项的字符串相同,例如'mingw32','gcc'等等).


att*_*wad -1

导入 distutils.ccompiler

编译器名称 = distutils.ccompiler.get_default_compiler()

  • 这不一定是 distutils 将使用的编译器! (5认同)