覆盖 Python setuptool 的默认 include_dirs 和 library_dirs? [英] Overriding Python setuptool's default include_dirs and library_dirs?
问题描述
我在 setup.py
中指定了以下 include_dirs
和 library_dirs
:
I specify in setup.py
the following include_dirs
and library_dirs
:
/opt/x86_64-sdk-linux/usr/bin/python3 setup.py build_ext \
--include-dirs=/opt/neon-poky-linux-gnueabi/usr/include/python3.5m/ \
--library-dirs=/opt/neon-poky-linux-gnueabi/usr/lib/ \
--rpath=/opt/neon-poky-linux-gnueabi/usr/lib/ \
--plat-name=linux_armv7l
但是,生成的 gcc 命令(在执行 python3 setup.py build_ext
时)还包括运行 python3
的包含路径(我添加了换行符以提高可读性):
However, the generated gcc commands (when executing python3 setup.py build_ext
) also include the include path where python3
is running from (I have added newlines for readability):
arm-poky-linux-gnueabi-gcc \
--sysroot=/opt/neon-poky-linux-gnueabi \
-I. \
-I/opt/neon-poky-linux-gnueabi/usr/include/python3.5m/ \
-I/opt/x86_64-sdk-linux/usr/include/python3.5m \
-c py/constraint.cpp -o build/temp.linux-x86_64-3.5/py/constraint.o
第三个包含路径没有明确指定,但在编译时仍然使用.
The third include path was not specified explicitly, but is still used when compiling.
我将如何确保仅使用我指定的 include-dirs
?
How would I go about ensuring only the include-dirs
I specify are being used?
推荐答案
您将需要覆盖 build_ext
命令,因为 stdlib 的 build_ext
确保特定于平台的头文件和 Python 头文件始终不是特定于平台的包含路径.
You will need to override build_ext
command because the stdlib's build_ext
ensures the Python header files, both platform specific and not, are always appended to the include paths.
以下是自定义 build_ext
命令的示例,用于在选项完成后清理包含路径:
Here's an example of custom build_ext
command that cleans up the include paths after the options are finalized:
# setup.py
from distutils import sysconfig
from setuptools import setup
from setuptools.command.build_ext import build_ext as build_ext_orig
class build_ext(build_ext_orig):
def finalize_options(self):
super().finalize_options()
py_include = sysconfig.get_python_inc()
plat_py_include = sysconfig.get_python_inc(plat_specific=1)
for path in (py_include, plat_py_include, ):
for _ in range(self.include_dirs.count(path)):
self.include_dirs.remove(path)
setup(
...,
cmdclass={'build_ext': build_ext},
)
更新
库目录的相同方法:在选项最终确定时清理列表:
Update
Same approach for library dirs: clean the list when options are finalized:
class build_ext(build_ext_orig):
def finalize_options(self):
super().finalize_options()
...
libdir = sysconfig.get_config_var('LIBDIR')
for _ in range(self.library_dirs.count(libdir)):
self.library_dirs.remove(libdir)
这篇关于覆盖 Python setuptool 的默认 include_dirs 和 library_dirs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!