views:

932

answers:

4

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.

A: 

See the default with: distutils.ccompiler.get_default_compiler

vartec
When I call distutils.sysconfig.get_config_vars()it returns {'EXE': '.exe', 'exec_prefix': 'C:\\Python25', 'LIBDEST': 'C:\\Python25\\Lib', 'prefix': 'C:\\Python25', 'SO': '.pyd', 'BINLIBDEST': 'C:\\Python25\\Lib', 'INCLUDEPY': 'C:\\Python25\\include'}No 'CC' there...
Headcrab
That's because it's going to use the default one. Use distutils.ccompiler.get_default_compiler()
vartec
No, there's no 'CC' in the dictionary even if I specify (and it uses) a non-default compiler.
Headcrab
I guess that's unix-only thing..
vartec
A: 

import distutils.ccompiler

compiler_name = distutils.ccompiler.get_default_compiler()

attwad
+1  A: 

You can subclass the distutils.command.build_ext.build_ext command.

Once build_ext.finalize_options() method has been called, the compiler type is stored in self.compiler as a string (the same as the one passed to the build_ext's --compiler option, e.g. 'mingw32', 'gcc', etc...).

Luper Rouch
A: 
#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() + '"'
popelkopp