Python distutils,如何获取将要使用的编译器? [英] Python distutils, how to get a compiler that is going to be used?
问题描述
例如,我可以使用 python setup.py build --compiler = msvc
或 python setup.py build --compiler = mingw32
或只是 python setup.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
看看是否有一个 - 编译器...
string there。
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的扩展版本的答案,在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,如何获取将要使用的编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!