下载源码

        https://www.python.org/downloads/source/。选择Python-3.8.20的xz版本。

交叉编译

        配置交叉编译工具链环境,由于使用的是模组厂商提供的yocto编译链,aarch64-zxic-linux-gcc,里面已经包含了很多库和头文件,所以不需要额外下载单独编译。

        最终配置参数命令:

mkdir local_build
./configure --host=arm-linux --build=x86_64-linux-gnu --prefix=/home/wangyb/sda3/zm9300-tools/tools/Python-3.8.20/local_build/

        问题1:提示交叉编译的文件不能运行,需要同时设置host和build参数

[wangyb@wangyb-VirtualBox:Python-3.8.20]$ ./configure
configure: loading site script /home/wangyb/sda3/zm9300c-V2XR240060/toolchain/site-config-aarch64-zxic-linux
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.8... python3.8
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... "linux"
checking for gcc... aarch64-zxic-linux-gcc  -mcpu=cortex-a53+crc  -fstack-protector-all  -pie -fPIE  -Wformat -Wformat-security -Werror=format-security  -fstack-protector-all  -pie -fPIE  -Wformat -Wformat-security -Werror=format-security --sysroot=/home/wangyb/sda3/zm9300c-V2XR240060/toolchain/sysroots/aarch64-zxic-linux
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... configure: error: in `/home/wangyb/sda3/zm9300-tools/tools/Python-3.8.20':
configure: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
See `config.log' for more details
	
configure:4045: checking whether we are cross compiling
configure:4053: aarch64-zxic-linux-gcc --sysroot=/home/wangyb/sda3/zm9300c-V2XR240060/toolchain/sysroots/aarch64-zxic-linux -o conftest  -O2 -pipe -g -feliminate-unused-debug-types   -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed  -fstack-protector-all -Wl,-z,relro,-z,now  -fstack-protector-all -Wl,-z,relro,-z,now conftest.c  >&5
configure:4057: $? = 0
configure:4064: ./conftest
./configure: line 4066: ./conftest: cannot execute binary file: Exec format error
configure:4068: $? = 126
configure:4075: error: in `/home/wangyb/sda3/zm9300-tools/tools/Python-3.8.20':
configure:4077: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
See `config.log' for more details

        问题2:提示/dev/ptmx... not set,查看是存在/dev/ptmx文件,所以需要加上yes,删除目录重新解压重新配置解决。

./configure --host=arm-linux --build=x86_64-linux-gnu --prefix=/home/wangyb/sda3/zm9300-tools/tools/Python-3.8.20/local_build/ --disable-ipv6 ac_cv_file__dev_ptmx=yes

        问题3:提示/dev/ptc... not set,查看没有/dev/ptc文件,所以需要设置no

./configure --host=arm-linux --build=x86_64-linux-gnu --prefix=/home/wangyb/sda3/zm9300-tools/tools/Python-3.8.20/local_build/ --disable-ipv6 ac_cv_file__dev_ptmx=yes ac_cv_file__dev_ptc=no

        make编译成功:

[wangyb@wangyb-VirtualBox:Python-3.8.20]$ file python
python: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, BuildID[sha1]=a750cfafb04ee7d9d390d4b2491ce932e8bfa724, for GNU/Linux 3.14.0, not stripped

[wangyb@wangyb-VirtualBox:Python-3.8.20]$ ls python -lh
-rwxrwxr-x 1 wangyb wangyb 17M 2月  25 14:25 python

[wangyb@wangyb-VirtualBox:Python-3.8.20]$ aarch64-zxic-linux-strip python
[wangyb@wangyb-VirtualBox:Python-3.8.20]$ ls python -lh
-rwxrwxr-x 1 wangyb wangyb 3.3M 2月  25 14:30 python

linux_genvict:root# ./python-strip -V
Python 3.8.20

        make install进行依赖库和文件安装到目录local_build,否则单独的python不能成功运行

linux_genvict:root# ./python
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Python path configuration:
  PYTHONHOME = (not set)
  PYTHONPATH = (not set)
  program name = './python-strip'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = '/root/python-strip'
  sys.base_prefix = '/home/wangyb/sda3/zm9300-tools/tools/Python-3.8.20/local_build'
  sys.base_exec_prefix = '/home/wangyb/sda3/zm9300-tools/tools/Python-3.8.20/local_build'
  sys.executable = '/root/python-strip'
  sys.prefix = '/home/wangyb/sda3/zm9300-tools/tools/Python-3.8.20/local_build'
  sys.exec_prefix = '/home/wangyb/sda3/zm9300-tools/tools/Python-3.8.20/local_build'
  sys.path = [
    '/home/wangyb/sda3/zm9300-tools/tools/Python-3.8.20/local_build/lib/python38.zip',
    '/home/wangyb/sda3/zm9300-tools/tools/Python-3.8.20/local_build/lib/python3.8',
    '/home/wangyb/sda3/zm9300-tools/tools/Python-3.8.20/local_build/lib/lib-dynload',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x0000007fbd09c600 (most recent call first):
<no Python frame>
linux_genvict:root# 

安装运行

        local_build目录200MB,压缩后拷贝到板子环境中,解压到emmc存储中进行运行测试。

[wangyb@wangyb-VirtualBox:Python-3.8.20]$ du -sm local_build/
200	local_build/

linux_genvict:root# /mnt/emmc1/local_build/bin/python3 -V
Python 3.8.20

linux_genvict:root# /mnt/emmc1/local_build/bin/python3
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello")
hello
>>> 

        如果需要放在发布包中需要压缩后存放app目录。

Logo

openvela 操作系统专为 AIoT 领域量身定制,以轻量化、标准兼容、安全性和高度可扩展性为核心特点。openvela 以其卓越的技术优势,已成为众多物联网设备和 AI 硬件的技术首选,涵盖了智能手表、运动手环、智能音箱、耳机、智能家居设备以及机器人等多个领域。

更多推荐