-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_openssl_android.sh
More file actions
executable file
·181 lines (148 loc) · 5.03 KB
/
build_openssl_android.sh
File metadata and controls
executable file
·181 lines (148 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/bash
# Don't exit on error - we want to handle errors gracefully
set +e
echo "Building OpenSSL for Android..."
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
# Check for Android NDK
if [ -z "$ANDROID_NDK" ]; then
if [ -d "$HOME/Library/Android/sdk/ndk" ]; then
# Try to find NDK automatically
ANDROID_NDK=$(find "$HOME/Library/Android/sdk/ndk" -maxdepth 1 -type d | grep -E '[0-9]' | sort -V | tail -1)
print_status "Found Android NDK at: $ANDROID_NDK"
else
print_error "Android NDK not found!"
print_status "Please set ANDROID_NDK environment variable or install Android NDK"
print_status "Example: export ANDROID_NDK=/path/to/android-ndk"
exit 1
fi
fi
print_status "Using Android NDK: $ANDROID_NDK"
# OpenSSL version and directories
OPENSSL_VERSION="3.1.4"
OPENSSL_DIR="openssl-$OPENSSL_VERSION"
OPENSSL_ARCHIVE="$OPENSSL_DIR.tar.gz"
OPENSSL_URL="https://www.openssl.org/source/$OPENSSL_ARCHIVE"
# Common staging directory
PREFIX_BASE="$(pwd)/third_party/openssl-android"
# Check if OpenSSL is already built for all required ABIs
REQUIRED_ABIS=("arm64-v8a" "x86_64")
ALL_EXIST=true
for ABI in "${REQUIRED_ABIS[@]}"; do
if [ ! -f "$PREFIX_BASE/$ABI/lib/libssl.a" ] || [ ! -f "$PREFIX_BASE/$ABI/lib/libcrypto.a" ]; then
ALL_EXIST=false
break
fi
done
if [ "$ALL_EXIST" = true ]; then
print_success "OpenSSL already built for all required ABIs"
print_status "OpenSSL libraries location: $PREFIX_BASE"
print_status "To rebuild, delete: $PREFIX_BASE"
exit 0
fi
# Set up environment
export ANDROID_NDK_ROOT="$ANDROID_NDK"
export PATH="$ANDROID_NDK/toolchains/llvm/prebuilt/$(uname -s | tr '[:upper:]' '[:lower:]')-x86_64/bin:$PATH"
print_status "Setting up OpenSSL source..."
# Download and extract OpenSSL if not already present
if [ ! -d "$OPENSSL_DIR" ]; then
if [ ! -f "$OPENSSL_ARCHIVE" ]; then
print_status "Downloading OpenSSL $OPENSSL_VERSION..."
curl -L -o "$OPENSSL_ARCHIVE" "$OPENSSL_URL" || {
print_error "Failed to download OpenSSL"
exit 1
}
fi
print_status "Extracting OpenSSL..."
tar -xzf "$OPENSSL_ARCHIVE" || {
print_error "Failed to extract OpenSSL"
exit 1
}
fi
cd "$OPENSSL_DIR"
# Android ABIs and their corresponding OpenSSL targets
# Format: "ABI:OPENSSL_TARGET:MIN_API_LEVEL"
# Only build the ABIs we actually need
ANDROID_CONFIGS=(
"arm64-v8a:android-arm64:21"
"x86_64:android-x86_64:21"
)
# Build for each ABI
for CONFIG in "${ANDROID_CONFIGS[@]}"; do
IFS=':' read -r ABI OPENSSL_TARGET API_LEVEL <<< "$CONFIG"
print_status "Building OpenSSL for Android $ABI (API level $API_LEVEL)..."
# Clean previous build
make clean > /dev/null 2>&1 || true
# Configure OpenSSL for this ABI
./Configure "$OPENSSL_TARGET" \
-D__ANDROID_API__="$API_LEVEL" \
--prefix="$PREFIX_BASE/$ABI" \
--openssldir="$PREFIX_BASE/$ABI/ssl" \
no-shared \
no-tests \
no-ui-console \
no-asm || {
print_error "Failed to configure OpenSSL for $ABI"
continue
}
# Build OpenSSL
make -j$(nproc) || {
print_error "Failed to build OpenSSL for $ABI"
continue
}
# Install OpenSSL
make install_sw
if [ $? -ne 0 ]; then
print_error "Failed to install OpenSSL for $ABI"
continue
fi
# Verify the build
if [ -f "$PREFIX_BASE/$ABI/lib/libssl.a" ] && [ -f "$PREFIX_BASE/$ABI/lib/libcrypto.a" ]; then
print_success "Built and verified OpenSSL for Android $ABI"
ls -lh "$PREFIX_BASE/$ABI/lib/lib*.a" 2>/dev/null || true
else
print_error "OpenSSL libraries not found for $ABI after build"
fi
done
cd ..
# Final verification - check if all required ABIs were built
FINAL_CHECK_FAILED=false
for ABI in "${REQUIRED_ABIS[@]}"; do
if [ ! -f "$PREFIX_BASE/$ABI/lib/libssl.a" ] || [ ! -f "$PREFIX_BASE/$ABI/lib/libcrypto.a" ]; then
print_error "Missing OpenSSL libraries for $ABI"
FINAL_CHECK_FAILED=true
fi
done
if [ "$FINAL_CHECK_FAILED" = true ]; then
print_error "OpenSSL build failed for some ABIs"
print_status "You may need to:"
print_status " 1. Check your Android NDK installation"
print_status " 2. Delete $PREFIX_BASE and try again"
print_status " 3. Check build logs above for errors"
exit 1
fi
print_success "OpenSSL build completed successfully!"
print_status "OpenSSL libraries are available in: $PREFIX_BASE"
print_status "You can now build your project with SSL support enabled."
# Cleanup
print_status "Cleaning up temporary files..."
rm -f "$OPENSSL_ARCHIVE"
rm -rf "$OPENSSL_DIR"
print_success "OpenSSL Android build script completed successfully!"
exit 0