使用 OpenSSL 静态编译 Python 3.6

Jay*_*Jay 4 python linux openssl static-linking

我正在尝试使用 OpenSSL 在 Linux 上静态编译 Python 3.6。

我的构建发生在 dockerfile 中,但本质上是:

$ ./configure --prefix=/task/build --disable-shared LDFLAGS="-static"
$ make altinstall
Run Code Online (Sandbox Code Playgroud)

通过更新使其Modules/Setup.local看起来像:

*static*

# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl
_ssl _ssl.c \
 -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
 -L$(SSL)/lib -lssl -lcrypto
Run Code Online (Sandbox Code Playgroud)

但是,在配置步骤中,我收到错误:

Step 9/14 : RUN ./configure --prefix=/task/build --disable-shared LDFLAGS="-static"
     ---> Running in cb79ee47052b
    checking for git... found
    checking build system type... x86_64-pc-linux-gnu
    checking host system type... x86_64-pc-linux-gnu
    checking for python3.6... no
    checking for python3... no
    checking for python... python
    checking for --enable-universalsdk... no
    checking for --with-universal-archs... no
    checking MACHDEP... linux
    checking for --without-gcc... no
    checking for --with-icc... no
    checking for gcc... gcc
    checking whether the C compiler works... no
    configure: error: in `/task/cpython':
    configure: error: C compiler cannot create executables
    See `config.log' for more details
The command '/bin/sh -c ./configure --prefix=/task/build --disable-shared LDFLAGS="-static"' returned a non-zero code: 77
Run Code Online (Sandbox Code Playgroud)

如果我将配置命令更改为:

*static*

# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl
_ssl _ssl.c \
 -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
 -L$(SSL)/lib -lssl -lcrypto
Run Code Online (Sandbox Code Playgroud)

我得到了一个已编译的二进制文件,但它没有静态链接到 OpenSSL。

我究竟做错了什么?

谢谢!


构建dockerfile:

FROM amazonlinux:2017.03.1.20170812

ARG python_version=3.6.8

WORKDIR /task
COPY Modules-Setup.local /task/Modules-Setup.local

# Install requirements
RUN yum install -y \
  gcc \
  git \
  gzip \
  openssl-devel \
  tar \
  zlib \
  zlib-devel

# Get openssl and python source
RUN git clone https://github.com/python/cpython.git
WORKDIR /task/cpython
RUN git checkout tags/v${python_version}

# Configure the build
RUN ./configure --prefix=/task/build --disable-shared LDFLAGS="-static"

# Append modules setup with custom values
RUN cat /task/Modules-Setup.local >> /task/cpython/Modules/Setup.local
RUN cat /task/cpython/Modules/Setup.local

# Build
RUN make altinstall

# Zip the results
WORKDIR /task/build
RUN tar --create --gzip --file=/task/python-${python_version}.tar.gz \
  lib/ bin/
Run Code Online (Sandbox Code Playgroud)

jww*_*jww 6

我正在尝试使用 OpenSSL 在 Linux 上静态编译 Python 3.6。
...

# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl
_ssl _ssl.c \
 -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
 -L$(SSL)/lib -lssl -lcrypto
Run Code Online (Sandbox Code Playgroud)

-lssl和更改-lcrypto-l:libssl.a-l:libcrypto.a

SSL=/usr/local/ssl
_ssl _ssl.c \
  -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
  -L$(SSL)/lib -l:libssl.a -l:libcrypto.a
Run Code Online (Sandbox Code Playgroud)

您还可以使用存档的完整路径:

SSL=/usr/local/ssl
_ssl _ssl.c \
  -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
  $(SSL)/lib/libssl.a $(SSL)/lib/libcrypto.a
Run Code Online (Sandbox Code Playgroud)

存档 ( *.a) 只是目标文件 ( *.o) 的集合,因此无论您在何处使用目标文件,都可以使用存档。

另请参阅-l:filename手册ld(2)

--library=namespec

将 namespec 指定的存档或目标文件添加到要链接的文件列表中。该选项可以使用任意多次。如果 namespec 的形式为 :filename,ld 将在库路径中搜索名为 filename 的文件,否则它将在库路径中搜索名为 libnamespec.a 的文件。

如果您正在/usr/local使用其他组件,那么您可能需要添加-L/usr/local/lib -Wl,-R,/usr/local/lib -Wl,--enable-new-dtags到您的LDFLAGS. 在 ELF 标头中嵌入new-dtagsa RUNPATH(与 相对)。可以用 覆盖。RPATHRUNPATHLD_LIBRARY_PATH


我得到了一个已编译的二进制文件,但它没有静态链接到 OpenSSL。

检查的方法是ldd与运行时使用的路径一起使用。例如,以下来自 Fedora 上的本地 OpenSSL 构建:

$ ldd /usr/local/bin/openssl
    linux-vdso.so.1 (0x00007fff3cde6000)
    libssl.so.1.0.0 => /usr/local/lib64/libssl.so.1.0.0 (0x00007f043dc4e000)
    libcrypto.so.1.0.0 => /usr/local/lib64/libcrypto.so.1.0.0 (0x00007f043d9df000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007f043d9c0000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f043d7fa000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f043dcc0000)
Run Code Online (Sandbox Code Playgroud)

这里有几个相关的问题,但看起来它们并没有涵盖 Python 的静态链接。


需要明确的是,config.log有错误,但您没有显示其中的相关部分:

checking whether the C compiler works... no
configure: error: in `/task/cpython':
configure: error: C compiler cannot create executables
See `config.log' for more details
Run Code Online (Sandbox Code Playgroud)

静态 OpenSSL 可能(也可能不会)解决该问题。