pythonc++cdistutilscpython

finding distutils' C compiler version


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.


Solution

  • [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: