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.
[Python.Docs]: distutils.ccompiler - CCompiler base class doesn't mention anything in regards to this matter.
A bit of research (and code browsing) revealed a way:
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>> import subprocess as sproc >>> from distutils import ccompiler <stdin>:1: DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives >>> >>> >>> cc = ccompiler.new_compiler() >>> >>> dir(cc) ['EXECUTABLE', 'SHARED_LIBRARY', 'SHARED_OBJECT', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_check_macro_definitions', '_compile', '_find_macro', '_fix_compile_args', '_fix_lib_args', '_fix_object_args', '_get_cc_args', '_is_gcc', '_need_link', '_prep_compile', '_setup_compile', 'add_include_dir', 'add_library', 'add_library_dir', 'add_link_object', 'add_runtime_library_dir', 'announce', 'archiver', 'compile', 'compiler', 'compiler_cxx', 'compiler_so', 'compiler_type', 'create_static_lib', 'debug_print', 'define_macro', 'detect_language', 'dry_run', 'dylib_lib_extension', 'dylib_lib_format', 'exe_extension', 'executable_filename', 'executables', 'execute', 'find_library_file', 'force', 'has_function', 'include_dirs', 'language_map', 'language_order', 'libraries', 'library_dir_option', 'library_dirs', 'library_filename', 'library_option', 'link', 'link_executable', 'link_shared_lib', 'link_shared_object', 'linker_exe', 'linker_so', 'macros', 'mkpath', 'move_file', 'obj_extension', 'object_filenames', 'objects', 'output_dir', 'preprocess', 'preprocessor', 'ranlib', 'runtime_library_dir_option', 'runtime_library_dirs', 'set_executable', 'set_executables', 'set_include_dirs', 'set_libraries', 'set_library_dirs', 'set_link_objects', 'set_runtime_library_dirs', 'shared_lib_extension', 'shared_lib_format', 'shared_object_filename', 'spawn', 'src_extensions', 'static_lib_extension', 'static_lib_format', 'undefine_macro', 'verbose', 'warn', 'xcode_stub_lib_extension', 'xcode_stub_lib_format'] >>> >>> cc.compiler ['cc'] >>> cc.compiler_type 'unix' >>> >>> # @TODO - cfati: ------- Invoke with "-v" ------- >>> #cc.spawn(cc.compiler + ["-v"]) # This only dumps output on screen >>> cc_ver_out = sproc.run(cc.compiler + ["-v"], stdout=sproc.PIPE, stderr=sproc.STDOUT).stdout.decode() >>> print(cc_ver_out) Using built-in specs. COLLECT_GCC=/usr/bin/cc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.4.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2 Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
Notes:
It's not canonical, but it gets the information without having to compile any file
Relies on system paths for finding the executable
I wouldn't use compiler specific arguments (-dumpversion). -v works with both GCC and CLang (in my case GCC is default):
[cfati@cfati-5510-0:~]> which cc /usr/bin/cc [cfati@cfati-5510-0:~]> file /usr/bin/cc /usr/bin/cc: symbolic link to /etc/alternatives/cc [cfati@cfati-5510-0:~]> file /etc/alternatives/cc /etc/alternatives/cc: symbolic link to /usr/bin/gcc [cfati@cfati-5510-0:~]> file /usr/bin/gcc /usr/bin/gcc: symbolic link to gcc-11 [cfati@cfati-5510-0:~]> file /usr/bin/gcc-11 /usr/bin/gcc-11: symbolic link to x86_64-linux-gnu-gcc-11 [cfati@cfati-5510-0:~]> file /usr/bin/x86_64-linux-gnu-gcc-11 /usr/bin/x86_64-linux-gnu-gcc-11: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=bee27145fd189a47a04c578e204051498e609ed2, for GNU/Linux 3.2.0, stripped [cfati@cfati-5510-0:~]> [cfati@cfati-5510-0:~]> clang -v Ubuntu clang version 14.0.0-1ubuntu1.1 Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/11 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9 Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12 Candidate multilib: .;@m64 Selected multilib: .;@m64 Found CUDA installation: /usr/local/cuda-11.3, version 11.3
Extra output parsing could / should be done to extract the relevant information (but that again, would be compiler dependent - here's for the above example):
>>> [line for line in cc_ver_out.split("\n") if len(line) < 100 and "version" in line][0] 'gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04) '