build-libssl.sh 10 KB

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