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

标签 python compiler-construction installation distutils

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

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

最佳答案

这是 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 } )

关于Python distutils,如何获得将要使用的编译器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/724664/

相关文章:

linux - 如何使用 ssh 在 Linux 主机服务器上安装 Elastic Search

python - 在 Linux 上安装和运行 pypy

python - 为什么Python中的字典和列表不能继承 'len'函数

python执行远程程序

python - 用于字段选择的 Int 与 String

c++ - Qt 有资源系统限制吗?

c - 如何在程序中自动插入 pragma

javascript - JS词汇-多行字符串

python-2.7 - 安装位于 Github 上的 Python 包

python - 有人可以向我解释这种 python 行为吗?