简体   繁体   English

如何交叉编译Python?

[英]How to cross-compile Python?

I have an SoC with Cortex A9 ARMv7 architecture.我有一个采用 Cortex A9 ARMv7 架构的 SoC。 I need to be able to run python scripts on a board with this SoC.我需要能够使用这个 SoC 在板上运行 python 脚本。 So I need to cross-compile Python for this platform.所以我需要为这个平台交叉编译Python。

The SoC has ARMv7 architecture, so I've installed the arm-linux-gnueabihf cross-compiler for Ubuntu 20.04 running in the VirtualBox. SoC 具有 ARMv7 架构,因此我为 VirtualBox 中运行的 Ubuntu 20.04 安装了arm-linux-gnueabihf交叉编译器。

I am following this instruction:我正在遵循说明:

  1. First I've downloaded Python 2.7.1 sources and extracted it to the /home/user/python/Python-2.7.1 directory.首先,我下载了Python 2.7.1源代码并将其解压缩到/home/user/python/Python-2.7.1目录。

  2. Then I've downloaded the patch from the instruction I am following.然后我从我遵循的说明下载了补丁

  3. Then I've applied the patch:然后我应用了补丁:

     patch -p1 < python-2.7.1-cross-compile.patch
  4. Then I've compiled some tools for host:然后我为主机编译了一些工具:

     ./configure make python Parser/pgen mv python hostpython mv Parser/pgen Parser/hostpgen make distclean
  5. Then I've configured Python for cross-compilation:然后我配置了 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. And then I finally cross-compile it:然后我终于交叉编译它:

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

    But eventually I have got an IndentationError :但最终我得到了一个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

What I do wrong and how to solve this problem?我做错了什么以及如何解决这个问题?


It seems that Python itself is successfully compiled and linked because after all this process I get the python file in the build directory:似乎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

I did python 2.7.3 a while ago.不久前我做了python 2.7.3。 The summary is here . 总结在这里 All other files are in the same directory as this txt summary.所有其他文件与此 txt 摘要位于同一目录中。 Not sure whether it still works or not on ubuntu 20. Though it was for arm v5, you'll need to do some adjustment.不确定它在 ubuntu 20 上是否仍然有效。虽然它适用于 arm v5,但您需要做一些调整。

Any version after 2.7.3 uses a dramatically different build system. 2.7.3 之后的任何版本都使用截然不同的构建系统。 The same was not working.同样不起作用。 I'm looking for instructions to build 3.8.我正在寻找构建 3.8 的说明。


Update: If you want to build python 3.8.6, I saw someone mentioned on SO about using buildroot.更新:如果你想构建 python 3.8.6,我看到有人在 SO 上提到使用 buildroot。 Here is a note that works .这是一个有效的说明 I've created a virtual env using it on the target device.我已经在目标设备上使用它创建了一个虚拟环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM