您的位置:首页 > 编程语言 > Python开发

交叉编译Python-2.7.13到ARM(aarch32)平台

2017-03-20 13:38 801 查看
作者:彭东林

邮箱:pengdonglin137@163.com

QQ:405728433

环境

主机: ubuntu14.04 64bit

开发板: qemu + vexpress-a9 (参考: http://www.cnblogs.com/pengdonglin137/p/6442583.html

工具链: arm-none-linux-gnueabi-gcc (gcc version 4.8.3 20140320)

Python版本: Python-2.7.13

参考

http://www.41443.com/HTML/Python/20151105/414154.html

http://www.cnblogs.com/tolimit/p/4519838.html?utm_source=tuicool&utm_medium=referral

正文

1、下载解压python源码

https://www.python.org/downloads/下载最新的python2系列的软件,这里我用的是Python-2.7.13

#解压
tar -xf Python-2.7.13.tar.xz

#创建python2_7_13_for_x86_64
mkdir python2_7_13_for_x86_64

#创建python2_7_13_for_arm
mkdir python2_7_13_for_arm


2、编译x86_64版本的python软件

我把编译过程写成了脚本, 进入python2_7_13_for_x86_64/目录,然后执行如下脚本:

配置 mk1_conf.sh

#!/bin/bash

../Python-2.7.13/configure --prefix=`pwd`


编译mk2_make.sh

#!/bin/bash

make -j4


安装 mk3_install.sh

#!/bin/bash

make install


3、交叉编译

交叉编译的第一步是为python源码打上交叉编译用的patch:Python-2.7.13-compile.patch.tar.gz

cd Python-2.7.13/
patch -p1 < ../python2_7_13_for_arm/Python-2.7.13-xcompile.patch


我也把编译过程放到脚本,也分为三个:

配置 mk1_conf.sh

#!/bin/bash
export PATH=/home/pengdonglin/src/qemu/aarch32/arm-2014.05/bin:$PATH
../Python-2.7.13/configure --prefix=`pwd` \
--host=arm-none-linux-gnueabi \
--build=x86_64-linux-gnu \
--enable-ipv6 \
--enable-shared \
ac_cv_file__dev_ptmx="yes" \
ac_cv_file__dev_ptc="no"


编译 mk2_make.sh

#!/bin/bash
export PATH=/home/pengdonglin/src/qemu/aarch32/arm-2014.05/bin:$PATH
make HOSTPYTHON=../python2_7_13_for_x86_64/python \
HOSTPGEN=../python2_7_13_for_x86_64/Parser/pgen \
BLDSHARED="arm-none-linux-gnueabi-gcc -shared" \
CROSS_COMPILE=arm-none-linux-gnueabi- \
CROSS_COMPILE_TARGET=yes \
HOSTARCH=arm-none-linux-gnueabi \
BUILDARCH=x86_64-linux-gnu \
-j4


安装 mk3_install.sh

#!/bin/bash
export PATH=/home/pengdonglin/src/qemu/aarch32/arm-2014.05/bin:$PATH
make install HOSTPYTHON=../python2_7_13_for_x86_64/python \
BLDSHARED="arm-none-linux-gnueabi-gcc -shared" \
CROSS_COMPILE=arm-none-linux-gnueabi- \
CROSS_COMPILE_TARGET=yes \
prefix=`pwd`


4、重新制作ramdisk镜像

参考博文:用Qemu搭建aarch32学习环境

修改mk_ramdisk.sh如下:

#!/bin/bash
sudo rm -rf rootfs
sudo rm -rf tmpfs
sudo rm -rf ramdisk*
sudo mkdir rootfs
sudo cp ../busybox-1.24.2/_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/
sudo rm -rf rootfs/lib/*.a
sudo /home/pengdonglin/src/qemu/aarch32/arm-2014.05/bin/arm-none-linux-gnueabi-strip rootfs/lib/*
# 拷贝python相关的文件到根文件系统中
sudo mkdir -p rootfs/usr
pushd rootfs/usr
sudo cp  -raf /home/pengdonglin/src/qemu/python_cross_compile/python2_7_13_for_arm/lib .
sudo cp  -raf /home/pengdonglin/src/qemu/python_cross_compile/python2_7_13_for_arm/include .
sudo cp  -raf /home/pengdonglin/src/qemu/python_cross_compile/python2_7_13_for_arm/bin .
sudo cp  -raf /home/pengdonglin/src/qemu/python_cross_compile/python2_7_13_for_arm/share .
sudo /home/pengdonglin/src/qemu/aarch32/arm-2014.05/bin/arm-none-linux-gnueabi-strip lib/python*
popd
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\+
sudo mkdir -p rootfs/tools
sudo cp ./other_tools/* rootfs/tools
# 将ramdisk的大小扩展为100MB
sudo dd if=/dev/zero of=ramdisk bs=1M count=100
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


这里需要注意:

1、将刚才交叉编译python所得的文件拷贝到rootfs/usr下面: bin、lib、include和share

2、由于Python的lib目录占用了很大空间,有70MB左右,所以这里我们把ramdisk的大小设置为100MB

3、此外,qemu-system-arm运行的,可以给-m设置较大的物理内存,这里我设置的是1GB

5、修改kernel配置

修改ramdisk的大小设置, 这里我设置的是100MB:

Device Drivers  --->

[*] Block devices  --->

(102400) Default RAM disk size (kbytes)


6、测试

制作好ramdisk以及编译出新的kernel后,运行系统:

sudo qemu-system-arm \
-M vexpress-a9 \
-m 1024M \
-smp 2 \
-kernel ./linux-4.10/out_aarch32/arch/arm/boot/zImage \
-nographic \
-append "root=/dev/ram0 rw rootfstype=ext4 console=ttyAMA0 init=/linuxrc ignore_loglevel" \
-initrd ./rootfs/ramdisk.img \
-net nic,vlan=0 -net tap,vlan=0,ifname=tap0 \
-dtb ./linux-4.10/out_aarch32/arch/arm/boot/dts/vexpress-v2p-ca9.dtb


下面是启动log:

$./run.sh
sudo tunctl -u root -t tap0
TUNSETIFF: Device or resource busy
sudo ifconfig tap0 0.0.0.0 promisc up
sudo brctl addif br0 tap0
brctl show
bridge name    bridge id        STP enabled    interfaces
br0        8000.480fcf3ace87    no        eth0
tap0
docker0        8000.02423772cc85    no
[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Linux version 4.10.0+ (pengdonglin@pengdonglin-HP) (gcc version 4.8.3 20140320 (prerelease) (Sourcery CodeBench Lite 2014.05-29) ) #10 SMP Mon Mar 20 11:31:00 CST 2017
[    0.000000] CPU: ARMv7 Processor [410fc090] revision 0 (ARMv7), cr=10c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache
[    0.000000] OF: fdt:Machine model: V2P-CA9
[    0.000000] debug: ignoring loglevel setting.
[    0.000000] Memory policy: Data cache writealloc
[    0.000000] On node 0 totalpages: 262144
[    0.000000] free_area_init_node: node 0, pgdat c0a637c0, node_mem_map ef7fa000
[    0.000000]   Normal zone: 1536 pages used for memmap
[    0.000000]   Normal zone: 0 pages reserved
[    0.000000]   Normal zone: 196608 pages, LIFO batch:31
[    0.000000]   HighMem zone: 65536 pages, LIFO batch:15
[    0.000000] percpu: Embedded 14 pages/cpu @ef7b5000 s27648 r8192 d21504 u57344
[    0.000000] pcpu-alloc: s27648 r8192 d21504 u57344 alloc=14*4096
[    0.000000] pcpu-alloc: [0] 0 [0] 1 [0] 2 [0] 3
[    0.000000] Built 1 zonelists in Zone order, mobility grouping on.  Total pages: 260608
[    0.000000] Kernel command line: root=/dev/ram0 rw rootfstype=ext4 console=ttyAMA0 init=/linuxrc ignore_loglevel
[    0.000000] log_buf_len individual max cpu contribution: 4096 bytes
[    0.000000] log_buf_len total cpu_extra contributions: 12288 bytes
[    0.000000] log_buf_len min size: 16384 bytes
[    0.000000] log_buf_len: 32768 bytes
[    0.000000] early log buf free: 14860(90%)
[    0.000000] PID hash table entries: 4096 (order: 2, 16384 bytes)
[    0.000000] Dentry cache hash table entries: 131072 (order: 7, 524288 bytes)
[    0.000000] Inode-cache hash table entries: 65536 (order: 6, 262144 bytes)
[    0.000000] Memory: 1007212K/1048576K available (6144K kernel code, 453K rwdata, 1440K rodata, 1024K init, 191K bss, 41364K reserved, 0K cma-reserved, 262144K highmem)
[    0.000000] Virtual kernel memory layout:
[    0.000000]     vector  : 0xffff0000 - 0xffff1000   (   4 kB)
[    0.000000]     fixmap  : 0xffc00000 - 0xfff00000   (3072 kB)
[    0.000000]     vmalloc : 0xf0800000 - 0xff800000   ( 240 MB)
[    0.000000]     lowmem  : 0xc0000000 - 0xf0000000   ( 768 MB)
[    0.000000]     pkmap   : 0xbfe00000 - 0xc0000000   (   2 MB)
[    0.000000]     modules : 0xbf000000 - 0xbfe00000   (  14 MB)
[    0.000000]       .text : 0xc0008000 - 0xc0700000   (7136 kB)
[    0.000000]       .init : 0xc0900000 - 0xc0a00000   (1024 kB)
[    0.000000]       .data : 0xc0a00000 - 0xc0a71784   ( 454 kB)
[    0.000000]        .bss : 0xc0a73000 - 0xc0aa2c4c   ( 192 kB)
[    0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=4, Nodes=1
[    0.000000] Hierarchical RCU implementation.
[    0.000000]     Build-time adjustment of leaf fanout to 32.
[    0.000000]     RCU restricting CPUs from NR_CPUS=8 to nr_cpu_ids=4.
[    0.000000] RCU: Adjusting geometry for rcu_fanout_leaf=32, nr_cpu_ids=4
[    0.000000] NR_IRQS:16 nr_irqs:16 16
[    0.000000] L2C: platform modifies aux control register: 0x02020000 -> 0x02420000
[    0.000000] L2C: DT/platform modifies aux control register: 0x02020000 -> 0x02420000
[    0.000000] L2C-310 enabling early BRESP for Cortex-A9
[    0.000000] L2C-310 full line of zeros enabled for Cortex-A9
[    0.000000] L2C-310 dynamic clock gating disabled, standby mode disabled
[    0.000000] L2C-310 cache controller enabled, 8 ways, 128 kB
[    0.000000] L2C-310: CACHE_ID 0x410000c8, AUX_CTRL 0x46420001
[    0.000000] smp_twd: clock not found -2
[    0.000206] sched_clock: 32 bits at 24MHz, resolution 41ns, wraps every 89478484971ns
[    0.002899] clocksource: arm,sp804: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 1911260446275 ns
[    0.003447] Failed to initialize '/smb@04000000/motherboard/iofpga@7,00000000/timer@12000': -22
[    0.006792] Console: colour dummy device 80x30
[    0.007168] Calibrating local timer... 94.56MHz.
[    0.063191] Calibrating delay loop... 869.99 BogoMIPS (lpj=4349952)
[    0.148244] pid_max: default: 32768 minimum: 301
[    0.149235] Mount-cache hash table entries: 2048 (order: 1, 8192 bytes)
[    0.149279] Mountpoint-cache hash table entries: 2048 (order: 1, 8192 bytes)
[    0.158258] CPU: Testing write buffer coherency: ok
[    0.158718] ftrace: allocating 20771 entries in 61 pages
[    0.580570] CPU0: thread -1, cpu 0, socket 0, mpidr 80000000
[    0.584830] Setting up static identity map for 0x60100000 - 0x60100058
[    0.591663] smp: Bringing up secondary CPUs ...
[    0.677810] CPU1: thread -1, cpu 1, socket 0, mpidr 80000001
[    1.623456] CPU2: failed to boot: -38
[    2.569146] CPU3: failed to boot: -38
[    2.569341] smp: Brought up 1 node, 2 CPUs
[    2.569417] SMP: Total of 2 processors activated (1739.98 BogoMIPS).
[    2.569500] CPU: All CPU(s) started in SVC mode.
[    2.599822] devtmpfs: initialized
[    2.617472] VFP support v0.3: implementor 41 architecture 3 part 30 variant 9 rev 0
[    2.631508] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns
[    2.632028] futex hash table entries: 1024 (order: 4, 65536 bytes)
[    2.671232] NET: Registered protocol family 16
[    2.675992] DMA: preallocated 256 KiB pool for atomic coherent allocations
[    2.853307] cpuidle: using governor ladder
[    2.853708] hw-breakpoint: debug architecture 0x4 unsupported.
[    2.854172] Serial: AMBA PL011 UART driver
[    2.860429] OF: amba_device_add() failed (-19) for /memory-controller@100e0000
[    2.862876] OF: amba_device_add() failed (-19) for /memory-controller@100e1000
[    2.863329] OF: amba_device_add() failed (-19) for /watchdog@100e5000
[    2.865275] irq: type mismatch, failed to map hwirq-75 for /interrupt-controller@1e001000!
[    2.877129] 10009000.uart: ttyAMA0 at MMIO 0x10009000 (irq = 38, base_baud = 0) is a PL011 rev1
[    2.889012] console [ttyAMA0] enabled
[    2.893012] 1000a000.uart: ttyAMA1 at MMIO 0x1000a000 (irq = 39, base_baud = 0) is a PL011 rev1
[    2.895140] 1000b000.uart: ttyAMA2 at MMIO 0x1000b000 (irq = 40, base_baud = 0) is a PL011 rev1
[    2.896620] 1000c000.uart: ttyAMA3 at MMIO 0x1000c000 (irq = 41, base_baud = 0) is a PL011 rev1
[    2.897695] OF: amba_device_add() failed (-19) for /smb@04000000/motherboard/iofpga@7,00000000/wdt@0f000
[    2.979213] SCSI subsystem initialized
[    2.980067] libata version 3.00 loaded.
[    2.981102] usbcore: registered new interface driver usbfs
[    2.981415] usbcore: registered new interface driver hub
[    2.981690] usbcore: registered new device driver usb
[    2.988362] Advanced Linux Sound Architecture Driver Initialized.
[    3.011566] clocksource: Switched to clocksource arm,sp804
[    3.159190] NET: Registered protocol family 2
[    3.164055] TCP established hash table entries: 8192 (order: 3, 32768 bytes)
[    3.164307] TCP bind hash table entries: 8192 (order: 4, 65536 bytes)
[    3.164553] TCP: Hash tables configured (established 8192 bind 8192)
[    3.166386] UDP hash table entries: 512 (order: 2, 16384 bytes)
[    3.166801] UDP-Lite hash table entries: 512 (order: 2, 16384 bytes)
[    3.167985] NET: Registered protocol family 1
[    3.171676] RPC: Registered named UNIX socket transport module.
[    3.171948] RPC: Registered udp transport module.
[    3.172020] RPC: Registered tcp transport module.
[    3.172115] RPC: Registered tcp NFSv4.1 backchannel transport module.
[    3.177825] Trying to unpack rootfs image as initramfs...
[    3.184610] rootfs image is not initramfs (no cpio magic); looks like an initrd
[    3.416590] Freeing initrd memory: 22120K
[    3.420686] hw perfevents: enabled with armv7_cortex_a9 PMU driver, 1 counters available
[    3.430377] workingset: timestamp_bits=30 max_order=18 bucket_order=0
[    3.457911] squashfs: version 4.0 (2009/01/31) Phillip Lougher
[    3.463134] jffs2: version 2.2. (NAND) © 2001-2006 Red Hat, Inc.
[    3.464581] 9p: Installing v9fs 9p2000 file system support
[    3.470989] bounce: pool size: 64 pages
[    3.471215] io scheduler noop registered (default)
[    3.475569] clcd-pl11x 10020000.clcd: PL111 designer 41 rev2 at 0x10020000
[    3.483346] clcd-pl11x 10020000.clcd: /clcd@10020000 hardware, 1024x768@59 display
[    3.602322] Console: switching to colour frame buffer device 128x48
[    3.617254] clcd-pl11x 1001f000.clcd: PL111 designer 41 rev2 at 0x1001f000
[    3.619006] clcd-pl11x 1001f000.clcd: /smb@04000000/motherboard/iofpga@7,00000000/clcd@1f000 hardware, 640x480@59 display
[    3.984710] brd: module loaded
[    3.991353] 40000000.flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
[    3.991791] Intel/Sharp Extended Query Table at 0x0031
[    3.992447] Using buffer write method
[    3.992792] erase region 0: offset=0x0,size=0x80000,blocks=128
[    3.994929] 40000000.flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
[    3.995125] Intel/Sharp Extended Query Table at 0x0031
[    3.995600] Using buffer write method
[    3.995708] erase region 0: offset=0x0,size=0x80000,blocks=128
[    3.995876] Concatenating MTD devices:
[    3.995986] (0): "40000000.flash"
[    3.996076] (1): "40000000.flash"
[    3.996153] into device "40000000.flash"
[    4.011057] libphy: Fixed MDIO Bus: probed
[    4.068484] libphy: smsc911x-mdio: probed
[    4.071230] smsc911x 4e000000.ethernet eth0: MAC Address: 52:54:00:12:34:56
[    4.179313] isp1760 4f000000.usb: bus width: 32, oc: digital
[    4.181070] isp1760 4f000000.usb: NXP ISP1760 USB Host Controller
[    4.181548] isp1760 4f000000.usb: new USB bus registered, assigned bus number 1
[    4.182418] isp1760 4f000000.usb: Scratch test failed.
[    4.182705] isp1760 4f000000.usb: can't setup: -19
[    4.183183] isp1760 4f000000.usb: USB bus 1 deregistered
[    4.185634] usbcore: registered new interface driver usb-storage
[    4.192487] mousedev: PS/2 mouse device common for all mice
[    4.201100] rtc-pl031 10017000.rtc: rtc core: registered pl031 as rtc0
[    4.216680] mmci-pl18x 10005000.mmci: Got CD GPIO
[    4.216979] mmci-pl18x 10005000.mmci: Got WP GPIO
[    4.218707] mmci-pl18x 10005000.mmci: mmc0: PL181 manf 41 rev0 at 0x10005000 irq 34,35 (pio)
[    4.272360] ledtrig-cpu: registered to indicate activity on CPUs
[    4.279939] usbcore: registered new interface driver usbhid
[    4.280124] usbhid: USB HID core driver
[    4.321426] input: AT Raw Set 2 keyboard as /devices/platform/smb@04000000/smb@04000000:motherboard/smb@04000000:motherboard:iofpga@7,00000000/10006000.kmi/serio0/input/input0
[    4.326835] aaci-pl041 10004000.aaci: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 33
[    4.326992] aaci-pl041 10004000.aaci: FIFO 512 entries
[    4.327894] oprofile: using arm/armv7-ca9
[    4.331342] NET: Registered protocol family 17
[    4.331955] 9pnet: Installing 9P2000 support
[    4.332582] Registering SWP/SWPB emulation handler
[    4.348427] rtc-pl031 10017000.rtc: setting system clock to 2017-03-20 03:31:48 UTC (1489980708)
[    4.349574] ALSA device list:
[    4.349669]   #0: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 33
[    4.999294] input: ImExPS/2 Generic Explorer Mouse as /devices/platform/smb@04000000/smb@04000000:motherboard/smb@04000000:motherboard:iofpga@7,00000000/10007000.kmi/serio1/input/input2
[    5.014045] RAMDISK: gzip image found at block 0
[   11.157823] EXT4-fs (ram0): mounted filesystem with ordered data mode. Opts: (null)
[   11.158866] VFS: Mounted root (ext4 filesystem) on device 1:0.
[   11.189561] Freeing unused kernel memory: 1024K
[   12.209586] Generic PHY 4e000000.etherne:01: attached PHY driver [Generic PHY] (mii_bus:phy_addr=4e000000.etherne:01, irq=-1)
[   12.236870] smsc911x 4e000000.ethernet eth0: SMSC911x/921x identified at 0xf1420000, IRQ: 31
Please press Enter to activate this console.
[root@vexpress ]#
[root@vexpress ]# [   31.810292] random: fast init done
[root@vexpress ]#
[root@vexpress ]#
[root@vexpress ]# python
Python 2.7.13 (default, Mar 20 2017, 11:20:45)
[GCC 4.8.3 20140320 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> for i in range(100): print "hello world"
...
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
>>> [  236.572628] random: crng init done


完。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: