34

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.: 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.

4

6 回答 6

34

这是 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 } )
于 2011-03-04T10:49:56.167 回答
8

您可以对distutils.command.build_ext.build_ext命令进行子类化。

一旦build_ext.finalize_options()方法被调用,编译器类型被存储self.compiler.compiler_type为一个字符串(与传递给build_ext'--compiler选项的那个相同,例如'mingw32'、'gcc'等)。

于 2009-11-04T00:37:28.550 回答
2
#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() + '"'
于 2010-01-04T23:16:04.480 回答
0
import sys
sys.argv.extend(['--compiler', 'msvc'])
于 2011-11-17T14:17:00.913 回答
0
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

您可以使用 self.distribution.get_command_obj('build_ext') 获取 build_ext 实例,然后获取 compiler_type

于 2012-04-07T04:22:45.107 回答
-1

导入 distutils.ccompiler

compiler_name = distutils.ccompiler.get_default_compiler()

于 2009-04-07T09:04:37.270 回答