build-libssl.sh 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #!/bin/sh
  2. # Automatic build script for libssl and libcrypto
  3. # for iPhoneOS and iPhoneSimulator
  4. #
  5. # Created by Felix Schulze on 16.12.10.
  6. # Copyright 2010-2015 Felix Schulze. All rights reserved.
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License");
  9. # you may not use this file except in compliance with the License.
  10. # You may obtain a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS,
  16. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. #
  20. ###########################################################################
  21. # Change values here #
  22. # #
  23. VERSION="1.0.2h" #
  24. IOS_SDKVERSION=`xcrun -sdk iphoneos --show-sdk-version` #
  25. TVOS_SDKVERSION=`xcrun -sdk appletvos --show-sdk-version` #
  26. CURL_OPTIONS="" #
  27. CONFIG_OPTIONS="no-shared no-ssl2 no-ssl3 no-comp no-hw no-md2 no-md4 no-rc2 no-rc5 no-krb5 no-ripemd160 no-idea no-gost no-camellia no-seed no-3des no-heartbeats"
  28. FORCE_BUILD=false
  29. # To set "enable-ec_nistp_64_gcc_128" configuration for x64 archs set next variable to "true"
  30. ENABLE_EC_NISTP_64_GCC_128="" #
  31. # #
  32. ###########################################################################
  33. # #
  34. # Don't change anything under this line! #
  35. # #
  36. ###########################################################################
  37. spinner()
  38. {
  39. local pid=$!
  40. local delay=0.75
  41. local spinstr='|/-\'
  42. while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
  43. local temp=${spinstr#?}
  44. printf " [%c] " "$spinstr"
  45. local spinstr=$temp${spinstr%"$temp"}
  46. sleep $delay
  47. printf "\b\b\b\b\b\b"
  48. done
  49. printf " \b\b\b\b"
  50. }
  51. CURRENTPATH=`pwd`
  52. ARCHS="i386 x86_64 armv7 armv7s arm64 tv_x86_64 tv_arm64"
  53. DEVELOPER=`xcode-select -print-path`
  54. IOS_MIN_SDK_VERSION="6.1"
  55. TVOS_MIN_SDK_VERSION="9.0"
  56. if [ ${FORCE_BUILD} == false ]; then
  57. echo "Checking for existing libraries"
  58. LIBCRYPTO_VERSION=""
  59. LIBSSL_VERSION=""
  60. if [ -e ${CURRENTPATH}/lib/libcrypto.a ]; then
  61. LIBCRYPTO_VERSION=$(strings ${CURRENTPATH}/lib/libcrypto.a | grep "^OpenSSL ${VERSION}")
  62. fi
  63. if [ -e ${CURRENTPATH}/lib/libssl.a ]; then
  64. LIBSSL_VERSION=$(strings ${CURRENTPATH}/lib/libssl.a | grep "^OpenSSL ${VERSION}")
  65. fi
  66. if [[ "${LIBCRYPTO_VERSION}" == *${VERSION}* && "${LIBSSL_VERSION}" == *${VERSION}* ]]; then
  67. echo "libcrypto and libssl are built already built against target version: ${VERSION}"
  68. exit 0
  69. fi
  70. fi
  71. if [ ! -d "$DEVELOPER" ]; then
  72. echo "xcode path is not set correctly $DEVELOPER does not exist"
  73. echo "run"
  74. echo "sudo xcode-select -switch <xcode path>"
  75. echo "for default installation:"
  76. echo "sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer"
  77. exit 1
  78. fi
  79. case $DEVELOPER in
  80. *\ * )
  81. echo "Your Xcode path contains whitespaces, which is not supported."
  82. exit 1
  83. ;;
  84. esac
  85. case $CURRENTPATH in
  86. *\ * )
  87. echo "Your path contains whitespaces, which is not supported by 'make install'."
  88. exit 1
  89. ;;
  90. esac
  91. set -e
  92. if [ ! -e openssl-${VERSION}.tar.gz ]; then
  93. echo "Downloading openssl-${VERSION}.tar.gz"
  94. curl ${CURL_OPTIONS} -O https://www.openssl.org/source/openssl-${VERSION}.tar.gz
  95. else
  96. echo "Using openssl-${VERSION}.tar.gz"
  97. fi
  98. mkdir -p "${CURRENTPATH}/src"
  99. mkdir -p "${CURRENTPATH}/bin"
  100. mkdir -p "${CURRENTPATH}/lib"
  101. for ARCH in ${ARCHS}
  102. do
  103. if [[ "$ARCH" == tv* ]]; then
  104. SDKVERSION=$TVOS_SDKVERSION
  105. MIN_SDK_VERSION=$TVOS_MIN_SDK_VERSION
  106. else
  107. SDKVERSION=$IOS_SDKVERSION
  108. MIN_SDK_VERSION=$IOS_MIN_SDK_VERSION
  109. fi
  110. if [[ "${ARCH}" == "i386" || "${ARCH}" == "x86_64" ]]; then
  111. PLATFORM="iPhoneSimulator"
  112. elif [ "${ARCH}" == "tv_x86_64" ]; then
  113. ARCH="x86_64"
  114. PLATFORM="AppleTVSimulator"
  115. elif [ "${ARCH}" == "tv_arm64" ]; then
  116. ARCH="arm64"
  117. PLATFORM="AppleTVOS"
  118. else
  119. PLATFORM="iPhoneOS"
  120. fi
  121. export $PLATFORM
  122. export CROSS_TOP="${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer"
  123. export CROSS_SDK="${PLATFORM}${SDKVERSION}.sdk"
  124. export BUILD_TOOLS="${DEVELOPER}"
  125. mkdir -p "${CURRENTPATH}/bin/${PLATFORM}${SDKVERSION}-${ARCH}.sdk"
  126. LOG="${CURRENTPATH}/bin/${PLATFORM}${SDKVERSION}-${ARCH}.sdk/build-openssl-${VERSION}.log"
  127. echo "Building openssl-${VERSION} for ${PLATFORM} ${SDKVERSION} ${ARCH}"
  128. echo " Logfile: $LOG"
  129. LOCAL_CONFIG_OPTIONS="${CONFIG_OPTIONS}"
  130. if [ "${ENABLE_EC_NISTP_64_GCC_128}" == "true" ]; then
  131. case "$ARCH" in
  132. *64*)
  133. LOCAL_CONFIG_OPTIONS="${LOCAL_CONFIG_OPTIONS} enable-ec_nistp_64_gcc_128"
  134. ;;
  135. esac
  136. fi
  137. export CC="${BUILD_TOOLS}/usr/bin/gcc -arch ${ARCH}"
  138. echo " Patch source code..."
  139. src_work_dir="${CURRENTPATH}/src/${PLATFORM}-${ARCH}"
  140. mkdir -p "$src_work_dir"
  141. tar zxf "${CURRENTPATH}/openssl-${VERSION}.tar.gz" -C "$src_work_dir"
  142. cd "${src_work_dir}/openssl-${VERSION}"
  143. chmod u+x ./Configure
  144. if [[ "${PLATFORM}" == "AppleTVSimulator" || "${PLATFORM}" == "AppleTVOS" ]]; then
  145. LC_ALL=C sed -i -- 's/define HAVE_FORK 1/define HAVE_FORK 0/' "./apps/speed.c"
  146. LC_ALL=C sed -i -- 's/D\_REENTRANT\:iOS/D\_REENTRANT\:tvOS/' "./Configure"
  147. if [[ "${ARCH}" == "arm64" ]]; then
  148. sed -ie "s!static volatile sig_atomic_t intr_signal;!static volatile intr_signal;!" "crypto/ui/ui_openssl.c"
  149. fi
  150. elif [[ "$PLATFORM" == "iPhoneOS" ]]; then
  151. sed -ie "s!static volatile sig_atomic_t intr_signal;!static volatile intr_signal;!" "crypto/ui/ui_openssl.c"
  152. fi
  153. echo " Configure...\c"
  154. set +e
  155. if [ "$1" == "verbose" ]; then
  156. if [ "${ARCH}" == "x86_64" ]; then
  157. ./Configure no-asm darwin64-x86_64-cc --openssldir="${CURRENTPATH}/bin/${PLATFORM}${SDKVERSION}-${ARCH}.sdk" ${LOCAL_CONFIG_OPTIONS}
  158. else
  159. ./Configure iphoneos-cross --openssldir="${CURRENTPATH}/bin/${PLATFORM}${SDKVERSION}-${ARCH}.sdk" ${LOCAL_CONFIG_OPTIONS}
  160. fi
  161. else
  162. if [ "${ARCH}" == "x86_64" ]; then
  163. (./Configure no-asm darwin64-x86_64-cc --openssldir="${CURRENTPATH}/bin/${PLATFORM}${SDKVERSION}-${ARCH}.sdk" ${LOCAL_CONFIG_OPTIONS} > "${LOG}" 2>&1) & spinner
  164. else
  165. (./Configure iphoneos-cross --openssldir="${CURRENTPATH}/bin/${PLATFORM}${SDKVERSION}-${ARCH}.sdk" ${LOCAL_CONFIG_OPTIONS} > "${LOG}" 2>&1) & spinner
  166. fi
  167. fi
  168. if [ $? != 0 ]; then
  169. echo "Problem while configure - Please check ${LOG}"
  170. exit 1
  171. fi
  172. echo "\n Patch Makefile..."
  173. # add -isysroot to CC=
  174. if [[ "${PLATFORM}" == "AppleTVSimulator" || "${PLATFORM}" == "AppleTVOS" ]]; then
  175. sed -ie "s!^CFLAG=!CFLAG=-isysroot ${CROSS_TOP}/SDKs/${CROSS_SDK} -mtvos-version-min=${TVOS_MIN_SDK_VERSION} !" "Makefile"
  176. else
  177. sed -ie "s!^CFLAG=!CFLAG=-isysroot ${CROSS_TOP}/SDKs/${CROSS_SDK} -miphoneos-version-min=${MIN_SDK_VERSION} !" "Makefile"
  178. fi
  179. echo " Make...\c"
  180. if [ "$1" == "verbose" ]; then
  181. if [[ ! -z $CONFIG_OPTIONS ]]; then
  182. make depend
  183. fi
  184. make
  185. else
  186. if [[ ! -z $CONFIG_OPTIONS ]]; then
  187. make depend >> "${LOG}" 2>&1
  188. fi
  189. (make >> "${LOG}" 2>&1) & spinner
  190. fi
  191. echo "\n"
  192. if [ $? != 0 ]; then
  193. echo "Problem while make - Please check ${LOG}"
  194. exit 1
  195. fi
  196. set -e
  197. if [ "$1" == "verbose" ]; then
  198. make install_sw
  199. make clean
  200. else
  201. make install_sw >> "${LOG}" 2>&1
  202. make clean >> "${LOG}" 2>&1
  203. fi
  204. rm -rf "$src_work_dir"
  205. done
  206. echo "Build library for iOS..."
  207. lipo -create \
  208. ${CURRENTPATH}/bin/iPhoneSimulator${IOS_SDKVERSION}-i386.sdk/lib/libssl.a \
  209. ${CURRENTPATH}/bin/iPhoneSimulator${IOS_SDKVERSION}-x86_64.sdk/lib/libssl.a \
  210. ${CURRENTPATH}/bin/iPhoneOS${IOS_SDKVERSION}-armv7.sdk/lib/libssl.a \
  211. ${CURRENTPATH}/bin/iPhoneOS${IOS_SDKVERSION}-armv7s.sdk/lib/libssl.a \
  212. ${CURRENTPATH}/bin/iPhoneOS${IOS_SDKVERSION}-arm64.sdk/lib/libssl.a \
  213. -output ${CURRENTPATH}/lib/libssl.a
  214. lipo -create \
  215. ${CURRENTPATH}/bin/iPhoneSimulator${IOS_SDKVERSION}-i386.sdk/lib/libcrypto.a \
  216. ${CURRENTPATH}/bin/iPhoneSimulator${IOS_SDKVERSION}-x86_64.sdk/lib/libcrypto.a \
  217. ${CURRENTPATH}/bin/iPhoneOS${IOS_SDKVERSION}-armv7.sdk/lib/libcrypto.a \
  218. ${CURRENTPATH}/bin/iPhoneOS${IOS_SDKVERSION}-armv7s.sdk/lib/libcrypto.a \
  219. ${CURRENTPATH}/bin/iPhoneOS${IOS_SDKVERSION}-arm64.sdk/lib/libcrypto.a \
  220. -output ${CURRENTPATH}/lib/libcrypto.a
  221. echo "Build library for tvOS..."
  222. lipo -create \
  223. ${CURRENTPATH}/bin/AppleTVSimulator${TVOS_SDKVERSION}-x86_64.sdk/lib/libssl.a \
  224. ${CURRENTPATH}/bin/AppleTVOS${TVOS_SDKVERSION}-arm64.sdk/lib/libssl.a \
  225. -output ${CURRENTPATH}/lib/libssl-tvOS.a
  226. lipo -create \
  227. ${CURRENTPATH}/bin/AppleTVSimulator${TVOS_SDKVERSION}-x86_64.sdk/lib/libcrypto.a \
  228. ${CURRENTPATH}/bin/AppleTVOS${TVOS_SDKVERSION}-arm64.sdk/lib/libcrypto.a \
  229. -output ${CURRENTPATH}/lib/libcrypto-tvOS.a
  230. mkdir -p ${CURRENTPATH}/include
  231. cp -R ${CURRENTPATH}/bin/iPhoneSimulator${IOS_SDKVERSION}-x86_64.sdk/include/openssl ${CURRENTPATH}/include/
  232. echo "Done."