iOS 开发-编译第三方库 openssl及curl

1、前提

  • iOS编译库需要三个架构,arm64,arm64e,x86_64,其中x86_64为模拟器所需
  • iOS编译库需要下载xcode及对应的command line tool(执行命令时可以自动下载),下载失败需要去官网搜索下载

2、openssl

  • 参考iOS如何编译OpenSSL静态库(openssl版本:1.1.1b)
  • 执行配置命令(路径修改为要生成的目标架构文件夹路径)
  • ./Configure iphoneos-cross --prefix=/Users/xxx/openssl-1.1.1b/openssl_armv7
  • 修改makefile以下项目:
    1. 真机(arm64为例,arm64e修改其中arm64为arm64e一致)

      # sdk上层路径
      CROSS_TOP=/Users/relly/Downloads/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer
      # sdk文件
      CROSS_SDK=iPhoneOS12.1.sdk

      CROSS_COMPILE=
      CC=/Users/relly/Downloads/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch arm64
      CXX=
      CPPFLAGS=
      CFLAGS=-DDSO_DLFCN -DHAVE_DLFCN_H -DNDEBUG -DOPENSSL_THREADS -DOPENSSL_NO_STATIC_ENGINE -DOPENSSL_PIC -DOPENSSLDIR="\"$(OPENSSLDIR)\"" -DENGINESDIR="\"$(ENGINESDIR)\"" -O3 -D_REENTRANT -isysroot $(CROSS_TOP)/SDKs/$(CROSS_SDK) -fno-common

    2. 模拟器

      # sdk上层路径
      CROSS_TOP=/Users/relly/Downloads/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer
      # sdk文件
      CROSS_SDK=iPhoneSimulator12.1.sdk

      CROSS_COMPILE=
      CC=/Users/relly/Downloads/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch x86_64
      CXX=
      CPPFLAGS=
      CFLAGS=-DDSO_DLFCN -DHAVE_DLFCN_H -DNDEBUG -DOPENSSL_THREADS -DOPENSSL_NO_STATIC_ENGINE -DOPENSSL_PIC -DOPENSSLDIR="\"$(OPENSSLDIR)\"" -DENGINESDIR="\"$(ENGINESDIR)\"" -O3 -D_REENTRANT -isysroot $(CROSS_TOP)/SDKs/$(CROSS_SDK) -fno-common

  • 执行合并库命令
    1. lipo -create ../openssl_arm64/lib/libcrypto.a ../openssl_arm64e/lib/libcrypto.a ../openssl_x86_64/lib/libcrypto.a -output ./libcrypto.a

    2. lipo -create ../openssl_arm64/lib/libssl.a ../openssl_arm64e/lib/libssl.a ../openssl_x86_64/lib/libssl.a -output ./libssl.a

3、curl

  • 修改此脚本中的路径为openssl所编译路径后,赋予执行权限执行即可:
#!/bin/sh

make distclean

CONFIGURE_FLAGS="--disable-shared --disable-frontend"

ARCHS="arm64 arm64e x86_64"

# directories
# SOURCE是下载的第三方库源码包,解压后的目录,可以把sh脚本放到这个目录,source改为""
SOURCE=""
# FAT是所有指令集build后,输出的目录,所有静态库被合并成一个静态库
FAT="fat-libtool"

# SCRATCH是下载源码包,解压后的目录
SCRATCH="./"
# must be an absolute path
# THIN 各自指令集build后输出的静态库所在的目录,每个指令集为一个静态库
THIN=`pwd`/"thin-libtool"

COMPILE="y"
LIPO="y"

if [ "$*" ]
then
if [ "$*" = "lipo" ]
then
# skip compile
COMPILE=
else
ARCHS="$*"
if [ $# -eq 1 ]
then
# skip lipo
LIPO=
fi
fi
fi

if [ "$COMPILE" ]
then
CWD=`pwd`
echo "$CWD/$SOURCE........."
for ARCH in $ARCHS
do
echo "building $ARCH..."
mkdir -p "$SCRATCH/$ARCH"
cd "$SCRATCH/$ARCH"

if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]
then
PLATFORM="iPhoneSimulator"
if [ "$ARCH" = "x86_64" ]
then
SIMULATOR="-mios-simulator-version-min=7.0"
HOST=x86_64-apple-darwin
else
SIMULATOR="-mios-simulator-version-min=5.0"
HOST=i386-apple-darwin
fi
else
PLATFORM="iPhoneOS"
SIMULATOR=
HOST=arm-apple-darwin
fi

XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
CC="xcrun -sdk $XCRUN_SDK clang -arch $ARCH"
#AS="$CWD/$SOURCE/extras/gas-preprocessor.pl $CC"
CFLAGS="-stdlib=libc++ -static -arch $ARCH $SIMULATOR"
CXXFLAGS="$CFLAGS"
LDFLAGS="$CFLAGS"

if [ "$ARCH" = "arm64" ]
then
echo 11
SSL_PATH="--with-openssl=/Users/relly/Lib/IOS/openssl-1.1.1t-modified/build/openssl_arm64"
fi

if [ "$ARCH" = "arm64e" ]
then
echo 22
SSL_PATH="--with-openssl=/Users/relly/Lib/IOS/openssl-1.1.1t-modified/build/openssl_arm64e"
fi

if [ "$ARCH" = "x86_64" ]
then
echo 33
SSL_PATH="--with-openssl=/Users/relly/Lib/IOS/openssl-1.1.1t-modified/build/openssl_x86_64"
fi

echo $SSL_PATH
echo " $ARCH ------------------------- $SSL_PATH" 


CC=$CC $CWD/$SOURCE/configure $SSL_PATH --disable-ldap \
$CONFIGURE_FLAGS \
--host=$HOST \
--prefix="$THIN/$ARCH" \
CC="$CC" CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS"

make -j3 install
cd $CWD
done
fi

if [ "$LIPO" ]
then
echo "building fat binaries..."
mkdir -p $FAT/lib
set - $ARCHS
CWD=`pwd`
cd $THIN/$1/lib
for LIB in *.a
do
cd $CWD
lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB
done

cd $CWD
cp -rf $THIN/$1/include $FAT
fi

  • 若出现compiler cannot create executables,执行命令指定xcode路径
    sudo xcode-select --switch /Users/relly/Downloads/Xcode.app/Contents/Developer/

你可能感兴趣的:(ios,openssl,curl)