由于项目更新,需求将OpenSSL库进行升级,之前由于运用的OpenSSL库为1.1.1m,后来不知道听哪里说1.1.1系列后边不再保护。好嘛,那就更新一下。

直接运用官方库进行编译

下面说下自己的过程:

  1. 下载最新的OpenSSL-官网,github.
  2. 解压下载好的源码
  3. 创立存放编译好好静态库的文件夹:
mkdir openssl_64
  1. 装备文件夹
    打开终端cd 到 OpenSSL源码的途径下,然后:
./Configure iphone-cross --profix=xx/openssl-3.1.0-a/openssl_arm64
1 export CC="clang"
2 export CROSS_TOP="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer"
3 export CROSS_SDK="iPhoneOS.sdk"
  1. 执行OpenSSL 提供的编译
./Configure ios64-cross
  1. make (好像可有可无)
  2. 在OpenSSL源码目录下,能够看到新生成的libcrypto.a libssl.a便是需求的静态库,include目录下便是头文件

运用Github 上脚本编译

假如这个出问题了能够,能够在github上找一些脚本进行编译,可是大家注意自己看脚本,大多脚本会有一些版别限制。这个时候需求依据编译过错进行修正。以下面github上的脚本为例:
openssl_for_ios_and_android

这儿首要记载Openssl的脚本:

脚本分两份,一份是共用文件:build-ios-common.sh

source ./build-common.sh
if [ -z ${api+x} ]; then
 api="8.0"
fi
if [ -z ${arch+x} ]; then
 arch=("armv7" "arm64" "arm64e" "x86_64")
fi
if [ -z ${sdk+x} ]; then
 sdk=("iphoneos" "iphoneos" "iphoneos" "iphonesimulator")
fi
if [ -z ${platform+x} ]; then
 platform=("iPhoneOS" "iPhoneOS" "iphoneos" "iPhoneSimulator")
fi
export PLATFORM_TYPE="iOS"
export IOS_MIN_TARGET="$api"
export ARCHS=(${arch[@]})
export SDKS=(${sdk[@]})
export PLATFORMS=(${platform[@]})
# for test !!!
# export ARCHS=("armv7")
# export SDKS=("iphoneos")
# export PLATFORMS=("iPhoneOS")
function get_ios_arch() {
  local common_arch=$1
  case ${common_arch} in
  armv7)
    echo "armv7"
    ;;
  arm64)
    echo "arm64"
    ;;
  arm64e)
    echo "arm64e"
    ;;
  x86)
    echo "x86"
    ;;
  x86_64)
    echo "x86-64"
    ;;
  esac
}
function ios_get_build_host() {
  local arch=$(get_ios_arch $1)
  case ${arch} in
  armv7)
    echo "armv7-ios-darwin"
    ;;
  arm64)
    echo "aarch64-ios-darwin"
    ;;
  arm64e)
    echo "aarch64-ios-darwin"
    ;;
  x86)
    echo "x86-ios-darwin"
    ;;
  x86-64)
    echo "x86_64-ios-darwin"
    ;;
  esac
}
function set_ios_cpu_feature() {
  local name=$1
  local arch=$(get_ios_arch $2)
  local ios_min_target=$3
  local sysroot=$4
  case ${arch} in
  armv7)
    export CC="xcrun -sdk iphoneos clang -arch armv7"
    export CXX="xcrun -sdk iphoneos clang++ -arch armv7"
    export CFLAGS="-arch armv7 -target armv7-ios-darwin -march=armv7 -mcpu=cortex-a8 -mfpu=neon -mfloat-abi=softfp -Wno-unused-function -fstrict-aliasing -Oz -Wno-ignored-optimization-argument -isysroot ${sysroot} -fembed-bitcode -miphoneos-version-min=${ios_min_target} -I${sysroot}/usr/include"
    export LDFLAGS="-arch armv7 -target armv7-ios-darwin -march=armv7 -isysroot ${sysroot} -fembed-bitcode -L${sysroot}/usr/lib "
    export CXXFLAGS="-std=c++14 -arch armv7 -target armv7-ios-darwin -march=armv7 -mcpu=cortex-a8 -mfpu=neon -mfloat-abi=softfp -fstrict-aliasing -fembed-bitcode -miphoneos-version-min=${ios_min_target} -I${sysroot}/usr/include"
    ;;
  arm64)
    export CC="xcrun -sdk iphoneos clang -arch arm64"
    export CXX="xcrun -sdk iphoneos clang++ -arch arm64"
    export CFLAGS="-arch arm64 -target aarch64-ios-darwin -march=armv8 -mcpu=generic -Wno-unused-function -fstrict-aliasing -Oz -Wno-ignored-optimization-argument -isysroot ${sysroot} -fembed-bitcode -miphoneos-version-min=${ios_min_target} -I${sysroot}/usr/include"
    export LDFLAGS="-arch arm64 -target aarch64-ios-darwin -march=armv8 -isysroot ${sysroot} -fembed-bitcode -L${sysroot}/usr/lib "
    export CXXFLAGS="-std=c++14 -arch arm64 -target aarch64-ios-darwin -march=armv8 -mcpu=generic -fstrict-aliasing -fembed-bitcode -miphoneos-version-min=${ios_min_target} -I${sysroot}/usr/include"
    ;;
  arm64e)
    # -march=armv8.3 ???
    export CC="xcrun -sdk iphoneos clang -arch arm64e"
    export CXX="xcrun -sdk iphoneos clang++ -arch arm64e"
    export CFLAGS="-arch arm64e -target aarch64-ios-darwin -Wno-unused-function -fstrict-aliasing -isysroot ${sysroot} -fembed-bitcode -miphoneos-version-min=${ios_min_target} -I${sysroot}/usr/include"
    export LDFLAGS="-arch arm64e -target aarch64-ios-darwin -isysroot ${sysroot} -fembed-bitcode -L${sysroot}/usr/lib "
    export CXXFLAGS="-std=c++14 -arch arm64e -target aarch64-ios-darwin -fstrict-aliasing -fembed-bitcode -miphoneos-version-min=${ios_min_target} -I${sysroot}/usr/include"
    ;;
  x86)
    export CC="xcrun -sdk iphonesimulator clang -arch x86"
    export CXX="xcrun -sdk iphonesimulator clang++ -arch x86"
    expor CFLAGS="-arch x86 -target x86-ios-darwin -march=i386 -msse4.2 -mpopcnt -m64 -mtune=intel -Wno-unused-function -fstrict-aliasing -O2 -Wno-ignored-optimization-argument -isysroot ${sysroot} -mios-simulator-version-min=${ios_min_target} -I${sysroot}/usr/include"
    export LDFLAGS="-arch x86 -target x86-ios-darwin -march=i386 -isysroot ${sysroot} -L${sysroot}/usr/lib "
    export CXXFLAGS="-std=c++14 -arch x86 -target x86-ios-darwin -march=i386 -msse4.2 -mpopcnt -m64 -mtune=intel -fstrict-aliasing -mios-simulator-version-min=${ios_min_target} -I${sysroot}/usr/include"
    ;;
  x86-64)
    expor CC="xcrun -sdk iphonesimulator clang -arch x86_64"
    export CXX="xcrun -sdk iphonesimulator clang++ -arch x86_64"
    export CFLAGS="-arch x86_64 -target x86_64-ios-darwin -march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel -Wno-unused-function -fstrict-aliasing -O2 -Wno-ignored-optimization-argument -isysroot ${sysroot} -mios-simulator-version-min=${ios_min_target} -I${sysroot}/usr/include"
    export LDFLAGS="-arch x86_64 -target x86_64-ios-darwin -march=x86-64 -isysroot ${sysroot} -L${sysroot}/usr/lib "
    export CXXFLAGS="-std=c++14 -arch x86_64 -target x86_64-ios-darwin -march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel -fstrict-aliasing -mios-simulator-version-min=${ios_min_target} -I${sysroot}/usr/include"
    ;;
  *)
    log_error "not support" && exit 1
    ;;
  esac
}
function ios_printf_global_params() {
  local arch=$1
  local type=$2
  local platform=$3
  local in_dir=$4
  local out_dir=$5
  echo -e "arch =      $arch"
  echo -e "type =      $type"
  echo -e "platform =    $platform"
  echo -e "PLATFORM_TYPE = $PLATFORM_TYPE"
  echo -e "IOS_MIN_TARGET = $IOS_MIN_TARGET"
  echo -e "in_dir =     $in_dir"
  echo -e "out_dir =    $out_dir"
  echo -e "CC =       $CC"
  echo -e "CXX =      $CXX"
  echo -e "CFLAGS =     $CFLAGS"
  echo -e "CXXFLAGS =    $CXXFLAGS"
  echo -e "LDFLAGS =    $LDFLAGS"
}

针对Openssl的编译文件:

set -u
source ./build-ios-common.sh
if [ -z ${version+x} ]; then
 version="1.1.1d"
fi
TOOLS_ROOT=$(pwd)
SOURCE="$0"
while [ -h "$SOURCE" ]; do
  DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
pwd_path="$(cd -P "$(dirname "$SOURCE")" && pwd)"
echo pwd_path=${pwd_path}
echo TOOLS_ROOT=${TOOLS_ROOT}
# openssl-1.1.0f has a configure bug
# openssl-1.1.1d has fix configure bug
LIB_VERSION="OpenSSL_$(echo $version | sed 's/\./_/g')"
LIB_NAME="openssl-$version"
LIB_DEST_DIR="${pwd_path}/../output/ios/openssl-universal"
init_log_color
echo "https://www.openssl.org/source/${LIB_NAME}.tar.gz"
# https://github.com/openssl/openssl/archive/OpenSSL_1_1_1d.tar.gz
# https://github.com/openssl/openssl/archive/OpenSSL_1_1_1f.tar.gz
DEVELOPER=$(xcode-select -print-path)
rm -rf "${LIB_DEST_DIR}" "${LIB_NAME}"
[ -f "${LIB_NAME}.tar.gz" ] || curl https://www.openssl.org/source/${LIB_NAME}.tar.gz >${LIB_NAME}.tar.gz
function configure_make() {
  ARCH=$1
  SDK=$2
  PLATFORM=$3
  SDK_PATH=$(xcrun -sdk ${SDK} --show-sdk-path)
  log_info "configure $ARCH start..."
  if [ -d "${LIB_NAME}" ]; then
    rm -fr "${LIB_NAME}"
  fi
  tar xfz "${LIB_NAME}.tar.gz"
  pushd .
  cd "${LIB_NAME}"
  PREFIX_DIR="${pwd_path}/../output/ios/openssl-${ARCH}"
  if [ -d "${PREFIX_DIR}" ]; then
    rm -fr "${PREFIX_DIR}"
  fi
  mkdir -p "${PREFIX_DIR}"
  OUTPUT_ROOT=${TOOLS_ROOT}/../output/ios/openssl-${ARCH}
  mkdir -p ${OUTPUT_ROOT}/log
  set_ios_cpu_feature "openssl" "${ARCH}" "${IOS_MIN_TARGET}" "${SDK_PATH}"
  ios_printf_global_params "$ARCH" "$SDK" "$PLATFORM" "$PREFIX_DIR" "$OUTPUT_ROOT"
  unset IPHONEOS_DEPLOYMENT_TARGET
  if [[ "${ARCH}" == "x86_64" ]]; then
    # openssl1.1.1d can be set normally, 1.1.0f does not take effect
    ./Configure darwin64-x86_64-cc no-shared --prefix="${PREFIX_DIR}"
  elif [[ "${ARCH}" == "armv7" ]]; then
    # openssl1.1.1d can be set normally, 1.1.0f does not take effect
    ./Configure iphoneos-cross no-shared --prefix="${PREFIX_DIR}"
    sed -ie "s!-fno-common!-fno-common -fembed-bitcode !" "Makefile"
  elif [[ "${ARCH}" == "arm64" ]]; then
    # openssl1.1.1d can be set normally, 1.1.0f does not take effect
    ./Configure iphoneos-cross no-shared --prefix="${PREFIX_DIR}"
    sed -ie "s!-fno-common!-fno-common -fembed-bitcode !" "Makefile"
  elif [[ "${ARCH}" == "arm64e" ]]; then
    # openssl1.1.1d can be set normally, 1.1.0f does not take effect
    ./Configure iphoneos-cross no-shared --prefix="${PREFIX_DIR}"
    sed -ie "s!-fno-common!-fno-common -fembed-bitcode !" "Makefile"
  else
    log_error "not support" && exit 1
  fi
  log_info "make $ARCH start..."
  make clean >"${OUTPUT_ROOT}/log/${ARCH}.log"
  if make -j8 >>"${OUTPUT_ROOT}/log/${ARCH}.log" 2>&1; then
    make install_sw >>"${OUTPUT_ROOT}/log/${ARCH}.log" 2>&1
    make install_ssldirs >>"${OUTPUT_ROOT}/log/${ARCH}.log" 2>&1
  fi
  popd
}
log_info "${PLATFORM_TYPE} ${LIB_NAME} start..."
for ((i = 0; i < ${#ARCHS[@]}; i++)); do
  if [[ $# -eq 0 || "$1" == "${ARCHS[i]}" ]]; then
    configure_make "${ARCHS[i]}" "${SDKS[i]}" "${PLATFORMS[i]}"
  fi
done
log_info "lipo start..."
function lipo_library() {
  LIB_SRC=$1
  LIB_DST=$2
  LIB_PATHS=("${ARCHS[@]/#/${pwd_path}/../output/ios/openssl-}")
  LIB_PATHS=("${LIB_PATHS[@]/%//lib/${LIB_SRC}}")
  lipo ${LIB_PATHS[@]} -create -output "${LIB_DST}"
}
mkdir -p "${LIB_DEST_DIR}"
lipo_library "libcrypto.a" "${LIB_DEST_DIR}/libcrypto-universal.a"
lipo_library "libssl.a" "${LIB_DEST_DIR}/libssl-universal.a"
log_info "${PLATFORM_TYPE} ${LIB_NAME} end..."

编译OpenSSL3.x以上版别请继续

假如你只是需求一个Openssl最新版,那么祝贺你,上面的就够用了。可是假如你需求的是3.x以上版别,直接改上面脚本的版别号去编译,那么应该是编译不出库的。遇到问题不要慌,能够看output文件夹下的log,进去看,发现是bitcode在作怪,那么问题就好办了,修正通用文件(build-ios-common.sh)中:

表示支撑Bitcode,对应工程中build setting中的bitcode YES
-fembed-bitcode

删除去该选项。然后再运用新的OpenSSL版别号,比如:3.1.0.测试是能够正常编译出静态库的。头文件需求在工程中去复制,也能够修正脚本一步到位。

这儿再介绍一下编译静态库常用的一下命令:

  1. xcrun -sdk iphoneos –show-sdk-path /xcrun -sdk iphonesimulator –show-sdk-path用来查找真机和模拟器SDK的途径;
  2. xcrun –find gcc /xcrun –find clang用来查找编译器的途径
  3. -miphoneos-version-min选项指定最小支撑的iOS版别
  4. -fembed-bitcode选项开启bitcode的支撑,去掉就不支撑bitcode;