How to tell which specific compiler will be invoked for a Python C extension: GCC or Clang?
compiler-flags, distutils, python, setup.py, setuptools
Solution
I hit upon your question as I need the same kind of switch. Besides, in my case, `sys.prefix` is not great as the flags are for `clang` regardless of the platform.
I am not sure it is perfect but here is what works best for me. So, I check if a `CC` variable is set; if not, I check what I guess is where `distutils` looks at.
Any better solution welcome!
import os
import distutils
try:
if os.environ['CC'] == "clang":
clang = True
except KeyError:
clang = False
if clang or distutils.sysconfig_get_config_vars()['CC'] == 'clang':
try:
_ = os.environ['CFLAGS']
except KeyError:
os.environ['CFLAGS'] = ""
os.environ['CFLAGS'] += " -Wno-unused-function"
os.environ['CFLAGS'] += " -Wno-int-conversion"
os.environ['CFLAGS'] += " -Wno-incompatible-pointer-types
Note for the grumpy guys: I would have loved to use the `extra_compile_args` option, but it puts the flags at the wrong position in the `clang` compilation command.
Problem
I have a Python C++ extension that requires the following compilation flags when compiled using Clang on OS X: ``` CPPFLAGS='-std=c++11 -stdlib=libc++ -mmacosx-version-min=10.8' LDFLAGS='-lc++' ``` Detecting OS X in my `setup.py` is easy enough. I can do this: ``` if sys.prefix == 'darwin': compile_args.append(['-mmacosx-version-min=10.8', '-stdlib=libc++']) link_args.append('-lc++') ``` (See here for full context) However, on GCC this compilation flag is invalid. So, compilation will fail if someone will try to use GCC on OS X if I write the `setup.py` this way. GCC and Clang support different compiler flags. So, I need to know which compiler will be invoked, so I can send different flags. What is the right way to detect the compiler in the `setup.py`? Edit 1: Note that no Python exception is raised for compilation errors: ``` $ python setup.py build_ext --inplace running build_ext building 'spacy.strings' extension gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -c spacy/strings.cpp -o build/temp.linux-x86_64-2.7/spacy/strings.o -O3 -mmacosx-version-min=10.8 -stdlib=libc++ gcc: error: unrecognized command line option ‘-mmacosx-version-min=10.8’ gcc: error: unrecognized command line option ‘-stdlib=libc++’ error: command 'gcc' failed with exit status 1 $ ```