参考
https://datko.net/2013/05/10/cross-compiling-python-3-3-1-for-beaglebone-arm-angstrom/
平台
主机: ubuntu14.04 64bit
开发板: qemu + aarch64 (参考: http://www.cnblogs.com/pengdonglin137/p/6442583.html)
工具链:
aarch64-linux-gnu-gcc (gcc version 4.9.1 20140529) 用于编译aarch64上面运行的程序
arm-none-linux-gnueabi-gcc (gcc version 4.8.3 20140320) 用于编译aarch32上面运行的程序
Python版本: Python-3.6.0
概述
前面几篇博文介绍了Python2.7.13的交叉编译,在编译前需要给Python源码打上用于交叉编译的patch,从Python-3.3.1开始,就不需要再打什么patch了,只需要在configure的时候指定--build和--host参数即可。
下面我们以aarch64和aarch32为例说明。
正文
一、编译PC上面用的Python
因为在交叉编译的时候需要在PC上面运行对应版本的Python程序,所以我们需要先编译和安装PC上面用的Python,下面是命令:
#!/bin/bash ../Python-3.6./configure make -j8
sudo make install
默认,Python3.6.0会被安装到/usr/local下面。
二、交叉编译
1、配置 mk1_conf.sh
aarch32
#!/bin/bash export PATH=/home/pengdonglin/src/qemu/aarch32/arm-2014.05/bin:$PATH ../Python-3.6./configure \
--host=arm-none-linux-gnueabi \
--build=armv7 \
--prefix=`pwd` \
--enable-ipv6 \
--enable-shared \
ac_cv_file__dev_ptmx="yes" \
ac_cv_file__dev_ptc="no" \
LDFLAGS="-L/home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch32/lib" \
CPPFLAGS="-I/home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch32/include"
其中--build表示编译出来的程序运行的CPU架构,--host是交叉编译工具链。这里我已经吧sqlite3的lib和include添加上了,所以生成的python3是支持sqlite3的。
aarch64
#!/bin/bash export PATH=/home/pengdonglin/src/qemu/aarch64/gcc-linaro-aarch64-linux-gnu-4.9-.07_linux/bin:$PATH ../Python-3.6./configure \
--host=aarch64-linux-gnu \
--build=aarch64 \
--prefix=`pwd` \
--enable-ipv6 \
--enable-shared \
ac_cv_file__dev_ptmx="yes" \
ac_cv_file__dev_ptc="no" \
LDFLAGS="-L/home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch64/lib" \
CPPFLAGS="-I/home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch64/include"
只需要替换运行的CPU架构为aarch64,然后设置对应的工具链即可,别忘了还有sqlite3的相关路径的修改。
2、编译和安装 mk2_make.sh
这里没有什么好说的,下面是脚本内容:
aarch32
#!/bin/bash export PATH=/home/pengdonglin/src/qemu/aarch32/arm-2014.05/bin:$PATH make -j4
make install
- aarch64
#!/bin/bash export PATH=/home/pengdonglin/src/qemu/aarch64/gcc-linaro-aarch64-linux-gnu-4.9-.07_linux/bin:$PATH make -j4
make install
3、制作ramdisk
为了便于在python2和python3之间的切换,适当修改mk_ramdisk.sh如下:
aarch32
#!/bin/bash sudo rm -rf rootfs
sudo rm -rf tmpfs
sudo rm -rf ramdisk* sudo mkdir rootfs
sudo cp ../busybox-1.24./_install/* rootfs/ -raf sudo mkdir -p rootfs/proc/
sudo mkdir -p rootfs/sys/
sudo mkdir -p rootfs/tmp/
sudo mkdir -p rootfs/root/
sudo mkdir -p rootfs/var/
sudo mkdir -p rootfs/mnt/ sudo cp etc rootfs/ -arf sudo cp -arf ../arm-2014.05/arm-none-linux-gnueabi/libc/lib rootfs/ ver=3
sudo mkdir -p rootfs/usr
pushd rootfs/usr
sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/Python${ver}/aarch32/lib .
sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/Python${ver}/aarch32/include .
sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/Python${ver}/aarch32/bin .
sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/Python${ver}/aarch32/share .
sudo /home/pengdonglin/src/qemu/aarch32/arm-2014.05/bin/arm-none-linux-gnueabi-strip lib/python*
popd #sqlite
sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch32/bin/* rootfs/bin/
sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch32/include/* rootfs/include/
sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch32/lib/* rootfs/lib/
sudo strip rootfs/bin/sqlite3 sudo mkdir -p rootfs/dev/
sudo mknod rootfs/dev/tty1 c 4 1
sudo mknod rootfs/dev/tty2 c 4 2
sudo mknod rootfs/dev/tty3 c 4 3
sudo mknod rootfs/dev/tty4 c 4 4
sudo mknod rootfs/dev/console c 5 1
sudo mknod rootfs/dev/null c 1 3 sudo rm -rf rootfs/lib/*.a
sudo rm -rf rootfs/lib/*.la
sudo /home/pengdonglin/src/qemu/aarch32/arm-2014.05/bin/arm-none-linux-gnueabi-strip rootfs/lib/* sudo mkdir -p rootfs/tools
sudo cp ./other_tools/* rootfs/tools sudo mkdir -p rootfs/lib/modules/4\.10\.0\+ sudo dd if=/dev/zero of=ramdisk bs=1M count=150
sudo mkfs.ext4 -F ramdisk sudo mkdir -p tmpfs
sudo mount -t ext4 ramdisk ./tmpfs/ -o loop
sudo cp -raf rootfs/* tmpfs/
sudo umount tmpfs sudo gzip --best -c ramdisk > ramdisk.gz
sudo mkimage -n "ramdisk" -A arm -O linux -T ramdisk -C gzip -d ramdisk.gz ramdisk.img
如果提示空间不足的话,可以将dd命令的count参数扩大,同时不要忘了将kernel配置里的ramdisk的大小也扩大:
Device Drivers ---> [*] Block devices ---> () Default RAM disk size (kbytes)
这里将ramdisk的大小修改为了150MB,然后重新编译kernel
aarch64
#!/bin/bash sudo rm -rf rootfs
sudo rm -rf tmpfs
sudo rm -rf ramdisk* sudo mkdir rootfs
sudo cp ../busybox-1.24./_install/* rootfs/ -raf sudo mkdir -p rootfs/proc/
sudo mkdir -p rootfs/sys/
sudo mkdir -p rootfs/tmp/
sudo mkdir -p rootfs/root/
sudo mkdir -p rootfs/var/
sudo mkdir -p rootfs/mnt/ sudo cp etc rootfs/ -arf
sudo mkdir -p rootfs/lib sudo cp -arf ../gcc-linaro-aarch64-linux-gnu-4.9-2014.07_linux/aarch64-linux-gnu/libc/lib/aarch64-linux-gnu/* rootfs/lib/ sudo rm rootfs/lib/*.a
sudo ../gcc-linaro-aarch64-linux-gnu-4.9-2014.07_linux/bin/aarch64-linux-gnu-strip rootfs/lib/* version=3
sudo mkdir -p rootfs/usr
pushd rootfs/usr
sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/Python${version}/aarch64/lib .
sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/Python${version}/aarch64/include .
sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/Python${version}/aarch64/bin .
sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/Python${version}/aarch64/share .
sudo /home/pengdonglin/src/qemu/aarch64/gcc-linaro-aarch64-linux-gnu-4.9-2014.07_linux/bin/aarch64-linux-gnu-strip lib/python*
popd #sqlite
sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch64/bin/* rootfs/bin/
sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch64/include/* rootfs/include/
sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch64/lib/* rootfs/lib/
sudo strip rootfs/bin/sqlite3 sudo mkdir -p rootfs/dev/
sudo mknod rootfs/dev/tty1 c 4 1
sudo mknod rootfs/dev/tty2 c 4 2
sudo mknod rootfs/dev/tty3 c 4 3
sudo mknod rootfs/dev/tty4 c 4 4
sudo mknod rootfs/dev/console c 5 1
sudo mknod rootfs/dev/null c 1 3 sudo mkdir -p rootfs/lib/modules/4\.10\.0\+ pushd rootfs/lib
sudo ln -sf ld-2.19-2014.07.so ld-linux-aarch64.so.1
popd sudo mkdir -p rootfs/tools
sudo cp ./other_tools/* rootfs/tools sudo dd if=/dev/zero of=ramdisk bs=1M count=200
sudo mkfs.ext4 -F ramdisk sudo mkdir -p tmpfs
sudo mount -t ext4 ramdisk ./tmpfs/ -o loop
sudo cp -raf rootfs/* tmpfs/
sudo umount tmpfs sudo gzip --best -c ramdisk > ramdisk.gz
sudo mkimage -n "ramdisk" -A arm -O linux -T ramdisk -C gzip -d ramdisk.gz ramdisk.img
这里将ramdisk的大小设置为了200MB,同时修改kernel配置:
Device Drivers ---> [*] Block devices ---> () Default RAM disk size (kbytes)
然后重新编译kernel。
4、测试
在板子起来后,运行:
python3 /usr/lib/python3./test/test___all__.py
- aarch32
[root@vexpress ]# python3 /usr/lib/python3./test/test___all__.py
__phello__.foo
_bootlocale
_collections_abc
_compat_pickle
_compression
_dummy_thread
... ...
.
----------------------------------------------------------------------
Ran test in .731s
OK
- aarch64
[root@aarch64 ]# python3 /usr/lib/python3./test/test___all__.py
[ 20.347974] random: python3: uninitialized urandom read ( bytes read)
[ 21.049645] random: fast init done
__phello__.foo
_bootlocale
_collections_abc
_compat_pickle
_compression
_dummy_thread
_markupbase
... ...
.
----------------------------------------------------------------------
Ran test in .745s
OK
完