distutils
/<code>setuptools 允许在脚本extra_compile_args
中定义 Python 扩展时使用 /<code>extra_link_args 参数指定任何编译器/链接器标志。setup.py
这些额外的标志将在默认标志之后添加,并将覆盖之前存在的任何互斥标志。
但是,对于常规使用,这并没有多大用处,因为您通过 PyPI 分发的包可以由具有不兼容选项的不同编译器构建。
以下代码允许您以特定于扩展和编译器的方式指定这些选项:
from setuptools import setup
from setuptools.command.build_ext import build_ext
class build_ext_ex(build_ext):
extra_compile_args = {
'extension_name': {
'unix': ['-O0'],
'msvc': ['/Od']
}
}
def build_extension(self, ext):
extra_args = self.extra_compile_args.get(ext.name)
if extra_args is not None:
ctype = self.compiler.compiler_type
ext.extra_compile_args = extra_args.get(ctype, [])
build_ext.build_extension(self, ext)
setup(
...
cmdclass = {'build_ext': build_ext_ex},
...
)
当然,如果您希望所有扩展都使用相同的(但仍然是特定于编译器的)选项,您可以简化它。
以下是支持的编译器类型的列表(由 返回setup.py build_ext --help-compiler
):
--compiler=bcpp Borland C++ Compiler
--compiler=cygwin Cygwin port of GNU C Compiler for Win32
--compiler=mingw32 Mingw32 port of GNU C Compiler for Win32
--compiler=msvc Microsoft Visual C++
--compiler=unix standard UNIX-style compiler