【问题标题】:Overriding Python setuptool's default include_dirs and library_dirs?覆盖 Python setuptool 的默认 include_dirs 和 library_dirs?
【发布时间】:2018-07-25 15:08:06
【问题描述】:

我在setup.py 中指定了以下include_dirslibrary_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 的包含路径(为了便于阅读,我添加了换行符):

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

第三个包含路径没有明确指定,但在编译时仍然使用。

我将如何确保只使用我指定的include-dirs

【问题讨论】:

    标签: python setuptools


    【解决方案1】:

    您需要覆盖build_ext 命令,因为the stdlib's build_ext ensures the Python header files, both platform specific and not, are always appended to the include paths

    这是一个自定义 build_ext 命令的示例,该命令在选项完成后清理包含路径:

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

    更新

    库DIRS的方法:当最终确定选项时清洁列表:

    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)
    

    【讨论】:

    • 对于在安装后期修改用于链接的库目录有什么建议吗?我遇到了一个类似的问题,它确保在x86_64-sdk 路径下添加/usr/lib(因为这是python3 所在的位置),即使没有在--library-dirs 参数中指定该路径。
    • 清理库目录的方法是一样的;我已经用一个例子更新了答案。
    • 这也适用于 distutils 吗? (此处未使用设置工具)
    • @JohannesSchaub-litb 当然,只需覆盖 distutils.command.build_ext.build_ext
    猜你喜欢
    • 2011-12-20
    • 2020-07-12
    • 2018-03-26
    • 2016-04-13
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多