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

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

本文介绍了获取distutils传递给编译器的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



<$ p从distutils.core导入setup,Extension

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

setup(name ='PackageName',
version ='1.0',
description ='这是一个演示包',
ext_modules = [module1])$ ​​b $ b

很简单。现在我用这行代码调用 setup.py 脚本:

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



好的,但最新的问题是什么?



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



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

解决方案

作为一组选项,但你可以看到它是如何工作的。在你的python源代码目录下寻找这个

  distutils / ccompiler.py 

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

  #将编译器类型映射到(module_name,class_name)对 - 即。 
#在哪里找到实现此编译器接口的代码。 (模块
#被假定在'distutils'包中。)
compiler_class = {'unix':('unixccompiler','UnixCCompiler',
标准UNIX风格的编译器),
'msvc':('msvccompiler','MSVCCompiler',
Microsoft Visual C ++),
'cygwin':('cygwinccompiler','CygwinCCompiler',
用于Win32的GNU C编译器的Cygwin端口),
'mingw32':('cygwinccompiler','Mingw32CCompiler',
用于Win32的GNU C编译器的Mingw32端口),
'bcpp':('bcppcompiler','BCPPCompiler',
Borland C ++编译器),
'emx':('emxccompiler','EMXCCompiler',
GNU C编译器for OS / 2),
}

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

  distutils / cygwinccompiler.py 

如果你编辑你的setup.py脚本并从distutils.core import setup中添加这个

 ,扩展名
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 ='这是一个演示软件包',
ext_modules = [module1])$ ​​b $ b

你可以看到很多可用选项...

  {'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,
'库':[],
'library_dirs':[],
'linker_dll':'dllwrap',
'linker_exe':['gcc'],
'linker_so': ['dllwrap','-mdll','-static'],
'macros':[],
'objects':[],
'output_dir':None,
'preprocessor':无,
'ranlib':['ranlib'],
'runtime_library_dirs':[],
'verbose':0}

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

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

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


Lets say I have this Python code in a setup.py script to build a C extension:

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])

Easy enough. Now I call the setup.py script with this line:

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

Ok, but whats the question?

When distutils calls mingw32 and passes all the necessary and operating system independant flags and options to it, how does it figure those flags out?

Where does distutils keep the commands related to each platform, and how can I access them?

解决方案

It's not as simple as a set of options but you can see how it works. In your python source directory look for this

distutils/ccompiler.py

In that file each compiler has an entry like this

# 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"),
                 }

You can find the code you're looking for in

distutils/cygwinccompiler.py

If you edit your setup.py script and add this

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])

You can see quite a few of the options available...

{'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}

To access individual options you can use them as follows...

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

There's no simple set of flags. A lot of the flags are configured at runtime and the code above shows you were to look to see how they're generated etc.

这篇关于获取distutils传递给编译器的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:14