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

Get the commands distutils passes to the compiler

假设我在 setup.py 脚本中有此 Python 代码来构建 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']

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