-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_openssl_ios.sh
More file actions
executable file
·298 lines (246 loc) · 9.04 KB
/
build_openssl_ios.sh
File metadata and controls
executable file
·298 lines (246 loc) · 9.04 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/bin/bash
# Don't exit on error - we want to handle errors gracefully
set +e
echo "Building OpenSSL for iOS..."
# 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 Xcode and command line tools
if ! command -v xcodebuild &> /dev/null; then
print_error "Xcode command line tools not found!"
print_status "Please install Xcode and command line tools:"
print_status "xcode-select --install"
exit 1
fi
print_status "Using Xcode: $(xcode-select -p)"
# iOS deployment target
IOS_MIN="13.0"
print_status "iOS minimum deployment target: $IOS_MIN"
# 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"
# Build staging directory
BUILD_DIR="$(pwd)/build-openssl-ios"
PREFIX_BASE="$(pwd)/third_party/openssl-ios"
# Get SDK paths
SDK_OS=$(xcrun --sdk iphoneos --show-sdk-path)
SDK_SIM=$(xcrun --sdk iphonesimulator --show-sdk-path)
print_status "iOS Device SDK: $SDK_OS"
print_status "iOS Simulator SDK: $SDK_SIM"
# Check if OpenSSL is already built for all required platforms
REQUIRED_PLATFORMS=("iphoneos" "iphonesimulator")
ALL_EXIST=true
for PLATFORM in "${REQUIRED_PLATFORMS[@]}"; do
if [ ! -f "$PREFIX_BASE/$PLATFORM/lib/libssl.a" ] || [ ! -f "$PREFIX_BASE/$PLATFORM/lib/libcrypto.a" ]; then
ALL_EXIST=false
break
fi
done
if [ "$ALL_EXIST" = true ]; then
print_success "OpenSSL already built for all required iOS platforms"
print_status "OpenSSL libraries location: $PREFIX_BASE"
print_status "To rebuild, delete: $PREFIX_BASE"
exit 0
fi
# Clean and create directories
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR"
rm -rf "$PREFIX_BASE"
mkdir -p "$PREFIX_BASE"
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"
# iOS configurations
# Format: "PLATFORM:ARCH:OPENSSL_TARGET:SDK_PATH"
IOS_CONFIGS=(
"iphoneos:arm64:ios64-cross:$SDK_OS"
"iphonesimulator:arm64:iossimulator-xcrun:$SDK_SIM"
"iphonesimulator:x86_64:iossimulator-xcrun:$SDK_SIM"
)
# Build for each configuration
for CONFIG in "${IOS_CONFIGS[@]}"; do
IFS=':' read -r PLATFORM ARCH OPENSSL_TARGET SDK_PATH <<< "$CONFIG"
print_status "Building OpenSSL for iOS $PLATFORM/$ARCH..."
# Clean previous build
make clean > /dev/null 2>&1 || true
PREFIX_DIR="$PREFIX_BASE/$PLATFORM-$ARCH"
# Set deployment target based on platform
if [[ "$PLATFORM" == "iphonesimulator" ]]; then
DEPLOYMENT_FLAG="-mios-simulator-version-min=$IOS_MIN"
if [[ "$ARCH" == "x86_64" ]]; then
export CC="xcrun -sdk iphonesimulator clang -arch x86_64"
else
export CC="xcrun -sdk iphonesimulator clang -arch arm64"
fi
else
DEPLOYMENT_FLAG="-miphoneos-version-min=$IOS_MIN"
export CC="xcrun -sdk iphoneos clang -arch arm64"
fi
# Configure for iOS
./Configure "$OPENSSL_TARGET" \
no-shared \
no-tests \
no-ui-console \
--prefix="$PREFIX_DIR" \
--openssldir="$PREFIX_DIR/ssl" \
-isysroot "$SDK_PATH" \
$DEPLOYMENT_FLAG || {
print_error "Failed to configure OpenSSL for $PLATFORM/$ARCH"
continue
}
# Build OpenSSL
make -j4 build_sw
if [ $? -ne 0 ]; then
print_error "Failed to build OpenSSL for $PLATFORM/$ARCH"
continue
fi
# Install OpenSSL
make install_sw
if [ $? -ne 0 ]; then
print_error "Failed to install OpenSSL for $PLATFORM/$ARCH"
continue
fi
# Verify the build
if [ -f "$PREFIX_DIR/lib/libssl.a" ] && [ -f "$PREFIX_DIR/lib/libcrypto.a" ]; then
print_success "Built and verified OpenSSL for iOS $PLATFORM/$ARCH"
ls -lh "$PREFIX_DIR/lib/"*.a 2>/dev/null || true
else
print_error "OpenSSL libraries not found for $PLATFORM/$ARCH after build"
fi
# Clear CC environment variable
unset CC
done
cd ..
# Create universal simulator libraries
print_status "Creating universal simulator libraries..."
SIMULATOR_DIR="$PREFIX_BASE/iphonesimulator"
mkdir -p "$SIMULATOR_DIR/lib"
mkdir -p "$SIMULATOR_DIR/include"
# Copy headers from any simulator build (they should be the same)
if [ -d "$PREFIX_BASE/iphonesimulator-arm64/include" ]; then
cp -R "$PREFIX_BASE/iphonesimulator-arm64/include/"* "$SIMULATOR_DIR/include/"
fi
# Create universal libraries for simulator
if [ -f "$PREFIX_BASE/iphonesimulator-arm64/lib/libssl.a" ] && [ -f "$PREFIX_BASE/iphonesimulator-x86_64/lib/libssl.a" ]; then
print_status "Creating universal libssl.a for simulator..."
lipo -create \
"$PREFIX_BASE/iphonesimulator-arm64/lib/libssl.a" \
"$PREFIX_BASE/iphonesimulator-x86_64/lib/libssl.a" \
-output "$SIMULATOR_DIR/lib/libssl.a"
print_status "Creating universal libcrypto.a for simulator..."
lipo -create \
"$PREFIX_BASE/iphonesimulator-arm64/lib/libcrypto.a" \
"$PREFIX_BASE/iphonesimulator-x86_64/lib/libcrypto.a" \
-output "$SIMULATOR_DIR/lib/libcrypto.a"
print_success "Created universal simulator libraries"
else
print_warning "Could not create universal simulator libraries - some builds may have failed"
fi
# Copy device libraries to a consistent location
if [ -d "$PREFIX_BASE/iphoneos-arm64" ]; then
print_status "Setting up device libraries..."
cp -R "$PREFIX_BASE/iphoneos-arm64" "$PREFIX_BASE/iphoneos"
print_success "Device libraries ready"
fi
# Create CMake configuration file
print_status "Creating CMake configuration..."
cat > "$PREFIX_BASE/OpenSSLConfig.cmake" << 'EOF'
# OpenSSL iOS Configuration
# Determine platform based on sysroot
if(CMAKE_OSX_SYSROOT MATCHES "simulator")
set(OPENSSL_PLATFORM "iphonesimulator")
else()
set(OPENSSL_PLATFORM "iphoneos")
endif()
set(OPENSSL_ROOT_DIR "${CMAKE_CURRENT_LIST_DIR}/${OPENSSL_PLATFORM}")
set(OPENSSL_INCLUDE_DIR "${OPENSSL_ROOT_DIR}/include")
set(OPENSSL_CRYPTO_LIBRARY "${OPENSSL_ROOT_DIR}/lib/libcrypto.a")
set(OPENSSL_SSL_LIBRARY "${OPENSSL_ROOT_DIR}/lib/libssl.a")
set(OPENSSL_LIBRARIES "${OPENSSL_SSL_LIBRARY};${OPENSSL_CRYPTO_LIBRARY}")
set(OPENSSL_FOUND TRUE)
# Create imported targets
if(NOT TARGET OpenSSL::Crypto)
add_library(OpenSSL::Crypto STATIC IMPORTED)
set_target_properties(OpenSSL::Crypto PROPERTIES
IMPORTED_LOCATION "${OPENSSL_CRYPTO_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIR}"
)
endif()
if(NOT TARGET OpenSSL::SSL)
add_library(OpenSSL::SSL STATIC IMPORTED)
set_target_properties(OpenSSL::SSL PROPERTIES
IMPORTED_LOCATION "${OPENSSL_SSL_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "OpenSSL::Crypto"
)
endif()
if(NOT TARGET OpenSSL::OpenSSL)
add_library(OpenSSL::OpenSSL INTERFACE IMPORTED)
set_target_properties(OpenSSL::OpenSSL PROPERTIES
INTERFACE_LINK_LIBRARIES "OpenSSL::SSL;OpenSSL::Crypto"
)
endif()
EOF
# Final verification - check if all required platforms were built
FINAL_CHECK_FAILED=false
for PLATFORM in "${REQUIRED_PLATFORMS[@]}"; do
if [ ! -f "$PREFIX_BASE/$PLATFORM/lib/libssl.a" ] || [ ! -f "$PREFIX_BASE/$PLATFORM/lib/libcrypto.a" ]; then
print_error "Missing OpenSSL libraries for $PLATFORM"
FINAL_CHECK_FAILED=true
fi
done
if [ "$FINAL_CHECK_FAILED" = true ]; then
print_error "OpenSSL build failed for some platforms"
print_status "You may need to:"
print_status " 1. Check your Xcode 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 iOS build completed successfully!"
print_success "Libraries are available at:"
print_status " • Device libraries: $PREFIX_BASE/iphoneos/lib/"
print_status " • Simulator libraries: $PREFIX_BASE/iphonesimulator/lib/"
print_status " • CMake config: $PREFIX_BASE/OpenSSLConfig.cmake"
# Show usage instructions
print_status ""
print_status "Usage in CMake projects:"
print_status " include(\"\${CMAKE_SOURCE_DIR}/third_party/openssl-ios/OpenSSLConfig.cmake\")"
print_status " target_link_libraries(your_target OpenSSL::OpenSSL)"
# Cleanup
print_status "Cleaning up temporary files..."
rm -f "$OPENSSL_ARCHIVE"
rm -rf "$OPENSSL_DIR"
print_success "OpenSSL iOS build script completed successfully!"
exit 0