编译 Python 源代码

为什么可以/需要编译

通常在 Windows 下安装软件,只需要下载可执行文件,一直下一步就可以完成安装。而 Linux 下,你可以获取软件的源代码,自行编译生成可执行文件进行安装。用源代码有什么好处呢?你可以根据自己的需要对软件进行修改,甚至提前修补 已经发现但厂商没有更新 的漏洞。这也就是开源的好处。

什么是源代码、可执行文件

源代码就是用编程语言编写好的程序,其实就是文本文件,可以通过文本编辑器进行编辑。而可执行文件是操作系统认识的二进制程序。要想将源代码变成可执行文件,就需要对源代码进行编译

C语言的编译程序就是gcc

编译是怎样的一个过程

软件的源代码(文本文档)+ 系统已有的函数库,经过编译器的编译,生成可执行文件
函数库是做什么的?如同 Python 会调用大量外部的模块/库帮助实现一些功能一样,软件也会用到系统的函数完成一些任务,所以需要在编译时写入,以便程序执行时调用。

make 与 configure

一个软件往往不仅有一个源代码文件,如果手动对每一个源文件进行编译,太累了,好在有一个非常有用的工具make,可以自动完成编译的过程。那么,make根据什么进行编译呢?他会在当前目录下搜索 Makefile/makefile 文件,这个文件中提供了所需的所有内容。但这个文件并不是生来就有、一成不变的。

由于各种 Linux 系统内部环境不同,软件开发者会编写一个自动检测环境是否符合要求,为每种环境生成特定 Makefile/makefile 文件的程序,就是configure

值得一提的是,当源代码修改后重新编译时,make 能够识别变动的部分,进行增量更新。

Tarball 文件

源代码是纯文本文件,如果软件开发者直接提供这些纯文本文件,会浪费开发者的带宽,也使得用户不易下载。

所以,出现了 Tarball 文件,就是将软件的所有源代码先以 tar 打包,然后用压缩技术压缩,通常使用 gzip,所以一般扩展名是*.tar.gz*.tgz。不过后来出现了压缩效率更高的 bzip2,所以扩展名也会有*.tar.bz2之类。

Tarball 文件通常包含:

  • 源代码文件
  • 检测程序文件(configure 或 config等)
  • 软件的简单说明 与 安装说明(README 或 INSTALL)

tarball 安装软件的步骤

  1. 下载源代码。链接
  2. 解压,阅读源码所在目录下的 README/INSTALL。建议解压在/usr/local/src目录下。
  3. ./configure ‘–enable-framework[=DIR]’:执行源码所在目录下的 configure,生成 Makefile 文件。可以指定最终可执行文件的安装目录,建议安装在/usr/local/bin/下。

./configure --enable-framework=/Users/ronald/Library/Frameworks

  1. make:在目录下运行 make 程序,以寻找 Makefile 文件,根据文件内容进行编译。生成的可执行文件会存放在当前所在目录下。
  2. make install:make 根据 Makefile 文件中 install 的选项,将编译完成的可执行文件安装到指定目录,完成安装。

实测

➜  Python-2.7.9  ./configure
checking build system type... x86_64-apple-darwin14.5.0
checking host system type... x86_64-apple-darwin14.5.0
...
configure: checking for device files
checking for /dev/ptmx... yes
checking for /dev/ptc... no
checking for %lld and %llu printf() format support... yes
checking for %zd printf() format support... yes
checking for socklen_t... yes
checking for build directories... done
checking for ensurepip... no
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Modules/Setup.config
config.status: creating Misc/python.pc
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup
creating Modules/Setup.local
creating Makefile
➜  Python-2.7.9  make
....
Python build finished, but the necessary bits to build these modules were not found:
_bsddb             _sqlite3           _ssl
bsddb185           dbm                dl
gdbm               imageop            linuxaudiodev
nis                ossaudiodev        readline
spwd               sunaudiodev        zlib
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

running build_scripts
creating build/scripts-2.7
copying and adjusting /Users/chao/Downloads/Python-2.7.9/Tools/scripts/pydoc -> build/scripts-2.7
copying and adjusting /Users/chao/Downloads/Python-2.7.9/Tools/scripts/idle -> build/scripts-2.7
copying and adjusting /Users/chao/Downloads/Python-2.7.9/Tools/scripts/2to3 -> build/scripts-2.7
copying and adjusting /Users/chao/Downloads/Python-2.7.9/Lib/smtpd.py -> build/scripts-2.7
changing mode of build/scripts-2.7/pydoc from 644 to 755
changing mode of build/scripts-2.7/idle from 644 to 755
changing mode of build/scripts-2.7/2to3 from 644 to 755
changing mode of build/scripts-2.7/smtpd.py from 644 to 755
/usr/bin/install -c -m 644 ./Tools/gdb/libpython.py python.exe-gdb.py

setup.py
missing.append

卸载软件

删除安装目录
rm -rf /usr/local/bin/xxx

参考资料

《鸟哥的linux私房菜》第22章 软件安装:源码与Tarball

    原文作者:超net
    原文地址: https://www.jianshu.com/p/beb98f9b6429
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞