大家好,我是考100分的小小码 ,祝大家学习进步,加薪顺利呀。今天说一说iOS-OpenSSL 编译,希望您对编程的造诣更进一步.
由于项目更新,需要将OpenSSL库进行升级,之前由于使用的OpenSSL库为1.1.1m,后来不知道听哪里说1.1.1系列后面不再维护。好嘛,那就更新一下。
直接使用官方库进行编译
下面说下自己的步骤:
- 下载最新的OpenSSL-官网,github.
- 解压下载好的源码
- 创建存放编译好好静态库的文件夹:
mkdir openssl_64
- 配置文件夹 打开终端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"
- 执行OpenSSL 提供的编译
./Configure ios64-cross
- make (似乎可有可无)
- 在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.测试是可以正常编译出静态库的。头文件需要在工程中去拷贝,也可以修改脚本一步到位。
这里再介绍一下编译静态库常用的一下命令:
- xcrun -sdk iphoneos –show-sdk-path / xcrun -sdk iphonesimulator –show-sdk-path用来查找真机和模拟器SDK的路径;
- xcrun –find gcc / xcrun –find clang用来查找编译器的路径
- -miphoneos-version-min选项指定最小支持的iOS版本
- -fembed-bitcode选项开启bitcode的支持,去掉就不支持bitcode;
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/13225.html