【问题标题】:How to cross-compile Python?如何交叉编译 Python?
【发布时间】:2020-12-15 03:25:28
【问题描述】:

我有一个采用 Cortex A9 ARMv7 架构的 SoC。我需要能够使用这个 SoC 在板上运行 python 脚本。所以我需要为这个平台交叉编译Python。

该 SoC 具有 ARMv7 架构,因此我为在 VirtualBox 中运行的 Ubuntu 20.04 安装了 arm-linux-gnueabihf 交叉编译器。

我正在关注this 指令:

  1. 首先我下载了​​Python 2.7.1 源并将其解压缩到/home/user/python/Python-2.7.1 目录。

  2. 然后我根据我遵循的说明下载了patch

  3. 然后我已经应用了补丁:

    patch -p1 < python-2.7.1-cross-compile.patch
    
  4. 然后我为宿主编译了一些工具:

    ./configure
    make python Parser/pgen
    mv python hostpython
    mv Parser/pgen Parser/hostpgen
    make distclean
    
  5. 然后我为交叉编译配置了 Python:

    readonly CROSS_COMPILER=arm-linux-gnueabihf
    
    CC=${CROSS_COMPILER}-gcc \
    CXX=${CROSS_COMPILER}-g++ \
    AR=${CROSS_COMPILER}-ar \
    RANLIB=${CROSS_COMPILER}-ranlib \
    ./configure \
    --host=${CROSS_COMPILER} \
    --target=${CROSS_COMPILER} \
    --prefix=/python
    
  6. 然后我终于交叉编译了它:

    make \
    HOSTPYTHON=./hostpython \
    HOSTPGEN=./Parser/hostpgen \
    BLDSHARED="${CROSS_COMPILER}-gcc -shared" \
    CROSS_COMPILE=${CROSS_COMPILER}- \
    CROSS_COMPILE_TARGET=yes
    

    但最终我得到了IndentationError

    /usr/lib/gcc-cross/arm-linux-gnueabihf/9/../../../../arm-linux-gnueabihf/bin/ld: libpython2.7.a(posixmodule.o): in function `posix_tmpnam':
    /home/user/python/Python-2.7.1/./Modules/posixmodule.c:7346: warning: the use of `tmpnam_r' is dangerous, better use `mkstemp'
    /usr/lib/gcc-cross/arm-linux-gnueabihf/9/../../../../arm-linux-gnueabihf/bin/ld: libpython2.7.a(posixmodule.o): in function `posix_tempnam':
    /home/user/python/Python-2.7.1/./Modules/posixmodule.c:7301: warning: the use of `tempnam' is dangerous, better use `mkstemp'
    File "./setup.py", line 316
      self.announce('*** WARNING: renaming "%s" since importing it'
        ^
    IndentationError: expected an indented block
    make: *** [Makefile:425: sharedmods] Error 1
    

我做错了什么以及如何解决这个问题?


似乎 Python 本身已成功编译和链接,因为经过所有这些过程,我在构建目录中获得了 python 文件:

$ file python
python: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, BuildID[sha1]=1a757bca3295fe062ffbee5cf2d791eb36861524, for GNU/Linux 3.2.0, with debug_info, not stripped

【问题讨论】:

    标签: python linux python-2.7 cross-compiling embedded-linux


    【解决方案1】:

    我前段时间做了 python 2.7.3。 summary is here。所有其他文件与此 txt 摘要位于同一目录中。不确定它在 ubuntu 20 上是否仍然有效。虽然它适用于 arm v5,但您需要进行一些调整。

    2.7.3 之后的任何版本都使用截然不同的构建系统。同样没有用。我正在寻找构建 3.8 的说明。


    更新:如果你想构建 python 3.8.6,我看到有人在 SO 上提到使用 buildroot。这是a note that works。我已经在目标设备上使用它创建了一个虚拟环境。

    【讨论】:

    • @eanmos,它今天仍然有效吗?感谢您的反馈。
    • 对不起,我不再从事这个项目了,所以我无法访问董事会,也无法检查它。无论如何,谢谢你的回答,我希望它会对某人有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-05-04
    • 2012-01-26
    • 1970-01-01
    • 2017-11-13
    • 2014-04-30
    • 2014-07-10
    • 2011-07-14
    • 1970-01-01
    相关资源
    最近更新 更多