问题
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
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.
How would I go about ensuring only the include-dirs
I specify are being used?
回答1:
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.
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)
来源:https://stackoverflow.com/questions/51522248/overriding-python-setuptools-default-include-dirs-and-library-dirs