macos - Has anyone managed to cross-compile the Python cryptography package for Android on a Mac? - Stack Overflow

admin2025-04-16  1

I'm trying to cross-compile the pyca/cryptography for Android on Mac OS Sonoma. I tried all combinations of everything and I run into different errors every time while all other libraries cross-compile without any issues...

Anyone have a working example with modifications needed?

My latest attempt results in:

ld: error: unable to find library -lpthread

error: command '/Users/tobiaslindell/.buildozer/android/platform/android-ndk-r25b/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang' failed with exit code 1

This is what I see in the buildozer/p4a just before the error occurs:

[DEBUG]:    creating build/lib.macosx-14.4-arm64-cpython-311/cryptography/hazmat/bindings
[DEBUG]:    /Users/tobiaslindell/.buildozer/android/platform/android-ndk-r25b/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang -target aarch64-linux-android21 -fomit-frame-pointer -march=armv8-a -fPIC -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -L/Users/tobiaslindell/appStuff/.buildozer/android/platform/build-arm64-v8a/build/libs_collections/convokeeper/arm64-v8a -L/Users/tobiaslindell/appStuff/.buildozer/android/platform/build-arm64-v8a/build/other_builds/python3/arm64-v8a__ndk_target_21/python3/android-build -lpython3.11 -L/Users/tobiaslindell/appStuff/.buildozer/android/platform/build-arm64-v8a/build/other_builds/openssl/arm64-v8a__ndk_target_21/openssl1.1 -target aarch64-linux-android21 -fomit-frame-pointer -march=armv8-a -fPIC -I/Users/tobiaslindell/appStuff/.buildozer/android/platform/build-arm64-v8a/build/other_builds/python3/arm64-v8a__ndk_target_21/python3/Include -I/Users/tobiaslindell/appStuff/.buildozer/android/platform/build-arm64-v8a/build/other_builds/openssl/arm64-v8a__ndk_target_21/openssl1.1/include -I/Users/tobiaslindell/appStuff/.buildozer/android/platform/build-arm64-v8a/build/other_builds/openssl/arm64-v8a__ndk_target_21/openssl1.1/include/internal -I/Users/tobiaslindell/appStuff/.buildozer/android/platform/build-arm64-v8a/build/other_builds/openssl/arm64-v8a__ndk_target_21/openssl1.1/include/openssl -DANDROID -I/Users/tobiaslindell/.buildozer/android/platform/android-ndk-r25b/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include -I/Users/tobiaslindell/appStuff/.buildozer/android/platform/build-arm64-v8a/build/python-installs/convokeeper/arm64-v8a/include/python3.1 build/temp.macosx-14.4-arm64-cpython-311/build/temp.macosx-14.4-arm64-cpython-311/cryptography.hazmat.bindings._openssl.o -lssl -lcrypto -lpthread -o build/lib.macosx-14.4-arm64-cpython-311/cryptography/hazmat/bindings/_openssl.abi3.so
[DEBUG]:    ld: error: unable to find library -lpthread

I modified the last last line here (removed , "pthread") just to get past the error, which I assume is not the right way to go about this:

# This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.


import os
import sys
from distutils import dist
from distutilsompiler import get_default_compiler
from distutilsmand.config import config

from _cffi_src.utils import build_ffi_for_binding, compiler_type


def _get_openssl_libraries(platform):
    if os.environ.get("CRYPTOGRAPHY_SUPPRESS_LINK_FLAGS", None):
        return []
    # OpenSSL goes by a different library name on different operating systems.
    if platform == "win32" and compiler_type() == "msvc":
        return [
            "libssl",
            "libcrypto",
            "advapi32",
            "crypt32",
            "gdi32",
            "user32",
            "ws2_32",
        ]
    else:
        # darwin, linux, mingw all use this path
        # In some circumstances, the order in which these libs are
        # specified on the linker command-line is significant;
        # libssl must come before libcrypto
        # (/?l=openssl-users&m=135361825921871)
        # -lpthread required due to usage of pthread an potential
        # existance of a static part containing e.g. pthread_atfork
        # ()
        if sys.platform == "zos":
            return ["ssl", "crypto"]
        else:
            return ["ssl", "crypto", "pthread"]
Python: 3.11.5
host:   arm64 (macOS Sonoma 14.4.1)
target: arm64-v8a (Android)
pip: 25.0
setuptools: 75.8.0
setuptools_rust: 1.10.2
rustc: 1.84.0 (9fc6b4312 2025-01-07)

appdirs                  1.4.4
build                    1.2.2.post1
buildozer                1.5.0
certifi                  2025.1.31
cffi                     1.15.1
charset-normalizer       3.4.1
colorama                 0.4.6
cryptography             38.0.1
Cython                   0.29.33
Deprecated               1.2.16
distlib                  0.3.9
docutils                 0.21.2
filelock                 3.17.0
filetype                 1.2.0
gcloud                   0.18.3
googleapis-common-protos 1.66.0
httplib2                 0.22.0
idna                     3.10
Jinja2                   3.1.5
jwcrypto                 1.4.2
Kivy                     2.3.1
Kivy-Garden              0.1.5
MarkupSafe               3.0.2
oauth2client             4.1.3
packaging                24.2
pexpect                  4.9.0
pip                      24.3.1
platformdirs             4.3.6
protobuf                 5.29.3
ptyprocess               0.7.0
pyasn1                   0.6.1
pyasn1_modules           0.4.1
pycparser                2.22
pycryptodome             3.21.0
Pygments                 2.19.1
pyparsing                3.2.1
pyproject_hooks          1.2.0
Pyrebase4                4.8.0
python-jwt               4.1.0
requests                 2.32.3
requests-toolbelt        0.10.1
rsa                      4.9
semantic-version         2.10.0
setuptools               75.8.0
setuptools-rust          1.10.2
sh                       1.14.3
six                      1.17.0
toml                     0.10.2
urllib3                  1.26.20
virtualenv               20.29.1
wheel                    0.45.1
wrapt                    1.17.2

# Homebrew path
export PATH="/opt/homebrew/bin:$PATH"

# Paths used for locating shared libraries and package configurations
export LDFLAGS="-L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib -L/usr/local/openssl-1.1/lib -L$(brew --prefix libffi)/lib -L/opt/homebrew/opt/binutils/lib -L/usr/local/opt/expat/lib"
export CPPFLAGS="-I$(brew --prefix zlib)/include -I$(brew --prefix bzip2)/include -I/usr/local/openssl-1.1/include -I$(brew --prefix libffi)/include -I/opt/homebrew/opt/binutils/include -I/usr/local/opt/expat/include"
export PKG_CONFIG_PATH="/usr/local/openssl-1.1/lib/pkgconfig:$(brew --prefix zlib)/lib/pkgconfig:$(brew --prefix bzip2)/lib/pkgconfig:$(brew --prefix libffi)/lib/pkgconfig:/opt/homebrew/opt/expat/lib/pkgconfig"
export SSL_CERT_FILE=$(python3 -m certifi)

# Pipenv language flag
export LANG=en_US.UTF-8

# Cargo path
export PATH="$HOME/.cargo/bin:$PATH"

# Perl path
eval "$(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib=$HOME/perl5)"

# Java path
export JAVA_HOME=/usr/local/jdk-17/jdk-17.0.14.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH

# OpenSSL path
export OPENSSL_HOME=/usr/local/openssl-1.1
export PATH=$OPENSSL_HOME/bin:$PATH
转载请注明原文地址:http://www.anycun.com/QandA/1744763880a87282.html