linux 使用OpenSSL静态编译Python 3.6

ibps3vxo  于 2023-10-16  发布在  Linux
关注(0)|答案(3)|浏览(120)

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

然而,在configure步骤中,我得到了错误:

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/
knsnq2tg

knsnq2tg1#

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

归档文件(*.a)只是目标文件(*.o)的集合,因此您可以在使用目标文件的任何地方使用归档文件。
另请参阅-l:filenameld(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-dtagsRUNPATH(与RPATH相对)嵌入到ELF头中。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可能(也可能不会)解决这个问题。

szqfcxe2

szqfcxe22#

我遇到了同样的问题,并通过安装静态glibc库解决了它:

yum install glibc-static
relj7zay

relj7zay3#

你可以看看python仓库,里面有一个python编译python所需的模块列表和c文件名:
https://github.com/python/cpython/blob/51863b7d6ea183167da09fc6b3f2745a1aaa4ef5/Modules/Setup#LL232
大概是这样的:

*static*

# Modules that should always be present (POSIX and Windows):

#_asyncio _asynciomodule.c
#_bisect _bisectmodule.c
#_contextvars _contextvarsmodule.c
#_csv _csv.c
#_datetime _datetimemodule.c
#_decimal _decimal/_decimal.c
#_heapq _heapqmodule.c
#_json _json.c
#_lsprof _lsprof.c rotatingtree.c
#_multiprocessing -I$(srcdir)/Modules/_multiprocessing _multiprocessing/multiprocessing.c _multiprocessing/semaphore.c
#_opcode _opcode.c
#_pickle _pickle.c
#_queue _queuemodule.c
#_random _randommodule.c
#_socket socketmodule.c
#_statistics _statisticsmodule.c
...

做一个取消注解,并把它放入'Setup.local'文件,然后你就可以开始了。
如果你真的不知道如何编译它,我有一个dockerfile给你:

FROM alpine:3.12

ENV PYTHON_VER 3.10.4
ENV PYTHON_LIB_VER 3.10

RUN mkdir /build /package
WORKDIR /build

RUN wget https://www.python.org/ftp/python/$PYTHON_VER/Python-$PYTHON_VER.tgz && tar -xzf Python-$PYTHON_VER.tgz
WORKDIR Python-$PYTHON_VER

RUN apk add musl-dev 
RUN apk add build-base
RUN apk add linux-headers
RUN apk add bash

RUN apk add python3-dev py3-pip 
RUN apk add git vim wget make

RUN apk add openssl-dev
RUN apk add sqlite-dev
RUN apk add zlib-dev
RUN apk add bzip2-dev
RUN apk add libffi-dev
RUN apk add util-linux-dev
RUN apk add xz-dev
RUN apk add libnsl-dev libtirpc-dev
RUN apk add gdbm-dev
RUN apk add tk-dev

RUN apk add ncurses-dev
RUN apk add readline-dev

# RUN python3 -m ensurepip
# RUN pip3 install readline

ADD Setup.local Modules/
ENV LDFLAGS "-static"
RUN ./configure LDFLAGS="-static -static-libgcc" CPPFLAGS="-static" --disable-shared --with-ensurepip=install --with-ssl --with-zlib --enable-nis --with-tirpc-include-dir=/usr/include/tirpc --with-gdbm
RUN make -j$(nproc) LDFLAGS="-static" LINKFORSHARED=" "

# RUN cp libpython$PYTHON_LIB_VER.a /usr/lib
# WORKDIR /build
# ENV LDFLAGS "-static -l:libpython3.10.a"

CMD sleep 999999999999
# Manually copy /build to host storage

相关问题