由于工作需要,要部署python3到rv1126b平台上,该平台上只支持buildroot,没有包管理器,不能直接下载python3。所以拉下源码,进行交叉编译部署,目前已经成功,分享给各位网友。
1、下载源码
PYVER=3.10.12
wget https://www.python.org/ftp/python/$PYVER/Python-$PYVER.tgz
tar xf Python-$PYVER.tgz
2、编译x86架构python3
进入源码根目录,执行:
mkdir build_x86 && cd build_x86
../configure --prefix=$(pwd)/output
make -j8
make install
3、交叉编译python3
3.1 _tkinter模块不编译
根目录下setup.py注释:
# if not self.detect_tkinter():
self.missing.append('_tkinter')
3.2 libffi库缺失
/home/yang/project/jes-ipc/rv1126b-source/Python-3.10.12/Modules/_ctypes/_ctypes.c:107:10: fatal error: ffi.h: No such file or directory
107 | #include <ffi.h>
| ^~~~~~~
compilation terminated.
需要交叉编译libffi库
wget https://github.com/libffi/libffi/releases/download/v3.4.4/libffi-3.4.4.tar.gz
export CROSS_COMPILE=aarch64-rockchip1240-linux-gnu-
export CC=${CROSS_COMPILE}gcc
export AR=${CROSS_COMPILE}ar
export RANLIB=${CROSS_COMPILE}ranlib
export LD=${CROSS_COMPILE}ld
export STRIP=${CROSS_COMPILE}strip
../configure --host=aarch64-rockchip1240-linux-gnu \
--prefix=/home/yang/project/jes-ipc/rv1126b-source/libffi-3.4.4/build-rv1126b/output \
--disable-docs
make -j8
make install
3.3 开始交叉编译
mkdir build_rv1126b && cd build_rv1126b
touch set.sh
touch clean.sh
chmod +x *.sh
set.sh脚本内容如下:
#!/bin/bash
set -e
CROSS_PREFIX=aarch64-rockchip1240-linux-gnu-
HOSTPY=/home/yangyang/projects/Python-3.10.12/build-x86/output/bin/python3 # 上一步编译得到的 hostpython
PY_PREFIX=/home/yangyang/projects/Python-3.10.12/build-rv1126b/rv1126b-python3
PYVER=3.10.12
export CXX=${CROSS_PREFIX}g++
export AR=${CROSS_PREFIX}ar
export RANLIB=${CROSS_PREFIX}ranlib
export LD=${CROSS_PREFIX}ld
export STRIP=${CROSS_PREFIX}strip
export READELF=${CROSS_PREFIX}readelf
export PATH=/home/yangyang/projects/Python-3.10.12/build-x86/output/bin:$PATH
export SYSROOT=/home/yangyang/projects/rv1126b/sysroot
export PKG_CONFIG_SYSROOT_DIR=$SYSROOT
export PKG_CONFIG_LIBDIR=$SYSROOT/usr/lib/pkgconfig:$SYSROOT/lib/pkgconfig
export PKG_CONFIG_PATH=$SYSROOT/usr/lib/pkgconfig:$SYSROOT/lib/pkgconfig
export CPPFLAGS="-I${SYSROOT}/usr/include -I${SYSROOT}/include"
export LDFLAGS="-L${SYSROOT}/usr/lib -L${SYSROOT}/lib"
../configure \
--host=aarch64-rockchip1240-linux-gnu \
--build=$(../config.guess) \
--prefix=${PY_PREFIX} \
--enable-shared \
--with-system-ffi \
--with-ensurepip=install \
--disable-ipv6 \
--disable-toolbox-glue \
ac_cv_file__dev_ptmx=yes ac_cv_file__dev_ptc=no
# 编译
make -j8 HOSTPYTHON=${HOSTPY} BUILDPYTHON=${HOSTPY}
# 安装
make install
clean.sh脚本内容如下:
#!/bin/bash
set -e
# 获取脚本所在目录的绝对路径
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
echo "清理目录: $SCRIPT_DIR"
# 遍历目录下的文件和文件夹
for f in "$SCRIPT_DIR"/*; do
# 如果是 .sh 文件则跳过
if [[ "$f" == *.sh ]]; then
echo "保留: $f"
continue
fi
# 删除其他文件或文件夹
echo "删除: $f"
rm -rf "$f"
done
echo "✅ 清理完成"
在编译完成后,日志会有哪些三方库模块没有参与编译的显示,需要注意的是,zlib和openssl等模块比较重要,最好还是参与编译:
The necessary bits to build these optional modules were not found:
_bz2 _curses _curses_panel
_dbm _gdbm _sqlite3
_tkinter nis readline
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
_abc pwd time
三方库参与编译方式:加入到CPPFLAGS以及LDFLAGS当中去。
部署到设备上之后,需要下载pip,可以执行:
curl -fsSL -o ./get-pip.py https://bootstrap.pypa.io/get-pip.py
chmod +x ./get-pip.py
python3 get-pip.py
3650

被折叠的 条评论
为什么被折叠?



