Mac 编译OpenSSL 静态库、动态链接库

1、编译环境

Mac系统版本:10.15.2

Android NDK版本:android-ndk-r14b

OpenSSL地址:https://www.openssl.org/source/

OpenSSL github:https://github.com/openssl/openssl

编译脚本地址 github:https://github.com/leenjewel/openssl_for_ios_and_android

2、下载编译脚本

openssl_for_ios_and_android

|____ example

|____ tools

        |____ _shared.sh
        |____ build-curl4android.sh
        |____ build-curl4ios.sh
        |____ build-openssl4android.sh
        |____ build-openssl4ios.sh
        |____ build-openssl_111_4android.sh

_shared.sh

#!/bin/bash

TOOLS_ROOT=`pwd`

#
# Warning !!! Android Build !!!
#
# Default to API 21 for it is the minimum requirement for 64 bit archs.
# IF you need to build for min api level 16, you need to modify it to 14 and will not build for 64 bit archs.
# api level 16 build is better because api level 21 and higher may have problem like
#
#     https://github.com/openssl/openssl/issues/988
#     http://stackoverflow.com/questions/37122126/whats-the-exact-significance-of-android-ndk-platform-version-compared-to-api-le
#
# So if you not need 64 bit arch api level 16 is better
#
# But but but cURL could not build by android-20 and earlier :-(
# So you can build openssl with android-16 then build cURL with android-21
#
if [ "${1}" == "cURL" ]; then
    ANDROID_API=${ANDROID_API:-21}
else
    ANDROID_API=${ANDROID_API:-16}
fi
echo ANDROID_API=${ANDROID_API}
# ARCHS=("android" "android-armeabi" "android-x86" "android-mips")
# ABIS=("armeabi" "armeabi-v7a" "x86" "mips")
ANDROID_API=${ANDROID_API:-21}
echo ANDROID_API=${ANDROID_API}
ANDROID_API=21
echo ANDROID_API=${ANDROID_API}
read -n1 -p "Press any key to continue..."

# ARCHS=("android" "android-armeabi" "android64-aarch64" "android-x86" "android64" "android-mips" "android-mips64")
# ABIS=("armeabi" "armeabi-v7a" "arm64-v8a" "x86" "x86_64" "mips" "mips64")

ARCHS=("android" "android-armeabi" "android64-aarch64" "android-x86" "android64")
ABIS=("armeabi" "armeabi-v7a" "arm64-v8a" "x86" "x86_64")

ANDROID_NDK=/Users/yu.zuo/Library/Android/sdk/ndk-bundle
NDK=${ANDROID_NDK}

configure_macro() {
  ARCH=$1; OUT=$2; CLANG=${3:-""};
  CLANG="TEST_CLANG"
  echo "CLANG" ${CLANG}

  TOOLCHAIN_ROOT=${TOOLS_ROOT}/${OUT}-android-toolchain

  if [ "$ARCH" == "android" ]; then
    export ARCH_FLAGS="-mthumb"
    export ARCH_LINK=""
    export TOOL="arm-linux-androideabi"
    NDK_FLAGS="--arch=arm"
  elif [ "$ARCH" == "android-armeabi" ]; then
    export ARCH_FLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -mfpu=neon"
    export ARCH_LINK="-march=armv7-a -Wl,--fix-cortex-a8"
    export TOOL="arm-linux-androideabi"
    NDK_FLAGS="--arch=arm"
  elif [ "$ARCH" == "android64-aarch64" ]; then
    export ARCH_FLAGS=""
    export ARCH_LINK=""
    export TOOL="aarch64-linux-android"
    NDK_FLAGS="--arch=arm64"
  elif [ "$ARCH" == "android-x86" ]; then
    export ARCH_FLAGS="-march=i686 -mtune=intel -msse3 -mfpmath=sse -m32"
    export ARCH_LINK=""
    export TOOL="i686-linux-android"
    NDK_FLAGS="--arch=x86"
  elif [ "$ARCH" == "android64" ]; then
    export ARCH_FLAGS="-march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel"
    export ARCH_LINK=""
    export TOOL="x86_64-linux-android"
    NDK_FLAGS="--arch=x86_64"
  elif [ "$ARCH" == "android-mips" ]; then
    export ARCH_FLAGS=""
    export ARCH_LINK=""
    export TOOL="mipsel-linux-android"
    NDK_FLAGS="--arch=mips"
  elif [ "$ARCH" == "android-mips64" ]; then
    export ARCH="linux64-mips64"
    export ARCH_FLAGS=""
    export ARCH_LINK=""
    export TOOL="mips64el-linux-android"
    NDK_FLAGS="--arch=mips64"
  else
    echo "not support."
  fi;

  [ -d ${TOOLCHAIN_ROOT} ] || python $NDK/build/tools/make_standalone_toolchain.py \
                                     --api ${ANDROID_API} \
                                     --stl libc++ \
                                     --install-dir=${TOOLCHAIN_ROOT} \
                                     $NDK_FLAGS
  # [ -d ${TOOLCHAIN_ROOT} ] || sh $NDK/build/tools/make-standalone-toolchain.sh --install-dir=${TOOLCHAIN_ROOT} $NDK_FLAGS

  export TOOLCHAIN_PATH=${TOOLCHAIN_ROOT}/bin
  export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
  export SYSROOT=${TOOLCHAIN_ROOT}/sysroot
  export CROSS_SYSROOT=$SYSROOT
  if [ -z "${CLANG}" ]; then
    export CC=${NDK_TOOLCHAIN_BASENAME}-gcc
    export CXX=${NDK_TOOLCHAIN_BASENAME}-g++
  else
    export CC=${NDK_TOOLCHAIN_BASENAME}-clang
    export CXX=${NDK_TOOLCHAIN_BASENAME}-clang++
  fi;
  export LINK=${CXX}
  export LD=${NDK_TOOLCHAIN_BASENAME}-ld
  export AR=${NDK_TOOLCHAIN_BASENAME}-ar
  export RANLIB=${NDK_TOOLCHAIN_BASENAME}-ranlib
  export STRIP=${NDK_TOOLCHAIN_BASENAME}-strip
  export CPPFLAGS=${CPPFLAGS:-""}
  export LIBS=${LIBS:-""}
  export CFLAGS="${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64"
  export CXXFLAGS="${CFLAGS} -std=c++11 -frtti -fexceptions"
  export LDFLAGS="${ARCH_LINK}"
  echo "**********************************************"
  echo "use ANDROID_API=${ANDROID_API}"
  echo "use NDK=${NDK}"
  echo "export ARCH=${ARCH}"
  echo "export NDK_TOOLCHAIN_BASENAME=${NDK_TOOLCHAIN_BASENAME}"
  echo "export SYSROOT=${SYSROOT}"
  echo "export CC=${CC}"
  echo "export CXX=${CXX}"
  echo "export LINK=${LINK}"
  echo "export LD=${LD}"
  echo "export AR=${AR}"
  echo "export RANLIB=${RANLIB}"
  echo "export STRIP=${STRIP}"
  echo "export CPPFLAGS=${CPPFLAGS}"
  echo "export CFLAGS=${CFLAGS}"
  echo "export CXXFLAGS=${CXXFLAGS}"
  echo "export LDFLAGS=${LDFLAGS}"
  echo "export LIBS=${LIBS}"
  echo "**********************************************"
}

build-curl4android.sh

#!/bin/bash
#
# Copyright 2016 leenjewel
# 
# 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

source ./_shared.sh cURL

# Setup architectures, library name and other vars + cleanup from previous runs
TOOLS_ROOT=`pwd`
LIB_NAME="curl-7.66.0"
LIB_DEST_DIR=${TOOLS_ROOT}/libs
[ -f ${LIB_NAME}.tar.gz ] || wget https://curl.haxx.se/download/${LIB_NAME}.tar.gz
# Unarchive library, then configure and make for specified architectures
configure_make() {
  ARCH=$1; ABI=$2;
  [ -d "${LIB_NAME}" ] && rm -rf "${LIB_NAME}"
  tar xfz "${LIB_NAME}.tar.gz"
  pushd "${LIB_NAME}";

  configure_macro $*
  # fix me
  echo ARCH=${ARCH}
  echo ABI=${ABI}
  echo SYSROOT=${SYSROOT}
  cp ${TOOLS_ROOT}/../output/android/openssl-${ABI}/lib/libssl.a ${SYSROOT}/usr/lib
  cp ${TOOLS_ROOT}/../output/android/openssl-${ABI}/lib/libcrypto.a ${SYSROOT}/usr/lib
  cp -r ${TOOLS_ROOT}/../output/android/openssl-${ABI}/include/openssl ${SYSROOT}/usr/include

  mkdir -p ${LIB_DEST_DIR}/${ABI}

  ./configure --prefix=${LIB_DEST_DIR}/${ABI} --with-sysroot=${SYSROOT} --host=${TOOL} \
    --enable-static \
    --disable-shared \
    --enable-threaded-resolver \
    --enable-ipv6 \
    --with-ssl=${SYSROOT}/usr 

  PATH=$TOOLCHAIN_PATH:$PATH
  make clean
  if make -j4
  then
    make install

    OUTPUT_ROOT=${TOOLS_ROOT}/../output/android/curl-${ABI}
    [ -d ${OUTPUT_ROOT}/include ] || mkdir -p ${OUTPUT_ROOT}/include
    cp -r ${LIB_DEST_DIR}/${ABI}/include/curl ${OUTPUT_ROOT}/include

    [ -d ${OUTPUT_ROOT}/lib ] || mkdir -p ${OUTPUT_ROOT}/lib
    cp ${LIB_DEST_DIR}/${ABI}/lib/libcurl.a ${OUTPUT_ROOT}/lib
  fi;
  popd;
}

for ((i=0; i < ${#ARCHS[@]}; i++))
do
  if [[ $# -eq 0 ]] || [[ "$1" == "${ARCHS[i]}" ]]; then
    [[ ${ANDROID_API} < 21 ]] && ( echo "${ABIS[i]}" | grep 64 > /dev/null ) && continue;
    configure_make "${ARCHS[i]}" "${ABIS[i]}"
  fi
done

build-curl4ios.sh

#!/bin/bash
#
# Copyright 2016 leenjewel
# 
# 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

SOURCE="$0"
echo SOURCE=${SOURCE}
read -n1 -p "Press any key to continue..."
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 )"

 
# Setup architectures, library name and other vars + cleanup from previous runs
ARCHS=("arm64" "armv7s" "armv7" "i386" "x86_64")
SDKS=("iphoneos" "iphoneos" "iphoneos" "iphonesimulator" "iphonesimulator")
PLATFORMS=("iPhoneOS" "iPhoneOS" "iPhoneOS" "iPhoneSimulator" "iPhoneSimulator")
LIB_NAME="curl-7.66.0"
# LIB_NAME="curl-7_53_1"
DEVELOPER=`xcode-select -print-path`
# TOOLCHAIN=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
TOOLCHAIN=${DEVELOPER}/Toolchains/XcodeDefault.xctoolchain
echo TOOLCHAIN=${TOOLCHAIN}
read -n1 -p "Press any key to continue..."

# If you can't compile with this version, please modify the version to it which on your mac.
# SDK_VERSION=""10.3""
SDK_VERSION=""
IPHONEOS_DEPLOYMENT_TARGET="9.0"
LIB_DEST_DIR="${pwd_path}/../output/ios/curl-universal"
HEADER_DEST_DIR="include"
rm -rf "${HEADER_DEST_DIR}" "${LIB_DEST_DIR}" "${LIB_NAME}"

# https://github.com/openssl/openssl/archive/OpenSSL_1_1_1d.tar.gz
# https://github.com/curl/curl/archive/curl-7_66_0.tar.gz #not
# https://github.com/curl/curl/releases/download/curl-7_66_0/curl-7.66.0.tar.gz
   [ -f "${LIB_NAME}.tar.gz" ] || curl https://curl.haxx.se/download/${LIB_NAME}.tar.gz > ${LIB_NAME}.tar.gz
   

 
# Unarchive library, then configure and make for specified architectures
configure_make()
{
   ARCH=$1; SDK=$2; PLATFORM=$3;
   echo ARCH=$1
   echo SDK=$2
   echo PLATFORM=$3
#    read -n1 -p "Press any key to continue..."

   export PATH="${TOOLCHAIN}/usr/bin:${PATH}"
   export CROSS_TOP="${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer"
   export CROSS_SDK="${PLATFORM}${SDK_VERSION}.sdk"
   export SYSROOT="${CROSS_TOP}/SDKs/${CROSS_SDK}"
   export CFLAGS="-arch ${ARCH} -pipe -Os -gdwarf-2 -isysroot ${SYSROOT} -miphoneos-version-min=${IPHONEOS_DEPLOYMENT_TARGET} -fembed-bitcode"
   export LDFLAGS="-arch ${ARCH} -isysroot ${SYSROOT}"

   if [ -d "${LIB_NAME}" ]; then
       rm -fr "${LIB_NAME}"
   fi

   tar xfz "${LIB_NAME}.tar.gz"
   pushd .; cd "${LIB_NAME}";
    # read -n1 -p "Press any key to continue..."

   PREFIX_DIR="${pwd_path}/../output/ios/curl-${ARCH}"
   if [ -d "${PREFIX_DIR}" ]; then
       rm -fr "${PREFIX_DIR}"
   fi
   mkdir -p "${PREFIX_DIR}"

   if [ "${ARCH}" == "arm64" ]; then
       HOST="--host=arm-apple-darwin"
   else
       HOST="--host=${ARCH}-apple-darwin"
   fi
   ./configure --prefix=${PREFIX_DIR} \
       --with-sysroot=${CROSS_TOP}/SDKs/${CROSS_SDK} \
       ${HOST} \
       --with-darwinssl \
       --enable-static \
       --disable-shared \
       --disable-verbose \
       --enable-threaded-resolver \
       --enable-ipv6
   make clean
   if make -j8
   then
       if [[ -d "curl-${ARCH}" ]]; then
           rm -fr "curl-${ARCH}"
       fi
       mkdir -p "curl-${ARCH}"
       make install
       popd; rm -fr ${LIB_NAME}
   fi
}
for ((i=0; i < ${#ARCHS[@]}; i++))
do
    if [[ $# -eq 0 || "$1" == "${ARCHS[i]}" ]]; then
        configure_make "${ARCHS[i]}" "${SDKS[i]}" "${PLATFORMS[i]}"
    fi
done

# Combine libraries for different architectures into one
# Use .a files from the temp directory by providing relative paths
create_lib()
{
   LIB_SRC=$1; LIB_DST=$2;
   LIB_PATHS=( "${ARCHS[@]/#/${pwd_path}/../output/ios/curl-}" )
   LIB_PATHS=( "${LIB_PATHS[@]/%//${LIB_SRC}}" )
   lipo ${LIB_PATHS[@]} -create -output "${LIB_DST}"
}
mkdir "${LIB_DEST_DIR}";
create_lib "lib/libcurl.a" "${LIB_DEST_DIR}/libcurl.a"
 

build-openssl4android.sh

#!/bin/bash
#
# Copyright 2016 leenjewel
# 
# 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 -e

source ./_shared.sh openssl

# Setup architectures, library name and other vars + cleanup from previous runs
LIB_NAME="openssl-1.1.0f"
LIB_DEST_DIR=${TOOLS_ROOT}/libs
echo LIB_DEST_DIR="${LIB_DEST_DIR}"
read -n1 -p "Press any key to continue..."
[ -d ${LIB_DEST_DIR} ] && rm -rf ${LIB_DEST_DIR}
[ -f "${LIB_NAME}.tar.gz" ] || wget https://www.openssl.org/source/${LIB_NAME}.tar.gz;
# Unarchive library, then configure and make for specified architectures
configure_make() {
  ARCH=$1; ABI=$2;
  rm -rf "${LIB_NAME}"
  tar xfz "${LIB_NAME}.tar.gz"
  pushd "${LIB_NAME}"

  configure_macro $*

  #support openssl-1.0.x
  if [[ $LIB_NAME != openssl-1.1.* ]]; then
    echo LIB_NAME="${LIB_NAME}"
    if [[ $ARCH == "android-armeabi" ]]; then
        ARCH="android-armv7"
    elif [[ $ARCH == "android64" ]]; then 
        ARCH="linux-x86_64 shared no-ssl2 no-ssl3 no-hw "
    elif [[ "$ARCH" == "android64-aarch64" ]]; then
        ARCH="android shared no-ssl2 no-ssl3 no-hw "
    fi
  fi
  # read -n1 -p "Press any key to continue..."

  ./Configure $ARCH \
              --prefix=${LIB_DEST_DIR}/${ABI} \
              --with-zlib-include=$SYSROOT/usr/include \
              --with-zlib-lib=$SYSROOT/usr/lib \
              zlib \
              no-asm \
              no-shared \
              no-unit-test
  PATH=$TOOLCHAIN_PATH:$PATH

  sed -ie 's/-mandroid//g' "Makefile"

  OUTPUT_ROOT=${TOOLS_ROOT}/../output/android/openssl-${ABI}
  mkdir -p ${OUTPUT_ROOT}/log

  make clean &> "${OUTPUT_ROOT}/log/${ABI}.log"
  
  if make -j4 >> "${OUTPUT_ROOT}/log/${ABI}.log" 2>&1; then
    # make install
    make install_sw >> "${OUTPUT_ROOT}/log/${ABI}.log" 2>&1
    make install_ssldirs >> "${OUTPUT_ROOT}/log/${ABI}.log" 2>&1

    OUTPUT_ROOT=${TOOLS_ROOT}/../output/android/openssl-${ABI}
    [ -d ${OUTPUT_ROOT}/include ] || mkdir -p ${OUTPUT_ROOT}/include
    cp -r ${LIB_DEST_DIR}/${ABI}/include/openssl ${OUTPUT_ROOT}/include

    [ -d ${OUTPUT_ROOT}/lib ] || mkdir -p ${OUTPUT_ROOT}/lib
    cp ${LIB_DEST_DIR}/${ABI}/lib/libcrypto.a ${OUTPUT_ROOT}/lib
    cp ${LIB_DEST_DIR}/${ABI}/lib/libssl.a ${OUTPUT_ROOT}/lib
  fi;

  popd

}

for ((i=0; i < ${#ARCHS[@]}; i++))
do
  echo ${ARCHS[i]}
  echo $1
  if [[ $# -eq 0 ]] || [[ "$1" == "${ARCHS[i]}" ]]; then
    # Do not build 64 bit arch if ANDROID_API is less than 21 which is
    # the minimum supported API level for 64 bit.
    [[ ${ANDROID_API} < 21 ]] && ( echo "${ABIS[i]}" | grep 64 > /dev/null ) && continue;
    configure_make "${ARCHS[i]}" "${ABIS[i]}"
  fi
done

build-openssl4ios.sh

#!/bin/bash
#
# Copyright 2016 leenjewel
# 
# 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

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}
# read -n1 -p "Press any key to continue..."

# Setting
IOS_MIN_TARGET="8.0"
LIB_NAME="openssl-1.1.0f"
LIB_DEST_DIR="${pwd_path}/../output/ios/openssl-universal"
HEADER_DEST_DIR="include"

# Setup architectures, library name and other vars + cleanup from previous runs
# ARCHS=("arm64" "armv7s" "armv7" "i386" "x86_64")
# SDKS=("iphoneos" "iphoneos" "iphoneos" "iphonesimulator" "iphonesimulator")
# PLATFORMS=("iPhoneOS" "iPhoneOS" "iPhoneOS" "iPhoneSimulator" "iPhoneSimulator")

ARCHS=("arm64" "armv7s" "armv7" "x86_64")
SDKS=("iphoneos" "iphoneos" "iphoneos" "iphonesimulator")
PLATFORMS=("iPhoneOS" "iPhoneOS" "iPhoneOS" "iPhoneSimulator")

# ARCHS=("arm64")
# SDKS=("iphoneos")
# PLATFORMS=("iPhoneOS")

DEVELOPER=`xcode-select -print-path`
SDK_VERSION=`xcrun -sdk iphoneos --show-sdk-version`
rm -rf "${HEADER_DEST_DIR}" "${LIB_DEST_DIR}" "${LIB_NAME}"
[ -f "${LIB_NAME}.tar.gz" ] || curl https://www.openssl.org/source/${LIB_NAME}.tar.gz > ${LIB_NAME}.tar.gz
 
# Unarchive library, then configure and make for specified architectures
configure_make()
{
   ARCH=$1; SDK=$2; PLATFORM=$3;
   if [ -d "${LIB_NAME}" ]; then
       rm -fr "${LIB_NAME}"
   fi
   tar xfz "${LIB_NAME}.tar.gz"
   pushd .; cd "${LIB_NAME}";

   if [[ "${ARCH}" == "i386" || "${ARCH}" == "x86_64" ]]; then
       echo ""
   else
       sed -ie "s!static volatile sig_atomic_t intr_signal;!static volatile intr_signal;!" "crypto/ui/ui_openssl.c"
   fi

   export CROSS_TOP="${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer"
   export CROSS_SDK="${PLATFORM}${SDK_VERSION}.sdk"
   export TOOLS="${DEVELOPER}"
   export CC="${TOOLS}/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch ${ARCH}"

   PREFIX_DIR="${pwd_path}/../output/ios/openssl-${ARCH}"
   if [ -d "${PREFIX_DIR}" ]; then
       rm -fr "${PREFIX_DIR}"
   fi
   mkdir -p "${PREFIX_DIR}"

   if [[ "${ARCH}" == "x86_64" ]]; then
        unset IPHONEOS_DEPLOYMENT_TARGET
       ./Configure darwin64-x86_64-cc --prefix="${PREFIX_DIR}" "-isysroot ${CROSS_TOP}/SDKs/${CROSS_SDK}"
   elif [[ "${ARCH}" == "i386" ]]; then
        unset IPHONEOS_DEPLOYMENT_TARGET
       ./Configure darwin-i386-cc --prefix="${PREFIX_DIR}" "-isysroot ${CROSS_TOP}/SDKs/${CROSS_SDK}"
   else
        # instruction: 
        # 1.no-shared and -fembed-bitcode is not compatibility
        # 2.advise use only .a static library on iOS
       export IPHONEOS_DEPLOYMENT_TARGET=${IOS_MIN_TARGET}
       ./Configure iphoneos-cross no-shared --prefix="${PREFIX_DIR}" "-fembed-bitcode"
   fi
#    read -n1 -p "Press any key to continue..."
   
   if [ ! -d ${CROSS_TOP}/SDKs/${CROSS_SDK} ]; then
       echo "ERROR: iOS SDK version:'${SDK_VERSION}' incorrect, SDK in your system is:"
       xcodebuild -showsdks | grep iOS
       exit -1
   fi
   
    OUTPUT_ROOT=${TOOLS_ROOT}/../output/ios/openssl-${ARCH}
    mkdir -p ${OUTPUT_ROOT}/log
   make clean &> "${OUTPUT_ROOT}/log/${ARCH}.log"
   if make -j8 >> "${OUTPUT_ROOT}/log/${ARCH}.log" 2>&1
   then
       # make install;
       make install_sw >> "${OUTPUT_ROOT}/log/${ARCH}.log" 2>&1
       make install_ssldirs >> "${OUTPUT_ROOT}/log/${ARCH}.log" 2>&1      
   fi

   popd
}
for ((i=0; i < ${#ARCHS[@]}; i++))
do
    if [[ $# -eq 0 || "$1" == "${ARCHS[i]}" ]]; then
        configure_make "${ARCHS[i]}" "${SDKS[i]}" "${PLATFORMS[i]}"
    fi
done

# Combine libraries for different architectures into one
# Use .a files from the temp directory by providing relative paths
create_lib()
{
   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}";
create_lib "libcrypto.a" "${LIB_DEST_DIR}/libcrypto.a"
create_lib "libssl.a" "${LIB_DEST_DIR}/libssl.a"

build-openssl_111_4android.sh

#!/bin/sh
#
# Copyright 2019 leenjewel
# 
# 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.

# Thanks https://stackoverflow.com/questions/52717228/how-to-compile-openssl-1-1-1-for-android

set -u

OPENSSL_VERSION=1.1.1a

API_LEVEL=16

SOURCE="$0"
while [ -h "$SOURCE" ]; do
    DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
PWDPATH="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

unameOut="$(uname -s)"
case "${unameOut}" in
    Linux*)     MACHINE=Linux;;
    Darwin*)    MACHINE=Mac;;
    CYGWIN*)    MACHINE=Cygwin;;
    MINGW*)     MACHINE=MinGw;;
    *)          MACHINE="UNKNOWN:${unameOut}"
esac

BUILD_DIR=$PWDPATH/../output/openssl_111_android_build
OUT_DIR=$PWDPATH/../output/openssl_111_android

BUILD_TARGETS="armeabi armeabi-v7a arm64-v8a x86 x86_64"

if [ ! -d openssl-${OPENSSL_VERSION} ]
then
    if [ ! -f openssl-${OPENSSL_VERSION}.tar.gz ]
    then
        wget https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz || exit 128
    fi
    tar xzf openssl-${OPENSSL_VERSION}.tar.gz || exit 128
fi

cd openssl-${OPENSSL_VERSION} || exit 128

##### remove output-directory #####
rm -rf $OUT_DIR

##### export ndk directory. Required by openssl-build-scripts #####
export ANDROID_NDK

##### build-function #####
build_the_thing() {
    if [[ "$MACHINE" == "Mac" ]]; then
        TOOLCHAIN=$ANDROID_NDK/toolchains/llvm/prebuilt/darwin-x86_64
    elif [[ "$MACHINE" == "Linux" ]]; then
        TOOLCHAIN=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64
    fi
    export PATH=$TOOLCHAIN/$TRIBLE/bin:$TOOLCHAIN/bin:"$PATH"
echo $PATH
    make clean
    #./Configure $SSL_TARGET $OPTIONS -fuse-ld="$TOOLCHAIN/$TRIBLE/bin/ld" "-gcc-toolchain $TOOLCHAIN" && \
    ./Configure $SSL_TARGET $OPTIONS -fuse-ld="$TOOLCHAIN/$TRIBLE/bin/ld" zlib \
              no-asm \
              no-shared \
              no-unit-test && \
    make && \
    make install DESTDIR=$DESTDIR || exit 128
}

##### set variables according to build-tagret #####
for build_target in $BUILD_TARGETS
do
    case $build_target in
    armeabi)
        TRIBLE="arm-linux-androideabi"
        TC_NAME="arm-linux-androideabi-4.9"
        #OPTIONS="--target=armv5te-linux-androideabi -mthumb -fPIC -latomic -D__ANDROID_API__=$API_LEVEL"
        OPTIONS="--target=armv5te-linux-androideabi -mthumb -fPIC -latomic -D__ANDROID_API__=$API_LEVEL"
        DESTDIR="$BUILD_DIR/armeabi"
        SSL_TARGET="android-arm"
    ;;
    armeabi-v7a)
        TRIBLE="arm-linux-androideabi"
        TC_NAME="arm-linux-androideabi-4.9"
        OPTIONS="--target=armv7a-linux-androideabi -Wl,--fix-cortex-a8 -fPIC -D__ANDROID_API__=$API_LEVEL"
        DESTDIR="$BUILD_DIR/armeabi-v7a"
        SSL_TARGET="android-arm"
    ;;
    x86)
        TRIBLE="i686-linux-android"
        TC_NAME="x86-4.9"
        OPTIONS="-fPIC -D__ANDROID_API__=${API_LEVEL}"
        DESTDIR="$BUILD_DIR/x86"
        SSL_TARGET="android-x86"
    ;;
    x86_64)
        TRIBLE="x86_64-linux-android"
        TC_NAME="x86_64-4.9"
        OPTIONS="-fPIC -D__ANDROID_API__=${API_LEVEL}"
        DESTDIR="$BUILD_DIR/x86_64"
        SSL_TARGET="android-x86_64"
    ;;
    arm64-v8a)
        TRIBLE="aarch64-linux-android"
        TC_NAME="aarch64-linux-android-4.9"
        OPTIONS="-fPIC -D__ANDROID_API__=${API_LEVEL}"
        DESTDIR="$BUILD_DIR/arm64-v8a"
        SSL_TARGET="android-arm64"
    ;;
    esac

    rm -rf $DESTDIR
    build_the_thing
#### copy libraries and includes to output-directory #####
    mkdir -p $OUT_DIR/inc/$build_target
    cp -R $DESTDIR/usr/local/include/* $OUT_DIR/inc/$build_target
    mkdir -p $OUT_DIR/lib/$build_target
    cp -R $DESTDIR/usr/local/lib/*.so $OUT_DIR/lib/$build_target
    cp -R $DESTDIR/usr/local/lib/*.a $OUT_DIR/lib/$build_target
done

echo Success

3、开始编译

进入到编译脚本目录

➜  openssl_for_ios_and_android-master ls
README.md example   output    tools
➜  openssl_for_ios_and_android-master cd tools
➜  tools ls
_shared.sh                    build-curl4ios.sh             build-openssl4ios.sh          openssl-1.1.1g
build-curl4android.sh         build-openssl4android.sh      build-openssl_111_4android.sh openssl-1.1.1g.tar.gz
➜  tools 

运行./build-openssl_111_4android.sh

编译成功后在根目录下的output文件夹内。

如果需要编译动态so库则需要修改编译脚本中no-shared改为shared即可。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值