编译static的Python及自带的相关module

非 static 编译

不指定额外参数直接编译 Python:

$ ./configure
$ make

查看所依赖的共享库:

$ ldd python
    linux-vdso.so.1 =>  (0x00007fffcd95a000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fab5c350000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fab5c140000)
    libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007fab5bf20000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fab5bc10000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fab5b840000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fab5c600000)

static 编译

指定 static 相关参数编译 Python:

$ ./configure LDFLAGS="-static" --disable-shared
$ make LDFLAGS="-static" LINKFORSHARED=" "

发现有部分库未能被编译,编译结果中输出如下内容:

Failed to build these modules:
_bisect            _bsddb             _codecs_cn
_codecs_hk         _codecs_iso2022    _codecs_jp
_codecs_kr         _codecs_tw         _collections
_csv               _ctypes            _ctypes_test
_curses            _curses_panel      _elementtree
_functools         _hashlib           _heapq
_hotshot           _io                _json
_locale            _lsprof            _multibytecodec
_multiprocessing   _random            _socket
_sqlite3           _ssl               _struct
_testcapi          _tkinter           array
audioop            binascii           bz2
cmath              cPickle            crypt
cStringIO          datetime           dbm
fcntl              future_builtins    gdbm
grp                itertools          linuxaudiodev
math               mmap               nis
operator           ossaudiodev        parser
pyexpat            readline           resource
select             spwd               strop
syslog             termios            time
unicodedata        zlib

查看所依赖的共享库:

$ ldd python
    not a dynamic executable

static 编译相关 module

编辑 Modules/Setup.local 为:

# Edit this file for local setup changes
*static*
_bisect _bisectmodule.c
# _bsddb _bsddb.c  # 额外依赖PyBSDDB
_codecs _codecsmodule.c
_codecs_cn cjkcodecs/_codecs_cn.c
_codecs_hk cjkcodecs/_codecs_hk.c
_codecs_iso2022 cjkcodecs/_codecs_iso2022.c
_codecs_jp cjkcodecs/_codecs_jp.c
_codecs_kr cjkcodecs/_codecs_kr.c
_codecs_tw cjkcodecs/_codecs_tw.c
_collections _collectionsmodule.c
_csv _csv.c
# _ctypes _ctypes/_ctypes.c _ctypes/callbacks.c _ctypes/callproc.c _ctypes/cfield.c _ctypes/malloc_closure.c _ctypes/stgdict.c  # 还依赖Python源中的 libffi
# _ctypes_test # 同_ctypes
# _curses _cursesmodule.c  # 额外依赖ncurses
# _curses_panel _curses_panel.c
# _elementtree _elementtree.c expat/loadlibrary.c expat/xmlparse.c expat/xmlrole.c expat/xmltok.c expat/xmltok_impl.c expat/xmltok_ns.c  # 依赖的libexpat可能编译出错
_functools _functoolsmodule.c
# _hashlib _hashopenssl.c  # 额外依赖OpenSSL
_heapq _heapqmodule.c
_hotshot _hotshot.c
_io _io/_iomodule.c _io/bufferedio.c _io/bytesio.c _io/fileio.c _io/iobase.c _io/stringio.c _io/textio.c
_json _json.c
_locale _localemodule.c
_lsprof _lsprof.c rotatingtree.c
_md5 md5.c md5module.c
_multibytecodec cjkcodecs/multibytecodec.c
# _multiprocessing _multiprocessing/multiprocessing.c _multiprocessing/pipe_connection.c _multiprocessing/semaphore.c _multiprocessing/socket_connection.c _multiprocessing/win32_functions.c
_multiprocessing _multiprocessing/multiprocessing.c _multiprocessing/socket_connection.c
_random _randommodule.c
_sha shamodule.c
_sha256 sha256module.c
_sha512 sha512module.c
_socket socketmodule.c
# _sqlite3 _sqlite/cache.c _sqlite/connection.c _sqlite/cursor.c _sqlite/microprotocols.c _sqlite/module.c _sqlite/prepare_protocol.c _sqlite/row.c _sqlite/statement.c _sqlite/util.c  # 额外依赖sqlite3 
_sre _sre.c
# _ssl _ssl.c 额外依赖OpenSSL
_struct _struct.c
_symtable symtablemodule.c
_testcapi _testcapimodule.c
# _tkinter _tkinter.c  # 额外依赖Tcl/Tk
_weakref _weakref.c
# al almodule.c
array arraymodule.c
audioop audioop.c
binascii binascii.c
# bz2 bz2module.c  # 额外依赖bzlib
# cd cdmodule.c
# cl clmodule.c
cmath cmathmodule.c
cPickle cPickle.c
# crypt cryptmodule.c  # 额外依赖OpenSSL
cStringIO cStringIO.c
datetime datetimemodule.c
# dbm dbmmodule.c  # 额外依赖gdbm
dl dlmodule.c
errno errnomodule.c
fcntl fcntlmodule.c
# fl flmodule.c
# fm fmmodule.c  # 额外依赖OpenGL
fpectl fpectlmodule.c
fpetest fpetestmodule.c
future_builtins future_builtins.c
gc gcmodule.c
# gdbm gdbmmodule.c  # 额外依赖gdbm
# gl glmodule.c  # 额外依赖OpenGL
grp grpmodule.c
imageop imageop.c
# imgfile imgfile.c
itertools itertoolsmodule.c
linuxaudiodev linuxaudiodev.c
math mathmodule.c _math.c
mmap mmapmodule.c
# nis nismodule.c  # 依赖glibc/nis
operator operator.c
ossaudiodev ossaudiodev.c
parser parsermodule.c
posix posixmodule.c
pure puremodule.c
pwd pwdmodule.c
# pyexpat pyexpat.c  # 依赖的libexpat可能编译出错
# readline readline.c  # 依赖readline
resource resource.c
select selectmodule.c
signal signalmodule.c
spwd spwdmodule.c
strop stropmodule.c
# sunaudiodev sunaudiodev.c
# sv svmodule.c
syslog syslogmodule.c
termios termios.c
thread threadmodule.c
time timemodule.c
timing timingmodule.c
unicodedata unicodedata.c
xx xxmodule.c
xxsubtype xxsubtype.c
zipimport zipimport.c
zlib zlibmodule.c zlib/adler32.c zlib/compress.c zlib/crc32.c zlib/deflate.c zlib/example.c zlib/gzclose.c zlib/gzlib.c zlib/gzread.c zlib/gzwrite.c zlib/infback.c zlib/inffast.c zlib/inflate.c zlib/inftrees.c zlib/minigzip.c zlib/trees.c zlib/uncompr.c zlib/zutil.c

重新编译后能将部分 Modules 编译为 static 库,输出:

Failed to build these modules:
_bsddb          _ctypes         _ctypes_test
_curses         _curses_panel   _elementtree
_hashlib        _sqlite3        _ssl
_tkinter        bz2             crypt
dbm             gdbm            nis
pyexpat         readline

查看所依赖的共享库:

$ ldd python
    not a dynamic executable

不过对于glibc相关的函数,还是不会被编译进libpythonx.y.a

$ nm libpython2.7.a | grep floor
00000000000018a0 t instance_floordiv
0000000000005130 t instance_ifloordiv
                 U floor
0000000000001b60 t float_floor_div
                 U floor
000000000000c2b0 t slot_nb_floor_divide
000000000000d080 t slot_nb_inplace_floor_divide
0000000000001a90 t proxy_floor_div
0000000000001070 t proxy_ifloor_div
                 U floor
                 U floor
                 U floor
                 U floor
0000000000001a53 t math_floor
00000000000004a0 d math_floor_doc
0000000000000371 t op_floordiv
0000000000000a69 t op_ifloordiv
                 U floor
                 U floor

如果需要生成shared的libpython,使用--enable-shared参数

$ ./configure --enable-shared

参考:

发表评论

电子邮件地址不会被公开。