36

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.

1
  • I have the same problem. I want to include an extra stdint.h header for msvc, but not other compilers. Commented Jul 7, 2015 at 22:36

6 Answers 6

36
+50

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 } ) 
Sign up to request clarification or add additional context in comments.

2 Comments

it should be noted that for gcc/clang under linux/posix the keyname for lopt has to be 'unix'
Thanks. The information really ought to be more accessible than that. We should report a bug to distutils
9

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.compiler_type as a string (the same as the one passed to the build_ext's --compiler option, e.g. 'mingw32', 'gcc', etc...).

2 Comments

The compiler object is self.compiler, the type is in self.compiler.compiler_type
The compiler_type is set in the build_ext.run method from distutils. So if you have a build_ext subclass and overridden the run method, compiler_type might never be set.
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() + '"' 

1 Comment

Regular expressions are not needed here and should be compiled when they are used multiple times.
0
import sys sys.argv.extend(['--compiler', 'msvc']) 

Comments

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 get build_ext instance, and then get the compiler_type

Comments

-1

import distutils.ccompiler

compiler_name = distutils.ccompiler.get_default_compiler()

1 Comment

This is not necessarily the compiler that distutils is going to use!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.