eeaaca75b2e3fab5fd826a8a018ea96ce8e5fcd6
[platform/upstream/freerdp.git] / scripts / OpenSSL-DownloadAndBuild.command
1 #!/bin/bash
2
3 # Copyright 2013 Thincast Technologies GmbH
4
5 # This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. 
6 # If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 #
8 # This script will download and build openssl for iOS (armv7, armv7s) and simulator (i386)
9
10 # Settings and definitions
11 USER_OS_SDK=""
12 USER_SIM_SDK=""
13
14 OPENSSLVERSION="1.0.0e"
15 MD5SUM="7040b89c4c58c7a1016c0dfa6e821c86"
16 OPENSSLPATCH="OpenSSL-iFreeRDP.diff"
17 INSTALLDIR="external"
18
19 MAKEOPTS="-j $CORES"
20 # disable parallell builds since openssl build
21 # fails sometimes
22 MAKEOPTS=""
23 CORES=`sysctl hw.ncpu | awk '{print $2}'`
24 SCRIPTDIR=$(dirname `cd ${0%/*} && echo $PWD/${0##*/}`)
25 OS_SDK=""
26 SIM_SDK=""
27 OS_SDK_PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs"
28 SIM_SDK_PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs"
29
30 # Functions
31 function buildArch(){
32         ARCH=$1
33         LOGFILE="BuildLog.darwin-${ARCH}.txt"
34         echo "Building architecture ${ARCH}. Please wait ..."
35         ./Configure darwin-${ARCH}-cc > ${LOGFILE}
36         make ${MAKEOPTS} >> ${LOGFILE} 2>&1
37         echo "Done. Build log saved in ${LOGFILE}"
38         cp libcrypto.a ../../lib/libcrypto_${ARCH}.a
39         cp libssl.a ../../lib/libssl_${ARCH}.a
40         make clean >/dev/null 2>&1
41         echo
42 }
43
44 # main
45 if [ $# -gt 0 ];then
46         INSTALLDIR=$1
47         if [ ! -d $INSTALLDIR ];then
48                 echo "Install directory \"$INSTALLDIR\" does not exist"
49                 exit 1
50         fi
51 fi
52
53 echo "Detecting SDKs..."
54 if [ "x${USER_OS_SDK}" == "x" ];then
55         OS_SDK=`ls -1 ${OS_SDK_PATH} | sort -n | head -1`
56         if [ "x${OS_SDK}" == "x" ];then
57                 echo "No iPhoneOS SDK found"
58                 exit 1;
59         fi
60 else
61         OS_SDK=${USER_OS_SDK}
62         if [ ! -d "${OS_SDK_PATH}/${OS_SDK}" ];then
63                 echo "User specified iPhoneOS SDK not found"
64                 exit 1
65         fi
66 fi
67 echo "Using iPhoneOS SDK: ${OS_SDK}"
68
69 if [ "x${USER_SIM_SDK}" == "x" ];then
70         SIM_SDK=`ls -1 ${SIM_SDK_PATH} | sort -n | head -1`
71         if [ "x${SIM_SDK}" == "x" ];then
72                 echo "No iPhoneSimulator SDK found"
73                 exit 1;
74         fi
75 else
76         SIM_SDK=${USER_SIM_SDK}
77         if [ ! -d "${SIM_SDK_PATH}/${SIM_SDK}" ];then
78                 echo "User specified iPhoneSimulator SDK not found"
79                 exit 1
80         fi
81 fi
82 echo "Using iPhoneSimulator SDK: ${SIM_SDK}"
83 echo
84
85 cd $INSTALLDIR
86 if [ ! -d openssl ];then
87         mkdir openssl
88 fi
89 cd openssl
90 CS=`md5 -q "openssl-$OPENSSLVERSION.tar.gz" 2>/dev/null`
91 if [ ! "$CS" = "$MD5SUM" ]; then
92     echo "Downloading OpenSSL Version $OPENSSLVERSION ..."
93     rm -f "openssl-$OPENSSLVERSION.tar.gz"
94     curl -o "openssl-$OPENSSLVERSION.tar.gz" http://www.openssl.org/source/openssl-$OPENSSLVERSION.tar.gz
95
96     CS=`md5 -q "openssl-$OPENSSLVERSION.tar.gz" 2>/dev/null`
97     if [ ! "$CS" = "$MD5SUM" ]; then
98         echo "Download failed or invalid checksum. Have a nice day."
99         exit 1
100     fi
101 fi
102
103 # remove old build dir
104 rm -rf openssltmp
105 mkdir openssltmp
106 cd openssltmp
107
108 echo "Unpacking OpenSSL ..."
109 tar xfz "../openssl-$OPENSSLVERSION.tar.gz"
110 if [ ! $? = 0 ]; then
111     echo "Unpacking failed."
112     exit 1
113 fi
114 echo
115
116 echo "Applying iFreeRDP patch ..."
117 cd "openssl-$OPENSSLVERSION"
118 cp ${SCRIPTDIR}/${OPENSSLPATCH} .
119 sed -ie "s#__ISIMSDK__#${SIM_SDK}#" ${OPENSSLPATCH}
120 sed -ie "s#__IOSSDK__#${OS_SDK}#" ${OPENSSLPATCH}
121
122 patch -p1 < $OPENSSLPATCH
123
124 if [ ! $? = 0 ]; then
125     echo "Patch failed."
126     exit 1
127 fi
128 echo
129
130 # Cleanup old build artifacts
131 mkdir -p ../../include/openssl
132 rm -f ../../include/openssl/*.h
133
134 mkdir -p ../../lib
135 rm -f ../../lib/*.a
136
137 echo "Copying header hiles ..."
138 cp include/openssl/*.h ../../include/openssl/
139 echo
140
141 buildArch i386
142 buildArch armv7
143 buildArch armv7s
144
145 echo "Combining to unversal binary"
146 lipo -create ../../lib/libcrypto_*.a -o ../../lib/libcrypto.a
147 lipo -create ../../lib/libssl_*.a -o ../../lib/libssl.a
148
149 echo "Finished. Please verify the contens of the openssl folder in \"$INSTALLDIR\""