#!/bin/bash## Copyright (c) 2013 Claudiu-Vlad Ursache <claudiu@cvursache.com># MIT License (see LICENSE.md file)## Based on work by Felix Schulze:## Automatic build script for libssl and libcrypto # for iPhoneOS and iPhoneSimulator## Created by Felix Schulze on 16.12.10.# Copyright 2010 Felix Schulze. All rights reserved.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.# 当执行时使用到未定义过的变量,则显示错误信息set -u
# Setup architectures, library name and other vars + cleanup from previous runs# 四个平台架构标识ARCHS=("arm64""armv7s""armv7""i386")# 四个平台架构分别对应的 SDK 名称SDKS=("iphoneos""iphoneos""iphoneos""macosx")# 使用的 OpenSSL 库版本LIB_NAME="openssl-1.0.2c"# 临时输出目录TEMP_LIB_PATH="/tmp/${LIB_NAME}"LIB_DEST_DIR="lib"HEADER_DEST_DIR="include"rm -rf "${HEADER_DEST_DIR}""${LIB_DEST_DIR}""${TEMP_LIB_PATH}*""${LIB_NAME}"# Unarchive library, then configure and make for specified architectures# 编译静态链接库的函数configure_make(){ARCH=$1; GCC=$2; SDK_PATH=$3;
LOG_FILE="${TEMP_LIB_PATH}-${ARCH}.log" tar xfz "${LIB_NAME}.tar.gz"pushd .; cd"${LIB_NAME}";
./Configure BSD-generic32 --openssldir="${TEMP_LIB_PATH}-${ARCH}" &> "${LOG_FILE}" make CC="${GCC} -arch ${ARCH}"CFLAG="-isysroot ${SDK_PATH}" &> "${LOG_FILE}";
make install &> "${LOG_FILE}";
popd; rm -rf "${LIB_NAME}";
}# 分别开始编译四个平台架构的静态链接库for((i=0; i < ${#ARCHS[@]}; i++))do# 获取 SDK 路径SDK_PATH=$(xcrun -sdk ${SDKS[i]} --show-sdk-path)# 过去 gcc 编译器路径GCC=$(xcrun -sdk ${SDKS[i]} -find gcc)# 编译 configure_make "${ARCHS[i]}""${GCC}""${SDK_PATH}"done# Combine libraries for different architectures into one# Use .a files from the temp directory by providing relative paths# 通过 lipo 命令将四个平台架构的静态库打包成一个静态库create_lib(){LIB_SRC=$1; LIB_DST=$2;
LIB_PATHS=("${ARCHS[@]/#/${TEMP_LIB_PATH}-}")LIB_PATHS=("${LIB_PATHS[@]/%//${LIB_SRC}}") lipo ${LIB_PATHS[@]} -create -output "${LIB_DST}"}mkdir "${LIB_DEST_DIR}";
create_lib "lib/libcrypto.a""${LIB_DEST_DIR}/libcrypto.a"create_lib "lib/libssl.a""${LIB_DEST_DIR}/libssl.a"# Copy header files + final cleanupsmkdir -p "${HEADER_DEST_DIR}"cp -R "${TEMP_LIB_PATH}-${ARCHS[0]}/include""${HEADER_DEST_DIR}"rm -rf "${TEMP_LIB_PATH}-*""{LIB_NAME}"