Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the specific C compiler type from Python distutils?

Tags:

python

c

I'd like to check the system's C compiler in Python so that I can add library links accordingly to compile my Cython code.

I understand distutils.ccompiler.get_default_compiler() or something like compiler.compiler_type would return a compiler name. But it is too coarse just like "unix", etc.

What I need is more specific information such as "gcc", "icc", "clang", etc., which are all shown as "unix" using the methods above.

One possible way to get the information is to check the system's environment variable CC via os.environ["CC"], but it is not guaranteed that every system has CC defined so it is not a universal solution.

So, what should I do then? Thanks in advance!

like image 866
YDW Avatar asked Sep 09 '16 23:09

YDW


2 Answers

Generally you should be able to use the platform module to get the info:

>>> import platform
>>> platform.python_compiler()
'GCC 4.8.5 20150623 (Red Hat 4.8.5-4)'
like image 167
l'L'l Avatar answered Oct 11 '22 05:10

l'L'l


You can use distutils to get the actual c compiler and read its properties

from distutils.ccompiler import new_compiler
from distutils.sysconfig import customize_compiler
ccompiler = new_compiler()
customize_compiler(ccompiler)

path_to_compiler = ccompiler.compiler[0]
like image 32
F Pereira Avatar answered Oct 11 '22 03:10

F Pereira