交叉编译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/bash
2 
3 ../Python-3.6.0/configure
4 
5 make -j8
6 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/bash
2 
3 export PATH=/home/pengdonglin/src/qemu/aarch32/arm-2014.05/bin:$PATH
4 
5 make -j4
6 make install
  • 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 make -j4
6 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/ -arf
18 
19 sudo cp -arf ../arm-2014.05/arm-none-linux-gnueabi/libc/lib rootfs/
20 
21 ver=3
22 sudo mkdir -p rootfs/usr
23 pushd rootfs/usr
24 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 popd
30 
31 #sqlite
32 sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch32/bin/* rootfs/bin/
33 sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch32/include/* rootfs/include/
34 sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch32/lib/* rootfs/lib/
35 sudo strip rootfs/bin/sqlite3
36 
37 sudo mkdir -p rootfs/dev/
38 sudo mknod rootfs/dev/tty1 c 4 1
39 sudo mknod rootfs/dev/tty2 c 4 2
40 sudo mknod rootfs/dev/tty3 c 4 3
41 sudo mknod rootfs/dev/tty4 c 4 4
42 sudo mknod rootfs/dev/console c 5 1
43 sudo mknod rootfs/dev/null c 1 3
44 
45 sudo rm -rf rootfs/lib/*.a
46 sudo rm -rf rootfs/lib/*.la
47 sudo /home/pengdonglin/src/qemu/aarch32/arm-2014.05/bin/arm-none-linux-gnueabi-strip rootfs/lib/*
48 
49 sudo mkdir -p rootfs/tools
50 sudo cp ./other_tools/* rootfs/tools
51 
52 sudo mkdir -p rootfs/lib/modules/4\.10\.0\+
53 
54 sudo dd if=/dev/zero of=ramdisk bs=1M count=150
55 sudo mkfs.ext4 -F ramdisk
56 
57 sudo mkdir -p tmpfs
58 sudo mount -t ext4 ramdisk ./tmpfs/  -o loop
59 sudo cp -raf rootfs/*  tmpfs/
60 sudo umount tmpfs
61 
62 sudo gzip --best -c ramdisk > ramdisk.gz
63 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/ -arf
18 sudo mkdir -p rootfs/lib
19 
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/*.a
23 sudo ../gcc-linaro-aarch64-linux-gnu-4.9-2014.07_linux/bin/aarch64-linux-gnu-strip rootfs/lib/*
24 
25 version=3
26 sudo mkdir -p rootfs/usr
27 pushd rootfs/usr
28 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 popd
34 
35 #sqlite
36 sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch64/bin/* rootfs/bin/
37 sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch64/include/* rootfs/include/
38 sudo cp -raf /home/pengdonglin/src/qemu/python_cross_compile/SQlite/aarch64/lib/* rootfs/lib/
39 sudo strip rootfs/bin/sqlite3
40 
41 sudo mkdir -p rootfs/dev/
42 sudo mknod rootfs/dev/tty1 c 4 1
43 sudo mknod rootfs/dev/tty2 c 4 2
44 sudo mknod rootfs/dev/tty3 c 4 3
45 sudo mknod rootfs/dev/tty4 c 4 4
46 sudo mknod rootfs/dev/console c 5 1
47 sudo mknod rootfs/dev/null c 1 3
48 
49 sudo mkdir -p rootfs/lib/modules/4\.10\.0\+
50 
51 pushd rootfs/lib
52 sudo ln -sf ld-2.19-2014.07.so ld-linux-aarch64.so.1
53 popd
54 
55 sudo mkdir -p rootfs/tools
56 sudo cp ./other_tools/* rootfs/tools
57 
58 sudo dd if=/dev/zero of=ramdisk bs=1M count=200
59 sudo mkfs.ext4 -F ramdisk
60 
61 sudo mkdir -p tmpfs
62 sudo mount -t ext4 ramdisk ./tmpfs/  -o loop
63 sudo cp -raf rootfs/*  tmpfs/
64 sudo umount tmpfs
65 
66 sudo gzip --best -c ramdisk > ramdisk.gz
67 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.731s
12 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_thread
10 _markupbase
11 ... ...
12 .
13 ----------------------------------------------------------------------
14 Ran 1 test in 10.745s
15 OK

 

posted @ 2017-03-23 12:26  摩斯电码  阅读(4734)  评论(0编辑  收藏