[ACCEPTED]-Python distutils, how to get a compiler that is going to be used?-distutils

Accepted answer
Score: 35

This is an expanded version of Luper Rouch's 10 answer that worked for me to get an openmp 9 extension to compile using both mingw and 8 msvc on windows. After subclassing build_ext 7 you need to pass it to setup.py in the cmdclass 6 arg. By subclassing build_extensions instead 5 of finalize_options you'll have the actual 4 compiler object to look into, so you can 3 then get more detailed version information. You 2 could eventually set compiler flags on a 1 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 } )
Score: 8

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

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

Score: 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() + '"'

0

Score: 0
import sys
sys.argv.extend(['--compiler', 'msvc'])

0

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

You can use self.distribution.get_command_obj('build_ext') to 2 get build_ext instance, and then get the 1 compiler_type

More Related questions