finding distutils' C compiler version

Viewed 103

The default C compiler to build CPython extensions with distutils is available via

from distutils.ccompiler import new_compiler, get_default_compiler
cc=new_compiler(get_default_compiler())

so that e.g.

cc.compile(["foo.c"])

will invoke it on foo.c. Then one can scrape the contents of foo.o to get the compiler version, say

$ strings foo.o | grep GCC
GCC: (Gentoo 8.3.1-r2 p4) 8.3.1 20190518

This (which certainly can also be coded in Python if needed) however strikes me as a very hacky way to get access to the compiler version. Is there a more Pythonic way?

Needless to say, one can easily find a use case for this, as sometimes one would need to add particular compiler option (distutils allows for this) if a particular version of compiler is used.


EDIT: one can do something like

_=cc.compile(['foo.c'],extra_preargs=['-dumpversion'])

and see something like 8.3.1 printed (this is with gcc), or more verbose output with --version instead of -dumpversion. Certainly a bit better than looking through the object file, but still not ideal.

0 Answers
Related