【问题标题】:Get the commands distutils passes to the compiler获取 distutils 传递给编译器的命令
【发布时间】:2016-07-12 18:28:47
【问题描述】:

假设我在 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 gcc command-line compilation distutils


【解决方案1】:

它不像一组选项那么简单,但您可以看到它是如何工作的。在你的 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']

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

【讨论】:

    猜你喜欢
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多