获取 distutils 传递给编译器的命令

2023-12-23

假设我有这个Python代码setup.py构建 C 扩展的脚本:

from distutils.core import setup, Extension

module1 = Extension('demo', sources = ['demo.c'])

setup (name = 'PackageName',
       version = '1.0',
       description = 'This is a demo package',
       ext_modules = [module1])

很容易。现在我打电话给setup.py包含这一行的脚本:

C:/> python setup.py build_ext --compiler=mingw32

好吧,但是问题是什么?

当 distutils 调用 mingw32 并将所有必要的且独立于操作系统的标志和选项传递给它时,它如何计算出这些标志?

distutils 在哪里保存与每个平台相关的命令,以及如何访问它们?


它并不像一组选项那么简单,但您可以看到它是如何工作的。在你的 python 源目录中查找这个

distutils/ccompiler.py

在该文件中,每个编译器都有一个像这样的条目

# Map compiler types to (module_name, class_name) pairs -- ie. where to
# find the code that implements an interface to this compiler.  (The module
# is assumed to be in the 'distutils' package.)
compiler_class = { 'unix':    ('unixccompiler', 'UnixCCompiler',
                               "standard UNIX-style compiler"),
                   'msvc':    ('msvccompiler', 'MSVCCompiler',
                               "Microsoft Visual C++"),
                   'cygwin':  ('cygwinccompiler', 'CygwinCCompiler',
                               "Cygwin port of GNU C Compiler for Win32"),
                   'mingw32': ('cygwinccompiler', 'Mingw32CCompiler',
                               "Mingw32 port of GNU C Compiler for Win32"),
                   'bcpp':    ('bcppcompiler', 'BCPPCompiler',
                               "Borland C++ Compiler"),
                   'emx':     ('emxccompiler', 'EMXCCompiler',
                               "EMX port of GNU C Compiler for OS/2"),
                 }    

您可以在其中找到您要查找的代码

distutils/cygwinccompiler.py

如果您编辑 setup.py 脚本并添加以下内容

from distutils.core import setup,Extension
from distutils.cygwinccompiler import Mingw32CCompiler
from pprint import pprint

module1 = Extension('demo', sources = ['demo.c'])

m32 = Mingw32CCompiler()
pprint (vars(m32))


setup (name = 'PackageName',
   version = '1.0',
   description = 'This is a demo package',
   ext_modules = [module1])

您可以看到很多可用的选项...

{'archiver': ['ar', '-cr'],
 'compiler': ['gcc', '-O', '-Wall'],
 'compiler_cxx': ['g++', '-O', '-Wall'],
 'compiler_so': ['gcc', '-mdll', '-O', '-Wall'],
 'dll_libraries': None,
 'dllwrap_version': None,
 'dry_run': 0,
 'force': 0,
 'gcc_version': LooseVersion ('4.2.1'),
 'include_dirs': [],
 'ld_version': None,
 'libraries': [],
 'library_dirs': [],
 'linker_dll': 'dllwrap',
 'linker_exe': ['gcc'],
 'linker_so': ['dllwrap', '-mdll', '-static'],
 'macros': [],
 'objects': [],
 'output_dir': None,
 'preprocessor': None,
 'ranlib': ['ranlib'],
 'runtime_library_dirs': [],
 'verbose': 0}

要访问各个选项,您可以按如下方式使用它们...

print m32.compile
['gcc', '-O', '-Wall']

没有简单的一组标志。许多标志是在运行时配置的,上面的代码显示您要查看它们是如何生成的等。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

获取 distutils 传递给编译器的命令 的相关文章

随机推荐