• 移植python3到flash有限的arm


    背景

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

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

    python版本:3.7

    host虚拟机:ubantu12.4 32位

    编译

    python官网下载源码包

    编译host虚拟机中运行的python

    因为等一下交叉编译的时候要用到。编译比较简单

    • ./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'

    解决:sudo apt-get install libffi.dev

    编译arm版本的python

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

    #!/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空间,没法用,要瘦身。

    删除不必要的文件

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

    strip缩小体积

    将 bin下的python3.7 和lib/python3.7下的所有.so strip,基本可以减小一半的体积

    • 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

  • 相关阅读:
    No enclosing instance of type XXX is accessible.
    No enclosing instance of type XXX is accessible.
    Websphere 学习(一)
    List去重与equals/hashcode
    List去重与equals/hashcode
    org.apache.log4j.Logger详解
    org.apache.log4j.Logger详解
    onclick="return checkForm()" 、onclick="checkForm();return false;"解析 与 return false;
    onclick="return checkForm()" 、onclick="checkForm();return false;"解析 与 return false;
    大数据基础第一天内容
  • 原文地址:https://www.cnblogs.com/real-bert/p/11662759.html
Copyright © 2020-2023  润新知