openssl-build.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/bin/bash
  2. # This script builds the iOS and Mac openSSL libraries
  3. # Download openssl http://www.openssl.org/source/ and place the tarball next to this script
  4. # Credits:
  5. # https://github.com/st3fan/ios-openssl
  6. # https://github.com/x2on/OpenSSL-for-iPhone/blob/master/build-libssl.sh
  7. set -e
  8. usage ()
  9. {
  10. echo "usage: $0 [minimum iOS SDK version (default 8.3)]"
  11. exit 127
  12. }
  13. if [ $1 -e "-h" ]; then
  14. usage
  15. fi
  16. SDK_VERSION_MIN="4.0"
  17. if [ -z $1 ]; then
  18. SDK_VERSION="8.3"
  19. else
  20. SDK_VERSION=$1
  21. fi
  22. OPENSSL_VERSION="openssl-1.0.2a"
  23. DEVELOPER=`xcode-select -print-path`
  24. buildMac()
  25. {
  26. ARCH=$1
  27. echo "Building ${OPENSSL_VERSION} for ${ARCH}"
  28. TARGET="darwin-i386-cc"
  29. if [[ $ARCH == "x86_64" ]]; then
  30. TARGET="darwin64-x86_64-cc"
  31. SDK_VERSION_MIN="6.0"
  32. fi
  33. pushd . > /dev/null
  34. cd "${OPENSSL_VERSION}"
  35. ./Configure ${TARGET} --openssldir="/tmp/${OPENSSL_VERSION}-${ARCH}" &> "/tmp/${OPENSSL_VERSION}-${ARCH}.log"
  36. make >> "/tmp/${OPENSSL_VERSION}-${ARCH}.log" 2>&1
  37. make install >> "/tmp/${OPENSSL_VERSION}-${ARCH}.log" 2>&1
  38. make clean >> "/tmp/${OPENSSL_VERSION}-${ARCH}.log" 2>&1
  39. popd > /dev/null
  40. }
  41. buildIOS()
  42. {
  43. ARCH=$1
  44. pushd . > /dev/null
  45. cd "${OPENSSL_VERSION}"
  46. if [[ "${ARCH}" == "i386" || "${ARCH}" == "x86_64" ]]; then
  47. PLATFORM="iPhoneSimulator"
  48. else
  49. PLATFORM="iPhoneOS"
  50. sed -ie "s!static volatile sig_atomic_t intr_signal;!static volatile intr_signal;!" "crypto/ui/ui_openssl.c"
  51. fi
  52. export $PLATFORM
  53. export CROSS_TOP="${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer"
  54. export CROSS_SDK="${PLATFORM}${SDK_VERSION}.sdk"
  55. export BUILD_TOOLS="${DEVELOPER}"
  56. export CC="${BUILD_TOOLS}/usr/bin/gcc -arch ${ARCH}"
  57. echo "Building ${OPENSSL_VERSION} for ${PLATFORM} ${SDK_VERSION} ${ARCH}"
  58. if [[ "${ARCH}" == "x86_64" ]]; then
  59. ./Configure darwin64-x86_64-cc --openssldir="/tmp/${OPENSSL_VERSION}-iOS-${ARCH}" &> "/tmp/${OPENSSL_VERSION}-iOS-${ARCH}.log"
  60. else
  61. ./Configure iphoneos-cross --openssldir="/tmp/${OPENSSL_VERSION}-iOS-${ARCH}" &> "/tmp/${OPENSSL_VERSION}-iOS-${ARCH}.log"
  62. fi
  63. # add -isysroot to CC=
  64. sed -ie "s!^CFLAG=!CFLAG=-isysroot ${CROSS_TOP}/SDKs/${CROSS_SDK} -miphoneos-version-min=${SDK_VERSION_MIN} !" "Makefile"
  65. make >> "/tmp/${OPENSSL_VERSION}-iOS-${ARCH}.log" 2>&1
  66. make install >> "/tmp/${OPENSSL_VERSION}-iOS-${ARCH}.log" 2>&1
  67. make clean >> "/tmp/${OPENSSL_VERSION}-iOS-${ARCH}.log" 2>&1
  68. popd > /dev/null
  69. }
  70. echo "Cleaning up"
  71. rm -rf include/openssl/* lib/*
  72. mkdir -p lib/iOS
  73. mkdir -p lib/Mac
  74. mkdir -p include/openssl/
  75. rm -rf "/tmp/${OPENSSL_VERSION}-*"
  76. rm -rf "/tmp/${OPENSSL_VERSION}-*.log"
  77. rm -rf "${OPENSSL_VERSION}"
  78. if [ ! -e ${OPENSSL_VERSION}.tar.gz ]; then
  79. echo "Downloading ${OPENSSL_VERSION}.tar.gz"
  80. curl -O https://www.openssl.org/source/${OPENSSL_VERSION}.tar.gz
  81. else
  82. echo "Using ${OPENSSL_VERSION}.tar.gz"
  83. fi
  84. echo "Unpacking openssl"
  85. tar xfz "${OPENSSL_VERSION}.tar.gz"
  86. buildMac "i386"
  87. buildMac "x86_64"
  88. echo "Copying headers"
  89. cp /tmp/${OPENSSL_VERSION}-i386/include/openssl/* include/openssl/
  90. echo "Building Mac libraries"
  91. lipo \
  92. "/tmp/${OPENSSL_VERSION}-i386/lib/libcrypto.a" \
  93. "/tmp/${OPENSSL_VERSION}-x86_64/lib/libcrypto.a" \
  94. -create -output lib/Mac/libcrypto.a
  95. lipo \
  96. "/tmp/${OPENSSL_VERSION}-i386/lib/libssl.a" \
  97. "/tmp/${OPENSSL_VERSION}-x86_64/lib/libssl.a" \
  98. -create -output lib/Mac/libssl.a
  99. buildIOS "armv7s"
  100. buildIOS "armv7"
  101. buildIOS "arm64"
  102. buildIOS "x86_64"
  103. buildIOS "i386"
  104. echo "Building iOS libraries"
  105. lipo \
  106. "/tmp/${OPENSSL_VERSION}-iOS-armv7s/lib/libcrypto.a" \
  107. "/tmp/${OPENSSL_VERSION}-iOS-armv7/lib/libcrypto.a" \
  108. "/tmp/${OPENSSL_VERSION}-iOS-arm64/lib/libcrypto.a" \
  109. "/tmp/${OPENSSL_VERSION}-iOS-i386/lib/libcrypto.a" \
  110. "/tmp/${OPENSSL_VERSION}-iOS-x86_64/lib/libcrypto.a" \
  111. -create -output lib/iOS/libcrypto.a
  112. lipo \
  113. "/tmp/${OPENSSL_VERSION}-iOS-armv7s/lib/libssl.a" \
  114. "/tmp/${OPENSSL_VERSION}-iOS-armv7/lib/libssl.a" \
  115. "/tmp/${OPENSSL_VERSION}-iOS-arm64/lib/libssl.a" \
  116. "/tmp/${OPENSSL_VERSION}-iOS-i386/lib/libssl.a" \
  117. "/tmp/${OPENSSL_VERSION}-iOS-x86_64/lib/libssl.a" \
  118. -create -output lib/iOS/libssl.a
  119. echo "Cleaning up"
  120. rm -rf /tmp/${OPENSSL_VERSION}-*
  121. rm -rf ${OPENSSL_VERSION}
  122. echo "Done"