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.
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 inself.compiler.compiler_type
as a string (the same as the one passed to thebuild_ext
's--compiler
option, e.g. 'mingw32', 'gcc', etc...).import distutils.ccompiler
compiler_name = distutils.ccompiler.get_default_compiler()
You can use self.distribution.get_command_obj('build_ext') to get build_ext instance, and then get the compiler_type
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: