您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何使用自制软件的openSSL在macOS上编译Python 3.6.2?

如何使用自制软件的openSSL在macOS上编译Python 3.6.2?

我之前发现的以前的答案都没有对我有用,但是我最终还是借助先前未提及的另一个答案来弄清楚了。这是实际的解决方法

过时的SSL是多个平台上的常见问题:

这是一般的方法

选项I:安装并行OpenSSL 1.x库(-dev或-devel)软件包的系统软件包。

# FreeBSD

pkg install openssl
OPENSSL_ROOT=/usr/local


# Mac (brew)

brew install openssl # DO NOT DO ANY WEIRD SYMLINK HACKS, ITS KEG-ONLY FOR A REASON!
OPENSSL_ROOT="$(brew --prefix openssl)"

选项II:将OpenSSL从源安装到临时目录

OPENSSL_ROOT="$HOME/.build/openssl-1.0.1e"

curl http://www.openssl.org/source/openssl-1.0.1e.tar.gz | tar zxvf -
cd openssl-1.0.1e
mkdir -p "$OPENSSL_ROOT"
./config no-hw --prefix="$OPENSSL_ROOT" --openssldir=...
# osx (instead of prevIoUs line): ./Configure darwin64-x86_64-cc no-hw --prefix="$OPENSSL_ROOT" --openssldir=...
make install
cd ..
rm -rf openssl-1.0.1e

选项A:使用pyenv:

export CONfigURE_OPTS="CPPFLAGS=-I"$OPENSSL_ROOT"/include LDFLAGS=-L"$OPENSSL_ROOT"/lib [your other options here]"
pyenv install 2.7.6

./configure CPPFLAGS="-I$OPENSSL_ROOT/include" LDFLAGS="-L$OPENSSL_ROOT/lib" [your other options here]`
make
# ...
# if compiled openssl was used, it can be safely deleted because python's module ssl links openssl statically.

示例:FreeBSD 9.2(make install出于演示目的而跳过)

pkg install openssl curl gmake gdbm sqlite3 readline ncurses
OPENSSL_ROOT=/usr/local
curl http://www.python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz | tar jxvf -
cd Python-2.7.6
./configure CPPFLAGS="-I$OPENSSL_ROOT/include" LDFLAGS="-L$OPENSSL_ROOT/lib" [your other options here]
make
./python -c 'import ssl; print(ssl.OPENSSL_VERSION)' # osx: ./python.exe ...
# prints: OpenSSL 1.0.1e 11 Feb 2013

之后,不再需要临时的openssl库b / c将带有openssl的ssl模型静态地放入python可执行文件中(使用otool或验证readelf)。

基本上,CPPFLAGS和LDFLAGS不能在环境中设置。您需要在configure命令旁边进行设置,如下所示:

./configure CPPFLAGS="-I[openSSL install location]/include" LDFLAGS="-L[openSSL install location]/lib" [other flags here]

然后,在编译和安装后,它开始工作了!

$ python3 -c "import ssl; print(ssl.OPENSSL_VERSION)"
OpenSSL 1.0.2l  25 May 2017

以下是无效的原因以及原因:

如何使用自定义OpenSSL编译Python 3.4?这无济于事,因为您无法在环境中设置LDFLAGS,CFLAGS或CPPFLAGS;setup.py不会将它们传递给实际的编译步骤。即使设置LD_LIBRARY_PATH可能起作用,您也不想这样做,因为这很危险(请参阅http://xahlee.info/UnixResource_dir/_/ldpath.html)。最后,–with- ssl不是有效的configure参数,并且在那里列出的用于添加它的补丁程序并不适用。

当您尝试从源代码构建某些东西,而不是试图获取一个已经编译的dylib来查找重定位的库时,Homebrew拒绝链接OpenSSL并不适用。此外,在/ usr / local中建立符号链接是很危险的,并且可能导致程序针对较新的标头进行编译,但使用较旧的系统二进制文件

如何在MacOS上将ssl与pythonbuild包括在一起无法正常工作。编辑setup.py以添加lib和include目录(用于安装OpenSSL的_部分位置)_ ,并且可以在SSL支持中进行编译。las,它们不可导入,因为旧版本仍在使用:

Following modules built successfully but were removed because they Could not be imported:
_hashlib              _ssl

[…]

building '_ssl' extension
gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -I/usr/local/opt/openssl/include/ -I./Include -I/Oebuild/python/python-3.6.1/include -I. -I/usr/local/include -I/Oebuild/python/src/Python-3.6.1/Include -I/Oebuild/python/src/Python-3.6.1 -c /oebuild/python/src/Python-3.6.1/Modules/_ssl.c -o build/temp.macosx-10.11-x86_64-3.6/oebuild/python/src/Python-3.6.1/Modules/_ssl.o
gcc -bundle -undefined dynamic_lookup build/temp.macosx-10.11-x86_64-3.6/oebuild/python/src/Python-3.6.1/Modules/_ssl.o -L/oebuild/python/python-3.6.1/lib -L/usr/local/lib -lssl -lcrypto -o build/lib.macosx-10.11-x86_64-3.6/_ssl.cpython-36m-darwin.so
building '_hashlib' extension
gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -I/usr/local/opt/openssl/include/ -I./Include -I/Oebuild/python/python-3.6.1/include -I. -I/usr/local/include -I/Oebuild/python/src/Python-3.6.1/Include -I/Oebuild/python/src/Python-3.6.1 -c /oebuild/python/src/Python-3.6.1/Modules/_hashopenssl.c -o build/temp.macosx-10.11-x86_64-3.6/oebuild/python/src/Python-3.6.1/Modules/_hashopenssl.o
gcc -bundle -undefined dynamic_lookup build/temp.macosx-10.11-x86_64-3.6/oebuild/python/src/Python-3.6.1/Modules/_hashopenssl.o -L/oebuild/python/python-3.6.1/lib -L/usr/local/lib -lssl -lcrypto -o build/lib.macosx-10.11-x86_64-3.6/_hashlib.cpython-36m-darwin.so
*** WARNING: renaming "_ssl" since importing it Failed: dlopen(build/lib.macosx-10.11-x86_64-3.6/_ssl.cpython-36m-darwin.so, 2): Symbol not found: _CRYPTO_THREADID_set_callback
  Referenced from: build/lib.macosx-10.11-x86_64-3.6/_ssl.cpython-36m-darwin.so
  Expected in: flat namespace
 in build/lib.macosx-10.11-x86_64-3.6/_ssl.cpython-36m-darwin.so
*** WARNING: renaming "_hashlib" since importing it Failed: dlopen(build/lib.macosx-10.11-x86_64-3.6/_hashlib.cpython-36m-darwin.so, 2): Symbol not found: _HMAC_CTX_copy
  Referenced from: build/lib.macosx-10.11-x86_64-3.6/_hashlib.cpython-36m-darwin.so
  Expected in: flat namespace
 in build/lib.macosx-10.11-x86_64-3.6/_hashlib.cpython-36m-darwin.so

otool -L 显示问题:

$ otool -L build/lib.macosx-10.11-x86_64-3.6/_ssl.cpython-36m-darwin_Failed.so 
build/lib.macosx-10.11-x86_64-3.6/_ssl.cpython-36m-darwin_Failed.so:
    /usr/lib/libssl.0.9.8.dylib (compatibility version 0.9.8, current version 0.9.8)
    /usr/lib/libcrypto.0.9.8.dylib (compatibility version 0.9.8, current version 0.9.8)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)

(根据https://wiki.openssl.org/index.php/Manual:Threads(3)#HISTORY,版本1.0.0中引入了CRYPTO_THREADID )

python 2022/1/1 18:29:05 有321人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶