使用OpenSSL静态编译Python 3.6

问题描述 投票:2回答:1

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

我的构建发生在dockerfile中,但基本上是这样的:

$ ./configure --prefix=/task/build --disable-shared LDFLAGS="-static"
$ make altinstall

随着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

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

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

如果我将configure命令更改为:

$ ./configure --prefix=/task/build --disable-shared

我得到一个编译的二进制文件,但它没有静态链接到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/
python linux openssl static-linking
1个回答
2
投票

我正在尝试使用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

-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

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

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

Archives(*.a)只是一个目标文件集合(*.o),因此您可以在任何使用目标文件的地方使用存档。

另见-l:filename中的ld(2) man page

--library=namespec

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

如果你在/usr/local中使用了其他组件,那么你可能想要将-L/usr/local/lib -Wl,-R,/usr/local/lib -Wl,--enable-new-dtags添加到你的LDFLAGSnew-dtags在ELF标题中嵌入了RUNPATH(而不是RPATH)。 RUNPATH可以用LD_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)

这里有几个相关的问题,但它们看起来并不像是用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

静态OpenSSL可能(或可能不)解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.