首页 > 代码库 > 交叉编译Python-3.6.0到aarch64/aarch32 —— 支持sqlite3
交叉编译Python-3.6.0到aarch64/aarch32 —— 支持sqlite3
参考
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,下面是命令:
1 #!/bin/bash2 3 ../Python-3.6.0/configure4 5 make -j86 sudo make install
默认,Python3.6.0会被安装到/usr/local下面。
二、交叉编译
1、配置 mk1_conf.sh
aarch32
1 #!/bin/bash 2 3 export PATH=/home/pengdonglin/src/qemu/aarch32/arm-2014.05/bin:$PATH 4 5 ../Python-3.6.0/configure 6 --host=arm-none-linux-gnueabi 7 --build=armv7 8 --prefix=`pwd` 9 --enable-ipv6 10 --enable-shared 11 ac_cv_file__dev_ptmx="yes" 12 ac_cv_file__dev_ptc="no" 13 LDFLAGS="-L/home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch32/lib" 14 CPPFLAGS="-I/home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch32/include"
其中--build表示编译出来的程序运行的CPU架构,--host是交叉编译工具链。这里我已经吧sqlite3的lib和include添加上了,所以生成的python3是支持sqlite3的。
aarch64
1 #!/bin/bash 2 3 export PATH=/home/pengdonglin/src/qemu/aarch64/gcc-linaro-aarch64-linux-gnu-4.9-2014.07_linux/bin:$PATH 4 5 ../Python-3.6.0/configure 6 --host=aarch64-linux-gnu 7 --build=aarch64 8 --prefix=`pwd` 9 --enable-ipv6 10 --enable-shared 11 ac_cv_file__dev_ptmx="yes" 12 ac_cv_file__dev_ptc="no" 13 LDFLAGS="-L/home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch64/lib" 14 CPPFLAGS="-I/home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch64/include"
只需要替换运行的CPU架构为aarch64,然后设置对应的工具链即可,别忘了还有sqlite3的相关路径的修改。
2、编译和安装 mk2_make.sh
这里没有什么好说的,下面是脚本内容:
aarch32
1 #!/bin/bash2 3 export PATH=/home/pengdonglin/src/qemu/aarch32/arm-2014.05/bin:$PATH4 5 make -j46 make install
- aarch64
1 #!/bin/bash2 3 export PATH=/home/pengdonglin/src/qemu/aarch64/gcc-linaro-aarch64-linux-gnu-4.9-2014.07_linux/bin:$PATH4 5 make -j46 make install
3、制作ramdisk
为了便于在python2和python3之间的切换,适当修改mk_ramdisk.sh如下:
aarch32
1 #!/bin/bash 2 3 sudo rm -rf rootfs 4 sudo rm -rf tmpfs 5 sudo rm -rf ramdisk* 6 7 sudo mkdir rootfs 8 sudo cp ../busybox-1.24.2/_install/* rootfs/ -raf 9 10 sudo mkdir -p rootfs/proc/11 sudo mkdir -p rootfs/sys/12 sudo mkdir -p rootfs/tmp/13 sudo mkdir -p rootfs/root/14 sudo mkdir -p rootfs/var/15 sudo mkdir -p rootfs/mnt/16 17 sudo cp etc rootfs/ -arf18 19 sudo cp -arf ../arm-2014.05/arm-none-linux-gnueabi/libc/lib rootfs/20 21 ver=322 sudo mkdir -p rootfs/usr23 pushd rootfs/usr24 sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/Python${ver}/aarch32/lib .25 sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/Python${ver}/aarch32/include .26 sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/Python${ver}/aarch32/bin .27 sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/Python${ver}/aarch32/share .28 sudo /home/pengdonglin/src/qemu/aarch32/arm-2014.05/bin/arm-none-linux-gnueabi-strip lib/python*29 popd30 31 #sqlite32 sudo cp /home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch32/bin/* rootfs/bin/33 sudo cp /home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch32/include/* rootfs/include/34 sudo cp /home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch32/lib/* rootfs/lib/35 sudo strip /rootfs/bin/sqlite336 37 sudo mkdir -p rootfs/dev/38 sudo mknod rootfs/dev/tty1 c 4 139 sudo mknod rootfs/dev/tty2 c 4 240 sudo mknod rootfs/dev/tty3 c 4 341 sudo mknod rootfs/dev/tty4 c 4 442 sudo mknod rootfs/dev/console c 5 143 sudo mknod rootfs/dev/null c 1 344 45 sudo rm -rf rootfs/lib/*.a46 sudo rm -rf rootfs/lib/*.la47 sudo /home/pengdonglin/src/qemu/aarch32/arm-2014.05/bin/arm-none-linux-gnueabi-strip rootfs/lib/*48 49 sudo mkdir -p rootfs/tools50 sudo cp ./other_tools/* rootfs/tools51 52 sudo mkdir -p rootfs/lib/modules/4\.10\.0\+53 54 sudo dd if=/dev/zero of=ramdisk bs=1M count=15055 sudo mkfs.ext4 -F ramdisk56 57 sudo mkdir -p tmpfs58 sudo mount -t ext4 ramdisk ./tmpfs/ -o loop59 sudo cp -raf rootfs/* tmpfs/60 sudo umount tmpfs61 62 sudo gzip --best -c ramdisk > ramdisk.gz63 sudo mkimage -n "ramdisk" -A arm -O linux -T ramdisk -C gzip -d ramdisk.gz ramdisk.img
如果提示空间不足的话,可以将dd命令的count参数扩大,同时不要忘了将kernel配置里的ramdisk的大小也扩大:
1 Device Drivers --->2 3 [*] Block devices --->4 5 (153600) Default RAM disk size (kbytes)
这里将ramdisk的大小修改为了150MB,然后重新编译kernel
aarch64
1 #!/bin/bash 2 3 sudo rm -rf rootfs 4 sudo rm -rf tmpfs 5 sudo rm -rf ramdisk* 6 7 sudo mkdir rootfs 8 sudo cp ../busybox-1.24.2/_install/* rootfs/ -raf 9 10 sudo mkdir -p rootfs/proc/11 sudo mkdir -p rootfs/sys/12 sudo mkdir -p rootfs/tmp/13 sudo mkdir -p rootfs/root/14 sudo mkdir -p rootfs/var/15 sudo mkdir -p rootfs/mnt/16 17 sudo cp etc rootfs/ -arf18 sudo mkdir -p rootfs/lib19 20 sudo cp -arf ../gcc-linaro-aarch64-linux-gnu-4.9-2014.07_linux/aarch64-linux-gnu/libc/lib/aarch64-linux-gnu/* rootfs/lib/21 22 sudo rm rootfs/lib/*.a23 sudo ../gcc-linaro-aarch64-linux-gnu-4.9-2014.07_linux/bin/aarch64-linux-gnu-strip rootfs/lib/*24 25 version=326 sudo mkdir -p rootfs/usr27 pushd rootfs/usr28 sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/Python${version}/aarch64/lib .29 sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/Python${version}/aarch64/include .30 sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/Python${version}/aarch64/bin .31 sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/Python${version}/aarch64/share .32 sudo /home/pengdonglin/src/qemu/aarch64/gcc-linaro-aarch64-linux-gnu-4.9-2014.07_linux/bin/aarch64-linux-gnu-strip lib/python*33 popd34 35 #sqlite36 sudo cp /home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch64/bin/* rootfs/bin/37 sudo cp /home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch64/include/* rootfs/include/38 sudo cp /home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch64/lib/* rootfs/lib/39 sudo strip /rootfs/bin/sqlite340 41 sudo mkdir -p rootfs/dev/42 sudo mknod rootfs/dev/tty1 c 4 143 sudo mknod rootfs/dev/tty2 c 4 244 sudo mknod rootfs/dev/tty3 c 4 345 sudo mknod rootfs/dev/tty4 c 4 446 sudo mknod rootfs/dev/console c 5 147 sudo mknod rootfs/dev/null c 1 348 49 sudo mkdir -p rootfs/lib/modules/4\.10\.0\+50 51 pushd rootfs/lib52 sudo ln -sf ld-2.19-2014.07.so ld-linux-aarch64.so.153 popd54 55 sudo mkdir -p rootfs/tools56 sudo cp ./other_tools/* rootfs/tools57 58 sudo dd if=/dev/zero of=ramdisk bs=1M count=20059 sudo mkfs.ext4 -F ramdisk60 61 sudo mkdir -p tmpfs62 sudo mount -t ext4 ramdisk ./tmpfs/ -o loop63 sudo cp -raf rootfs/* tmpfs/64 sudo umount tmpfs65 66 sudo gzip --best -c ramdisk > ramdisk.gz67 sudo mkimage -n "ramdisk" -A arm -O linux -T ramdisk -C gzip -d ramdisk.gz ramdisk.img
这里将ramdisk的大小设置为了200MB,同时修改kernel配置:
1 Device Drivers --->2 3 [*] Block devices --->4 5 (204800) Default RAM disk size (kbytes)
然后重新编译kernel。
4、测试
在板子起来后,运行:
python3 /usr/lib/python3.6/test/test___all__.py
- aarch32
1 [root@vexpress ]# python3 /usr/lib/python3.6/test/test___all__.py 2 __phello__.foo 3 _bootlocale 4 _collections_abc 5 _compat_pickle 6 _compression 7 _dummy_thread 8 ... ... 9 .10 ----------------------------------------------------------------------11 Ran 1 test in 15.731s12 OK
- aarch64
1 [root@aarch64 ]# python3 /usr/lib/python3.6/test/test___all__.py 2 [ 20.347974] random: python3: uninitialized urandom read (24 bytes read) 3 [ 21.049645] random: fast init done 4 __phello__.foo 5 _bootlocale 6 _collections_abc 7 _compat_pickle 8 _compression 9 _dummy_thread10 _markupbase11 ... ...12 .13 ----------------------------------------------------------------------14 Ran 1 test in 10.745s15 OK
完
交叉编译Python-3.6.0到aarch64/aarch32 —— 支持sqlite3