-
-
Save iplanetcn/7227f442f6eaab26da6598ccdf0387b9 to your computer and use it in GitHub Desktop.
Cross-compile OpenSSL 3.X for Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set -o nounset # Treat unset variables as an error | |
#!/bin/bash - | |
#=============================================================================== | |
# | |
# FILE: build-openssl.sh | |
# | |
# USAGE: ./build-openssl.sh | |
# | |
# DESCRIPTION: | |
# | |
# This script builds openSSL from source for the Android platform | |
# while patching some hand-written assembly that fails on Android | |
# It's useful when trying to use OpenSSL in Rust which tries to | |
# from the official sources and will therefore fail when launching | |
# your crate on Android | |
# | |
# Link to the main PR that fixes the sources: | |
# | |
# https://github.com/openssl/openssl/pull/22181 | |
# | |
# Set the ANDROID_NDK_HOME environment variable for this script to work | |
# | |
# OPTIONS: --- | |
# REQUIREMENTS: --- | |
# BUGS: --- | |
# NOTES: --- | |
# AUTHOR: YOUR NAME (), | |
# ORGANIZATION: | |
# CREATED: 12/20/2024 17:22 | |
# REVISION: --- | |
#=============================================================================== | |
set -ex | |
export ANDROID_NDK_HOME=/Users/john/Library/Android/sdk/ndk/27.2.12479018 | |
export ANDROID_NDK_ROOT=/Users/john/Library/Android/sdk/ndk/27.2.12479018 | |
export OPENSSL_VERSION="openssl-3.4.0" | |
rm -rf ${OPENSSL_VERSION} | |
# Uncommment this lines to try to download the file, however this URL might have changed, you can just download it yourself | |
# curl -O "https://www.openssl.org/source/${OPENSSL_VERSION}.tar.gz" | |
# tar xfz "${OPENSSL_VERSION}.tar.gz" | |
PROJECT_HOME=`pwd` | |
PATH_ORG=$PATH | |
OUTPUT_DIR="libs" | |
# Clean output | |
rm -rf $OUTPUT_DIR | |
mkdir $OUTPUT_DIR | |
build_android_clang() { | |
echo "" | |
echo "----- Build libcrypto & libssl.so for "$1" -----" | |
echo "" | |
ARCHITECTURE=$1 | |
TOOLCHAIN=$2 | |
stl="libc++" | |
# Set toolchain | |
export TOOLCHAIN_ROOT=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64 | |
export SYSROOT=$TOOLCHAIN_ROOT/sysroot | |
export CC=${TOOLCHAIN}21-clang | |
export CXX=${TOOLCHAIN}21-clang++ | |
export CXXFLAGS="-fPIC" | |
export CPPFLAGS="-DANDROID -fPIC" | |
export PATH=$TOOLCHAIN_ROOT/bin:$SYSROOT/usr/local/bin:$PATH | |
#cd "${OPENSSL_VERSION}" | |
./Configure $ARCHITECTURE no-asm no-shared -D__ANDROID_API__=21 | |
make clean | |
# Apply patch that fixes the armcap instruction | |
# Linux version | |
# sed -e '/[.]hidden.*OPENSSL_armcap_P/d; /[.]extern.*OPENSSL_armcap_P/ {p; s/extern/hidden/ }' -i -- crypto/*arm*pl crypto/*/asm/*arm*pl | |
# macOS version | |
sed -E -i '' -e '/[.]hidden.*OPENSSL_armcap_P/d' -e '/[.]extern.*OPENSSL_armcap_P/ {p; s/extern/hidden/; }' crypto/*arm*pl crypto/*/asm/*arm*pl | |
make | |
mkdir -p ../$OUTPUT_DIR/${ARCHITECTURE}/lib | |
mkdir -p ../$OUTPUT_DIR/${ARCHITECTURE}/include | |
cp libcrypto.a ../$OUTPUT_DIR/${ARCHITECTURE}/lib/libcrypto.a | |
cp libssl.a ../$OUTPUT_DIR/${ARCHITECTURE}/lib/libssl.a | |
cp -R include/openssl ../$OUTPUT_DIR/${ARCHITECTURE}/include | |
cd .. | |
} | |
build_android_clang "android-arm" "armv7a-linux-androideabi" | |
build_android_clang "android-x86" "i686-linux-android" | |
build_android_clang "android-x86_64" "x86_64-linux-android" | |
build_android_clang "android-arm64" "aarch64-linux-android" | |
export PATH=$PATH_ORG | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment