移植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

相關文章
相關標籤/搜索