移植python3到flash有限的arm

背景

想在嵌入式linux系统上使用python3.python

嵌入式平台:飞思卡尔(NXP)的IMX280 ARM7linux

python版本:3.7性能

host虚拟机:ubantu12.4 32位ui

编译

python官网下载源码包

编译host虚拟机中运行的python

由于等一下交叉编译的时候要用到。编译比较简单spa

  • ./configure --prefix=/home/bert/python3.7-x86
  • make
  • make install
  • ln -s /home/bert/python3.7-x86/bin/python3.7 /usr/bin/python3

报错:No module named '_ctypes'.net

解决:sudo apt-get install libffi.devcode

编译arm版本的python

编译arm版本比较麻烦,写一个config脚本,名为bertconfig-arm.sh。blog

#!/bin/sh arm_build=`pwd`/arm_build mkdir $arm_build cd $arm_build echo ac_cv_file__dev_ptmx=yes > config.site echo ac_cv_file__dev_ptc=yes >> config.site export CONFIG_SITE=config.site ../configure \ CC=/opt/freescale/usr/local/gcc-4.1.2-glibc-2.5-nptl-3/arm-none-linux-gnueabi/bin/arm-none-linux-gnueabi-gcc \ CXX=CC=/opt/freescale/usr/local/gcc-4.1.2-glibc-2.5-nptl-3/arm-none-linux-gnueabi/bin/arm-none-linux-gnueabi-g++ \ AR=/opt/freescale/usr/local/gcc-4.1.2-glibc-2.5-nptl-3/arm-none-linux-gnueabi/bin/arm-none-linux-gnueabi-ar \ READELF=/opt/freescale/usr/local/gcc-4.1.2-glibc-2.5-nptl-3/arm-none-linux-gnueabi/bin/arm-none-linux-gnueabi-readelf \ STRIP=/opt/freescale/usr/local/gcc-4.1.2-glibc-2.5-nptl-3/arm-none-linux-gnueabi/bin/arm-none-linux-gnueabi-strip \ --host=arm-none-linux-gnueabi \ --build=i686-linux-gnu \ --target=arm-none-linux-gnueabi \ --disable-ipv6 \ --prefix=/home/bert/python3.7-arm exit 0
  • make clean
  • ./bertconfig-arm.sh
  • make
  • make install

瘦身

编译经过后arm版python160M+,个人嵌入式产品flash才150M空间,无法用,要瘦身。ip

删除没必要要的文件

  • 删除include和share文件夹
  • bin和lib下面仅留下python3.7
  • /lib/python3.7下删除全部test和config相关的文件

strip缩小体积

将 bin下的python3.7 和lib/python3.7下的全部.so strip,基本能够减少一半的体积get

  • arm-strip /bin/python3.7
  • arm-strip /lib/python3.7/lib-dynload/*.so

将.py转换成.pyc文件

其实__pycache__里面放的是.py自动生成的.pyc文件,如今手动转换后,就能够减少一半的空间。

  • python3 -m compileall . -b
  • 删除__pycatche__和全部.py

 

通过以上步骤,体积缩小到50M左右勉强能够用啦。

其余思路:

建立zip版本的lib:能够是能够,可是运行的时候仍是要解压呀,增长第三方库的时候也不方便吧?

使用其余压缩软件:比较麻烦吧,不知道会不会影响性能。

 

终极瘦身之最小python

lib下面有好多包,并不是都是必须的,例如在没有显示屏的arm中,要tkinter做甚?

  • lib/python3下面保留lib-dynload(动态库)和os.pyc这两个文件
  • 在arm系统上运行bin/python3.7,提示差什么包就把什么包复制过去

最终lib结构以下:

-rw-r--r-- 1 bert bert 29K Oct 11 02:37 _collections_abc.pyc
-rw-r--r-- 1 bert bert 3.4K Oct 11 02:37 _sitebuiltins.pyc
-rw-r--r-- 1 bert bert 6.3K Oct 11 02:37 abc.pyc
-rw-r--r-- 1 bert bert 34K Oct 11 02:37 codecs.pyc
drwxr-xr-x 3 bert bert 12K Oct 11 04:12 encodings
-rw-r--r-- 1 bert bert 3.7K Oct 11 02:37 genericpath.pyc
-rw-r--r-- 1 bert bert 3.3K Oct 11 02:37 io.pyc
drwxr-xr-x 2 bert bert 4.0K Oct 12 2019 lib-dynload
-rw-r--r-- 1 bert bert 29K Oct 11 02:37 os.pyc
-rw-r--r-- 1 bert bert 11K Oct 11 02:37 posixpath.pyc
-rw-r--r-- 1 bert bert 17K Oct 11 02:37 site.pyc
-rw-r--r-- 1 bert bert 3.8K Oct 11 02:37 stat.pyc

如今只有13M了,彻底知足个人要求。

encodings文件夹下面确定能够继续缩减,不过已经超出个人需求了,到此为止。

参考

好多一键移植的方法,我我的以为,在linux里面,No way.

不得不说的两篇好文章供参考:

编译:https://blog.csdn.net/u012230668/article/details/89206857

瘦身:https://blog.csdn.net/yyw794/article/details/78059183