build-libssl.sh 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. git clean -Xdf ${CURRENTPATH}
  72. if [ ! -d "$DEVELOPER" ]; then
  73. echo "xcode path is not set correctly $DEVELOPER does not exist"
  74. echo "run"
  75. echo "sudo xcode-select -switch <xcode path>"
  76. echo "for default installation:"
  77. echo "sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer"
  78. exit 1
  79. fi
  80. case $DEVELOPER in
  81. *\ * )
  82. echo "Your Xcode path contains whitespaces, which is not supported."
  83. exit 1
  84. ;;
  85. esac
  86. case $CURRENTPATH in
  87. *\ * )
  88. echo "Your path contains whitespaces, which is not supported by 'make install'."
  89. exit 1
  90. ;;
  91. esac
  92. set -e
  93. if [ ! -e openssl-${VERSION}.tar.gz ]; then
  94. echo "Downloading openssl-${VERSION}.tar.gz"
  95. curl ${CURL_OPTIONS} -O https://www.openssl.org/source/openssl-${VERSION}.tar.gz
  96. else
  97. echo "Using openssl-${VERSION}.tar.gz"
  98. fi
  99. mkdir -p "${CURRENTPATH}/src"
  100. mkdir -p "${CURRENTPATH}/bin"
  101. mkdir -p "${CURRENTPATH}/lib"
  102. for ARCH in ${ARCHS}
  103. do
  104. if [[ "$ARCH" == tv* ]]; then
  105. SDKVERSION=$TVOS_SDKVERSION
  106. MIN_SDK_VERSION=$TVOS_MIN_SDK_VERSION
  107. else
  108. SDKVERSION=$IOS_SDKVERSION
  109. MIN_SDK_VERSION=$IOS_MIN_SDK_VERSION
  110. fi
  111. if [[ "${ARCH}" == "i386" || "${ARCH}" == "x86_64" ]]; then
  112. PLATFORM="iPhoneSimulator"
  113. elif [ "${ARCH}" == "tv_x86_64" ]; then
  114. ARCH="x86_64"
  115. PLATFORM="AppleTVSimulator"
  116. elif [ "${ARCH}" == "tv_arm64" ]; then
  117. ARCH="arm64"
  118. PLATFORM="AppleTVOS"
  119. else
  120. PLATFORM="iPhoneOS"
  121. fi
  122. export $PLATFORM
  123. export CROSS_TOP="${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer"
  124. export CROSS_SDK="${PLATFORM}${SDKVERSION}.sdk"
  125. export BUILD_TOOLS="${DEVELOPER}"
  126. mkdir -p "${CURRENTPATH}/bin/${PLATFORM}${SDKVERSION}-${ARCH}.sdk"
  127. LOG="${CURRENTPATH}/bin/${PLATFORM}${SDKVERSION}-${ARCH}.sdk/build-openssl-${VERSION}.log"
  128. echo "Building openssl-${VERSION} for ${PLATFORM} ${SDKVERSION} ${ARCH}"
  129. echo " Logfile: $LOG"
  130. LOCAL_CONFIG_OPTIONS="${CONFIG_OPTIONS}"
  131. if [ "${ENABLE_EC_NISTP_64_GCC_128}" == "true" ]; then
  132. case "$ARCH" in
  133. *64*)
  134. LOCAL_CONFIG_OPTIONS="${LOCAL_CONFIG_OPTIONS} enable-ec_nistp_64_gcc_128"
  135. ;;
  136. esac
  137. fi
  138. export CC="${BUILD_TOOLS}/usr/bin/gcc -arch ${ARCH}"
  139. echo " Patch source code..."
  140. src_work_dir="${CURRENTPATH}/src/${PLATFORM}-${ARCH}"
  141. mkdir -p "$src_work_dir"
  142. tar zxf "${CURRENTPATH}/openssl-${VERSION}.tar.gz" -C "$src_work_dir"
  143. cd "${src_work_dir}/openssl-${VERSION}"
  144. chmod u+x ./Configure
  145. if [[ "${PLATFORM}" == "AppleTVSimulator" || "${PLATFORM}" == "AppleTVOS" ]]; then
  146. LC_ALL=C sed -i -- 's/define HAVE_FORK 1/define HAVE_FORK 0/' "./apps/speed.c"
  147. LC_ALL=C sed -i -- 's/D\_REENTRANT\:iOS/D\_REENTRANT\:tvOS/' "./Configure"
  148. if [[ "${ARCH}" == "arm64" ]]; then
  149. sed -ie "s!static volatile sig_atomic_t intr_signal;!static volatile intr_signal;!" "crypto/ui/ui_openssl.c"
  150. fi
  151. elif [[ "$PLATFORM" == "iPhoneOS" ]]; then
  152. sed -ie "s!static volatile sig_atomic_t intr_signal;!static volatile intr_signal;!" "crypto/ui/ui_openssl.c"
  153. fi
  154. echo " Configure...\c"
  155. set +e
  156. if [ "$1" == "verbose" ]; then
  157. if [ "${ARCH}" == "x86_64" ]; then
  158. ./Configure no-asm darwin64-x86_64-cc --openssldir="${CURRENTPATH}/bin/${PLATFORM}${SDKVERSION}-${ARCH}.sdk" ${LOCAL_CONFIG_OPTIONS}
  159. else
  160. ./Configure iphoneos-cross --openssldir="${CURRENTPATH}/bin/${PLATFORM}${SDKVERSION}-${ARCH}.sdk" ${LOCAL_CONFIG_OPTIONS}
  161. fi
  162. else
  163. if [ "${ARCH}" == "x86_64" ]; then
  164. (./Configure no-asm darwin64-x86_64-cc --openssldir="${CURRENTPATH}/bin/${PLATFORM}${SDKVERSION}-${ARCH}.sdk" ${LOCAL_CONFIG_OPTIONS} > "${LOG}" 2>&1) & spinner
  165. else
  166. (./Configure iphoneos-cross --openssldir="${CURRENTPATH}/bin/${PLATFORM}${SDKVERSION}-${ARCH}.sdk" ${LOCAL_CONFIG_OPTIONS} > "${LOG}" 2>&1) & spinner
  167. fi
  168. fi
  169. if [ $? != 0 ]; then
  170. echo "Problem while configure - Please check ${LOG}"
  171. exit 1
  172. fi
  173. echo "\n Patch Makefile..."
  174. # add -isysroot to CC=
  175. if [[ "${PLATFORM}" == "AppleTVSimulator" || "${PLATFORM}" == "AppleTVOS" ]]; then
  176. sed -ie "s!^CFLAG=!CFLAG=-isysroot ${CROSS_TOP}/SDKs/${CROSS_SDK} -mtvos-version-min=${TVOS_MIN_SDK_VERSION} !" "Makefile"
  177. else
  178. sed -ie "s!^CFLAG=!CFLAG=-isysroot ${CROSS_TOP}/SDKs/${CROSS_SDK} -miphoneos-version-min=${MIN_SDK_VERSION} !" "Makefile"
  179. fi
  180. echo " Make...\c"
  181. if [ "$1" == "verbose" ]; then
  182. if [[ ! -z $CONFIG_OPTIONS ]]; then
  183. make depend
  184. fi
  185. make
  186. else
  187. if [[ ! -z $CONFIG_OPTIONS ]]; then
  188. make depend >> "${LOG}" 2>&1
  189. fi
  190. (make >> "${LOG}" 2>&1) & spinner
  191. fi
  192. echo "\n"
  193. if [ $? != 0 ]; then
  194. echo "Problem while make - Please check ${LOG}"
  195. exit 1
  196. fi
  197. set -e
  198. if [ "$1" == "verbose" ]; then
  199. make install_sw
  200. make clean
  201. else
  202. make install_sw >> "${LOG}" 2>&1
  203. make clean >> "${LOG}" 2>&1
  204. fi
  205. rm -rf "$src_work_dir"
  206. done
  207. echo "Build library for iOS..."
  208. lipo -create \
  209. ${CURRENTPATH}/bin/iPhoneSimulator${IOS_SDKVERSION}-i386.sdk/lib/libssl.a \
  210. ${CURRENTPATH}/bin/iPhoneSimulator${IOS_SDKVERSION}-x86_64.sdk/lib/libssl.a \
  211. ${CURRENTPATH}/bin/iPhoneOS${IOS_SDKVERSION}-armv7.sdk/lib/libssl.a \
  212. ${CURRENTPATH}/bin/iPhoneOS${IOS_SDKVERSION}-armv7s.sdk/lib/libssl.a \
  213. ${CURRENTPATH}/bin/iPhoneOS${IOS_SDKVERSION}-arm64.sdk/lib/libssl.a \
  214. -output ${CURRENTPATH}/lib/libssl.a
  215. lipo -create \
  216. ${CURRENTPATH}/bin/iPhoneSimulator${IOS_SDKVERSION}-i386.sdk/lib/libcrypto.a \
  217. ${CURRENTPATH}/bin/iPhoneSimulator${IOS_SDKVERSION}-x86_64.sdk/lib/libcrypto.a \
  218. ${CURRENTPATH}/bin/iPhoneOS${IOS_SDKVERSION}-armv7.sdk/lib/libcrypto.a \
  219. ${CURRENTPATH}/bin/iPhoneOS${IOS_SDKVERSION}-armv7s.sdk/lib/libcrypto.a \
  220. ${CURRENTPATH}/bin/iPhoneOS${IOS_SDKVERSION}-arm64.sdk/lib/libcrypto.a \
  221. -output ${CURRENTPATH}/lib/libcrypto.a
  222. echo "Build library for tvOS..."
  223. lipo -create \
  224. ${CURRENTPATH}/bin/AppleTVSimulator${TVOS_SDKVERSION}-x86_64.sdk/lib/libssl.a \
  225. ${CURRENTPATH}/bin/AppleTVOS${TVOS_SDKVERSION}-arm64.sdk/lib/libssl.a \
  226. -output ${CURRENTPATH}/lib/libssl-tvOS.a
  227. lipo -create \
  228. ${CURRENTPATH}/bin/AppleTVSimulator${TVOS_SDKVERSION}-x86_64.sdk/lib/libcrypto.a \
  229. ${CURRENTPATH}/bin/AppleTVOS${TVOS_SDKVERSION}-arm64.sdk/lib/libcrypto.a \
  230. -output ${CURRENTPATH}/lib/libcrypto-tvOS.a
  231. mkdir -p ${CURRENTPATH}/include
  232. cp -R ${CURRENTPATH}/bin/iPhoneSimulator${IOS_SDKVERSION}-x86_64.sdk/include/openssl ${CURRENTPATH}/include/
  233. echo "Done."