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

时间:2024-04-11 16:36:53

作者:彭东林

邮箱: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..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./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./
patch -p1 < ../python2_7_13_for_arm/Python-2.7.-xcompile.patch

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

  • 配置 mk1_conf.sh

 #!/bin/bash
export PATH=/home/pengdonglin/src/qemu/aarch32/arm-2014.05/bin:$PATH
../Python-2.7./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./_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  --->

                 () Default RAM disk size (kbytes) 

6、测试

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

 sudo qemu-system-arm \
-M vexpress-a9 \
-m 1024M \
-smp \
-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= -net tap,vlan=,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 .480fcf3ace87 no eth0
tap0
docker0 .02423772cc85 no
[ 0.000000] Booting Linux on physical CPU 0x0
[ 0.000000] Linux version 4.10.+ (pengdonglin@pengdonglin-HP) (gcc version 4.8. (prerelease) (Sourcery CodeBench Lite 2014.05-) ) # SMP Mon Mar :: CST
[ 0.000000] CPU: ARMv7 Processor [410fc090] revision (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 totalpages:
[ 0.000000] free_area_init_node: node , pgdat c0a637c0, node_mem_map ef7fa000
[ 0.000000] Normal zone: pages used for memmap
[ 0.000000] Normal zone: pages reserved
[ 0.000000] Normal zone: pages, LIFO batch:
[ 0.000000] HighMem zone: pages, LIFO batch:
[ 0.000000] percpu: Embedded pages/cpu @ef7b5000 s27648 r8192 d21504 u57344
[ 0.000000] pcpu-alloc: s27648 r8192 d21504 u57344 alloc=*
[ 0.000000] pcpu-alloc: [] [] [] []
[ 0.000000] Built zonelists in Zone order, mobility grouping on. Total pages:
[ 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: bytes
[ 0.000000] log_buf_len total cpu_extra contributions: bytes
[ 0.000000] log_buf_len min size: bytes
[ 0.000000] log_buf_len: bytes
[ 0.000000] early log buf free: (%)
[ 0.000000] PID hash table entries: (order: , bytes)
[ 0.000000] Dentry cache hash table entries: (order: , bytes)
[ 0.000000] Inode-cache hash table entries: (order: , 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 ( kB)
[ 0.000000] fixmap : 0xffc00000 - 0xfff00000 ( kB)
[ 0.000000] vmalloc : 0xf0800000 - 0xff800000 ( MB)
[ 0.000000] lowmem : 0xc0000000 - 0xf0000000 ( MB)
[ 0.000000] pkmap : 0xbfe00000 - 0xc0000000 ( MB)
[ 0.000000] modules : 0xbf000000 - 0xbfe00000 ( MB)
[ 0.000000] .text : 0xc0008000 - 0xc0700000 ( kB)
[ 0.000000] .init : 0xc0900000 - 0xc0a00000 ( kB)
[ 0.000000] .data : 0xc0a00000 - 0xc0a71784 ( kB)
[ 0.000000] .bss : 0xc0a73000 - 0xc0aa2c4c ( kB)
[ 0.000000] SLUB: HWalign=, Order=-, MinObjects=, CPUs=, Nodes=
[ 0.000000] Hierarchical RCU implementation.
[ 0.000000] Build-time adjustment of leaf fanout to .
[ 0.000000] RCU restricting CPUs from NR_CPUS= to nr_cpu_ids=.
[ 0.000000] RCU: Adjusting geometry for rcu_fanout_leaf=, nr_cpu_ids=
[ 0.000000] NR_IRQS: nr_irqs:
[ 0.000000] L2C: platform modifies aux control register: 0x02020000 -> 0x02420000
[ 0.000000] L2C: DT/platform modifies aux control register: 0x02020000 -> 0x02420000
[ 0.000000] L2C- enabling early BRESP for Cortex-A9
[ 0.000000] L2C- full line of zeros enabled for Cortex-A9
[ 0.000000] L2C- dynamic clock gating disabled, standby mode disabled
[ 0.000000] L2C- cache controller enabled, ways, kB
[ 0.000000] L2C-: CACHE_ID 0x410000c8, AUX_CTRL 0x46420001
[ 0.000000] smp_twd: clock not found -
[ 0.000206] sched_clock: bits at 24MHz, resolution 41ns, wraps every 89478484971ns
[ 0.002899] clocksource: arm,sp804: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: ns
[ 0.003447] Failed to initialize '/smb@04000000/motherboard/iofpga@7,00000000/timer@12000': -
[ 0.006792] Console: colour dummy device 80x30
[ 0.007168] Calibrating local timer... .56MHz.
[ 0.063191] Calibrating delay loop... 869.99 BogoMIPS (lpj=)
[ 0.148244] pid_max: default: minimum:
[ 0.149235] Mount-cache hash table entries: (order: , bytes)
[ 0.149279] Mountpoint-cache hash table entries: (order: , bytes)
[ 0.158258] CPU: Testing write buffer coherency: ok
[ 0.158718] ftrace: allocating entries in pages
[ 0.580570] CPU0: thread -, cpu , socket , mpidr
[ 0.584830] Setting up static identity map for 0x60100000 - 0x60100058
[ 0.591663] smp: Bringing up secondary CPUs ...
[ 0.677810] CPU1: thread -, cpu , socket , mpidr
[ 1.623456] CPU2: failed to boot: -
[ 2.569146] CPU3: failed to boot: -
[ 2.569341] smp: Brought up node, CPUs
[ 2.569417] SMP: Total of processors activated (1739.98 BogoMIPS).
[ 2.569500] CPU: All CPU(s) started in SVC mode.
[ 2.599822] devtmpfs: initialized
[ 2.617472] VFP support v0.: implementor architecture part variant rev
[ 2.631508] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: ns
[ 2.632028] futex hash table entries: (order: , bytes)
[ 2.671232] NET: Registered protocol family
[ 2.675992] DMA: preallocated 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 (-) for /memory-controller@100e0000
[ 2.862876] OF: amba_device_add() failed (-) for /memory-controller@100e1000
[ 2.863329] OF: amba_device_add() failed (-) for /watchdog@100e5000
[ 2.865275] irq: type mismatch, failed to map hwirq- for /interrupt-controller@1e001000!
[ 2.877129] .uart: ttyAMA0 at MMIO 0x10009000 (irq = , base_baud = ) is a PL011 rev1
[ 2.889012] console [ttyAMA0] enabled
[ 2.893012] 1000a000.uart: ttyAMA1 at MMIO 0x1000a000 (irq = , base_baud = ) is a PL011 rev1
[ 2.895140] 1000b000.uart: ttyAMA2 at MMIO 0x1000b000 (irq = , base_baud = ) is a PL011 rev1
[ 2.896620] 1000c000.uart: ttyAMA3 at MMIO 0x1000c000 (irq = , base_baud = ) is a PL011 rev1
[ 2.897695] OF: amba_device_add() failed (-) for /smb@/motherboard/iofpga@,/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
[ 3.164055] TCP established hash table entries: (order: , bytes)
[ 3.164307] TCP bind hash table entries: (order: , bytes)
[ 3.164553] TCP: Hash tables configured (established bind )
[ 3.166386] UDP hash table entries: (order: , bytes)
[ 3.166801] UDP-Lite hash table entries: (order: , bytes)
[ 3.167985] NET: Registered protocol family
[ 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. 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, counters available
[ 3.430377] workingset: timestamp_bits= max_order= bucket_order=
[ 3.457911] squashfs: version 4.0 (//) Phillip Lougher
[ 3.463134] jffs2: version 2.2. (NAND) © - Red Hat, Inc.
[ 3.464581] 9p: Installing v9fs 9p2000 file system support
[ 3.470989] bounce: pool size: pages
[ 3.471215] io scheduler noop registered (default)
[ 3.475569] clcd-pl11x .clcd: PL111 designer rev2 at 0x10020000
[ 3.483346] clcd-pl11x .clcd: /clcd@ hardware, 1024x768@ display
[ 3.602322] Console: switching to colour frame buffer device 128x48
[ 3.617254] clcd-pl11x 1001f000.clcd: PL111 designer rev2 at 0x1001f000
[ 3.619006] clcd-pl11x 1001f000.clcd: /smb@/motherboard/iofpga@,/clcd@1f000 hardware, 640x480@ display
[ 3.984710] brd: module loaded
[ 3.991353] .flash: Found x16 devices at 0x0 in -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 : offset=0x0,size=0x80000,blocks=
[ 3.994929] .flash: Found x16 devices at 0x0 in -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 : offset=0x0,size=0x80000,blocks=
[ 3.995876] Concatenating MTD devices:
[ 3.995986] (): "40000000.flash"
[ 3.996076] (): "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: :::::
[ 4.179313] isp1760 4f000000.usb: bus width: , oc: digital
[ 4.181070] isp1760 4f000000.usb: NXP ISP1760 USB Host Controller
[ 4.181548] isp1760 4f000000.usb: new USB bus registered, assigned bus number
[ 4.182418] isp1760 4f000000.usb: Scratch test failed.
[ 4.182705] isp1760 4f000000.usb: can't setup: -19
[ 4.183183] isp1760 4f000000.usb: USB bus deregistered
[ 4.185634] usbcore: registered new interface driver usb-storage
[ 4.192487] mousedev: PS/ mouse device common for all mice
[ 4.201100] rtc-pl031 .rtc: rtc core: registered pl031 as rtc0
[ 4.216680] mmci-pl18x .mmci: Got CD GPIO
[ 4.216979] mmci-pl18x .mmci: Got WP GPIO
[ 4.218707] mmci-pl18x .mmci: mmc0: PL181 manf rev0 at 0x10005000 irq , (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 keyboard as /devices/platform/smb@/smb@:motherboard/smb@:motherboard:iofpga@,/.kmi/serio0/input/input0
[ 4.326835] aaci-pl041 .aaci: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 33
[ 4.326992] aaci-pl041 .aaci: FIFO entries
[ 4.327894] oprofile: using arm/armv7-ca9
[ 4.331342] NET: Registered protocol family
[ 4.331955] 9pnet: Installing 9P2000 support
[ 4.332582] Registering SWP/SWPB emulation handler
[ 4.348427] rtc-pl031 .rtc: setting system clock to -- :: UTC ()
[ 4.349574] ALSA device list:
[ 4.349669] #: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 33
[ 4.999294] input: ImExPS/ Generic Explorer Mouse as /devices/platform/smb@/smb@:motherboard/smb@:motherboard:iofpga@,/.kmi/serio1/input/input2
[ 5.014045] RAMDISK: gzip image found at block
[ 11.157823] EXT4-fs (ram0): mounted filesystem with ordered data mode. Opts: (null)
[ 11.158866] VFS: Mounted root (ext4 filesystem) on device :.
[ 11.189561] Freeing unused kernel memory: 1024K
[ 12.209586] Generic PHY 4e000000.etherne:: attached PHY driver [Generic PHY] (mii_bus:phy_addr=4e000000.etherne:, irq=-)
[ 12.236870] smsc911x 4e000000.ethernet eth0: SMSC911x/921x identified at 0xf1420000, IRQ:
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. (default, Mar , ::)
[GCC 4.8. (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> for i in range(): 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

完。