Python distutils,如何获取将要使用的编译器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/724664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python distutils, how to get a compiler that is going to be used?
提问by Headcrab
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)?
例如,我可以使用python setup.py build --compiler=msvc
orpython setup.py build --compiler=mingw32
或 just python setup.py build
,在这种情况下将使用默认编译器(例如,bcpp
)。如何在 setup.py 中获取编译器名称(例如,分别为msvc
、mingw32
和bcpp
)?
UPD.: I don't need the default compiler, I need the one that is actuallygoing 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.
UPD。:我不需要默认编译器,我需要的是实际要使用的编译器,它不一定是默认编译器。到目前为止,我还没有找到比解析sys.argv
那里是否有--compiler...
字符串更好的方法。
回答by Jon
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:
这是 Luper Rouch 答案的扩展版本,它对我有用,可以在 Windows 上使用 mingw 和 msvc 获得一个 openmp 扩展来编译。在对 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 } )
回答by Luper Rouch
You can subclass the distutils.command.build_ext.build_ext
command.
您可以对distutils.command.build_ext.build_ext
命令进行子类化。
Once build_ext.finalize_options()
method has been called, the compiler type is stored in self.compiler.compiler_type
as a string (the same as the one passed to the build_ext
's --compiler
option, e.g. 'mingw32', 'gcc', etc...).
一旦build_ext.finalize_options()
方法被调用,编译器类型被存储self.compiler.compiler_type
为一个字符串(与传递给build_ext
's--compiler
选项的那个相同,例如'mingw32'、'gcc' 等...)。
回答by popelkopp
#This should work pretty good
def compilerName():
import re
import distutils.ccompiler
comp = distutils.ccompiler.get_default_compiler()
getnext = False
for a in sys.argv[2:]:
if getnext:
comp = a
getnext = False
continue
#separated by space
if a == '--compiler' or re.search('^-[a-z]*c$', a):
getnext = True
continue
#without space
m = re.search('^--compiler=(.+)', a)
if m == None:
m = re.search('^-[a-z]*c(.+)', a)
if m:
comp = m.group(1)
return comp
print "Using compiler " + '"' + compilerName() + '"'
回答by attwad
import distutils.ccompiler
导入 distutils.ccompiler
compiler_name = distutils.ccompiler.get_default_compiler()
compiler_name = distutils.ccompiler.get_default_compiler()
回答by Cees Timmerman
import sys
sys.argv.extend(['--compiler', 'msvc'])
回答by lygstate
class BuildWithDLLs(build):
# On Windows, we install the git2.dll too.
def _get_dlls(self):
# return a list of of (FQ-in-name, relative-out-name) tuples.
ret = []
bld_ext = self.distribution.get_command_obj('build_ext')
compiler_type = bld_ext.compiler.compiler_type
You can use self.distribution.get_command_obj('build_ext') to get build_ext instance, and then get the compiler_type
可以使用 self.distribution.get_command_obj('build_ext') 获取 build_ext 实例,然后获取 compiler_type