Python distutils, how to get a compiler that is going to be used?(Python distutils,如何获得将要使用的编译器?)
问题描述
例如,我可以使用 python setup.py build --compiler=msvc
或 python setup.py build --compiler=mingw32
或只是 pythonsetup.py build
,在这种情况下,将使用默认编译器(例如,bcpp
).如何在 setup.py 中获取编译器名称(例如分别为 msvc
、mingw32
和 bcpp
)?
For example, I may use python setup.py build --compiler=msvc
or python setup.py build --compiler=mingw32
or just python setup.py build
, in which case the default compiler (say, bcpp
) will be used. How can I get the compiler name inside my setup.py (e. g. msvc
, mingw32
and bcpp
, respectively)?
UPD.:我不需要默认编译器,我需要的是实际上将要使用的编译器,不一定是默认编译器.到目前为止,我还没有找到比解析 sys.argv
以查看是否有 --compiler...
字符串更好的方法.
UPD.: I don't need the default compiler, I need the one that is actually going to be used, which is not necessarily the default one. So far I haven't found a better way than to parse sys.argv
to see if there's a --compiler...
string there.
推荐答案
这是 Luper Rouch 答案的扩展版本,它帮助我获得了一个 openmp 扩展,以便在 Windows 上同时使用 mingw 和 msvc 进行编译.在子类化 build_ext 之后,您需要将其传递给 cmdclass arg 中的 setup.py.通过继承 build_extensions 而不是 finalize_options,您将拥有要查看的实际编译器对象,因此您可以获得更详细的版本信息.您最终可以在每个编译器、每个扩展的基础上设置编译器标志:
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 } )
这篇关于Python distutils,如何获得将要使用的编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python distutils,如何获得将要使用的编译器?
基础教程推荐
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01