选中内容(绿色)时除了会搜索文章名,还会搜索文章内容
点击结果中的文章名进入文章界面后可以按Ctrl+F在页面内搜索
  • 版权:CC BY-SA 4.0
  • 创建:2023-09-05
  • 更新:2023-09-06


下载源码

https://www.python.org/downloads/source/

编译给 HOST(PC)用的 python

  1. cd Python-3.6.9/
  2. ./configure --prefix=$PWD/../_install_host --enable-shared
  3. make -j`nproc`
  4. make install

配置参数
https://docs.python.org/3/using/configure.html#configure-options

  1. export PATH=_install_hostbin 路径
  2. export LD_LIBRARY_PATH=_install_hostlib路径
  3. python3 --version

编译 target

清除之前的

  1. make distclean

这里是 armA7 为例

  1. ./configure --host=arm-openwrt-linux --build=armv7l --prefix=`pwd`/../python_target --enable-shared --with-build-python --enable-ipv6 ac_cv_file__dev_ptmx="yes" ac_cv_file__dev_ptc="no"
  2. make -j`nproc`
  3. make install

如果最后遇到了subprocess.CalledProcessError: Command '('lsb_release', '-a')' returned non-zero exit status 1.

用 root 用户编辑 /usr/bin/lsb_release, #!/usr/bin/python3 改为 python3 即可,这是因为 python3 命令才能调用到我们编译出来的 python3。

交叉编译遇到 The necessary bits to build these optional modules were not found

  1. Python build finished successfully!
  2. The necessary bits to build these optional modules were not found:
  3. _bz2 _curses _curses_panel
  4. _dbm _gdbm _hashlib
  5. _lzma _sqlite3 _ssl
  6. _tkinter nis readline
  7. zlib
  8. To find the necessary bits, look in setup.py in detect_modules() for the module's name.
  9. The following modules found by detect_modules() in setup.py, have been
  10. built by the Makefile instead, as configured by the Setup files:
  11. _abc atexit pwd
  12. time
  13. Failed to build these modules:
  14. _ctypes _uuid
  15. Could not build the ssl module!
  16. Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
  17. LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381

编译好了使用python3 -m ensurepip 来安装 pip,也会出现找不到zlib模块的问题。

如果是本机编译比较好解决,比如Ubuntu直接apt安装对应的包就好了。
交叉编译则比较麻烦:
在Python 源码 setup.py 中有detect_modules函数来找依赖的源码,其中detect_compress_exts函数就是找zlib,可以看到它去zlib_inc = find_file('zlib.h', [], self.inc_dirs) include的路径找zlib.h头文件了,以及self.lib_dirs找库文件,所以理论上我们交叉编译好 zlib,并且编译 Python 时往./configure命令传入CFLAGS="-I***/include CPPFLAGS="-I***/include" LDFLAGS="-L***/lib环境变量就好了,其它模块同理去看对应的detect_*****()函数里面在检测什么变量,编译时传入环境变量即可。

可以参考这里: https://gist.github.com/j-c-cook/2a291dc0bfaa2f6639272e344ff66e62

  1. $ tar -xvf v1.2.11.tar.gz && cd cd zlib-1.2.11/
  2. $ CHOST="${cross}" CC="${cross}-gcc" CXX="${cross}-g++" AR="${cross}-ar" LD="${cross}-ld" RANLIB="${cross}-ranlib"
  3. $ ./configure --prefix=$HOME/zlibArm
  4. $ make -j`nproc`
  5. $ make install

pip 也需要 ssl, openssl 有专门的参数--with-openssl=DIR, 设置了这个参数,configure 会自动去这个目录下找include``lib,所以不用我们自己设置变量了。这个 DIR 就是交叉编译后的 openssl install 路径

  1. $ tar -xvf OpenSSL_1_1_1o.tar.gz && cd openssl-OpenSSL_1_1_1o
  2. $ ./Configure linux-generic32 shared -DL_ENDIAN --prefix=$HOME/opensslArm --openssldir=$HOME/opensslArm
  3. $ make CC="${cross}"-gcc RANLIB="${cross}"-ranlib LD="${cross}"-ld MAKEDEPPROG="${cross}"-gcc PROCESSOR=ARM
  4. $ make install
  1. $ cd cpython-3.7.13/
  2. $ CHOST="${cross}" CC="${cross}"-gcc CXX="${cross}"-g++ AR="${cross}"-ar LD="${cross}"-ld RANLIB="${cross}"-ranlib CFLAGS="-I$HOME/zlibArm/include -I$HOME/sqliteArm/include -I$HOME/libffiArm/include" LDFLAGS="-L$HOME/zlibArm/lib -L$HOME/sqliteArm/lib -L$HOME/libffiArm/lib" CPPFLAGS="-I$HOME/zlibArm/include -I$HOME/sqliteArm/include -I$HOME/libffiArm/include" ./configure --prefix=$HOME/pythonArm --host="${cross}" --target=arm --build=x86_64-linux-gnu --with-openssl="$HOME/opensslArm" --disable-ipv6 ac_cv_file__dev_ptmx=no ac_cv_file__dev_ptc=no --with-ensurepip=install --enable-loadable-sqlite-extensions --enable-optimizations
  3. $ make -j 8
文章有误?有想法想讨论?查看或者发起勘误/讨论 主题
(发起评论需要先登录 github)

/wallpaper/wallhaven-dg189g.jpg