查找 distutils 的 C 编译器版本

问题描述 投票:0回答:1

使用

distutils
构建 CPython 扩展的默认 C 编译器可通过

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

这样例如

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

将在

foo.c
上调用它。然后可以抓取
foo.o
的内容来获取编译器版本,比如

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

然而,这(如果需要的话当然也可以用 Python 编码)让我觉得这是一种访问编译器版本的非常黑客的方式。有没有更Pythonic的方式?

不用说,人们可以轻松找到这种情况的用例,因为如果使用特定版本的编译器,有时需要添加特定的编译器选项(

distutils
允许这样做)。


编辑: 人们可以做类似的事情

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

并看到类似

8.3.1
的打印内容(这是使用
gcc
实现的),或者使用
--version
而不是
-dumpversion
来显示更详细的输出。当然比查看目标文件好一点,但仍然不理想。

python c++ c distutils cpython
1个回答
0
投票

[Python.Docs]:distutils.ccompiler - CCompiler 基类 没有提及与此事有关的任何内容。
一些研究(和代码浏览)揭示了一种方法:

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
>>> output = sproc.run(cc.compiler + ["-v"], stdout=sproc.PIPE, stderr=sproc.STDOUT).stdout.decode()
>>> print(output)
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) 

注释

  • 它不是规范的,但它无需编译任何文件即可获取信息

  • 依赖系统路径来查找可执行文件

  • 我不会使用编译器特定的参数(-dumpversion)。 -v 适用于 GCCCLang(在我的例子中 GCC 是默认值):

    [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
    
    • 可以/应该进行额外的输出解析来提取相关信息(但这又取决于编译器 - 这是上面的示例):

      >>> [line for line in version.split("\n") if "version" in line][-1]
      'gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04) '
      
© www.soinside.com 2019 - 2024. All rights reserved.