changelog
[platform/upstream/freerdp.git] / scripts / OpenSSL-DownloadAndBuild.command
1 #!/bin/bash
2
3 # Copyright 2015 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 and simulator - see ARCHS for architectures built
9
10 ## Settings
11 # openssl version to use
12 OPENSSLVERSION="1.0.2q"
13 SHA256SUM="5744cfcbcec2b1b48629f7354203bc1e5e9b5466998bbccc5b5fcde3b18eb684"
14 # SDK version to use - if not set latest version found is used
15 SDK_VERSION=""
16
17 # Minimum SDK version the application supports
18 MIN_SDK_VERSION="10.0"
19
20 ## Defaults
21 INSTALLDIR="external"
22
23 # Architectures to build
24 ARCHS="i386 x86_64 armv7 armv7s arm64"
25
26 # Use default SDK version if not set
27 if [ -z ${SDK_VERSION} ]; then
28     SDK_VERSION=`xcrun -sdk iphoneos --show-sdk-version`
29 fi
30
31 CORES=`sysctl hw.ncpu | awk '{print $2}'`
32 MAKEOPTS="-j $CORES"
33 # disable parallell builds since openssl build
34 # fails sometimes
35 MAKEOPTS=""
36
37 DEVELOPER=`xcode-select -print-path`
38 if [ ! -d "$DEVELOPER" ]; then
39   echo "xcode path is not set correctly $DEVELOPER does not exist (most likely because of xcode > 4.3)"
40   echo "run"
41   echo "sudo xcode-select -switch <xcode path>"
42   echo "for default installation:"
43   echo "sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer"
44   exit 1
45 fi
46
47 function run {
48     "$@"
49     local status=$?
50     if [ $status -ne 0 ]; then
51         echo "error with $@" >&2
52         exit $status
53     fi
54     return $status
55 }
56
57 # Functions
58 function buildArch(){
59     ARCH=$1
60     if [[ "${ARCH}" == "i386" || "${ARCH}" == "x86_64" ]];
61     then
62         PLATFORM="iPhoneSimulator"
63     else
64         run sed -ie "s!static volatile sig_atomic_t intr_signal;!static volatile intr_signal;!" "crypto/ui/ui_openssl.c"
65         PLATFORM="iPhoneOS"
66     fi
67
68     run export CROSS_TOP="${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer"
69     run export CROSS_SDK="${PLATFORM}${SDK_VERSION}.sdk"
70     run export BUILD_TOOLS="${DEVELOPER}"
71     run export CC="${BUILD_TOOLS}/usr/bin/gcc -arch ${ARCH}"
72     if [ ! -z $MIN_SDK_VERSION ]; then
73         run export CC="$CC -miphoneos-version-min=${MIN_SDK_VERSION}"
74     fi
75     echo "Building openssl-${OPENSSLVERSION} for ${PLATFORM} ${SDK_VERSION} ${ARCH} (min SDK set: ${MIN_SDK_VERSION:-"none"})"
76
77     LOGFILE="BuildLog.darwin-${ARCH}.txt"
78     echo -n " Please wait ..."
79     if [[ "$OPENSSLVERSION" =~ 1.0.0. ]]; then
80         run ./Configure BSD-generic32 > "${LOGFILE}" 2>&1
81     elif [ "${ARCH}" == "x86_64" ]; then
82         run ./Configure darwin64-x86_64-cc > "${LOGFILE}" 2>&1
83     elif [ "${ARCH}" == "i386" ]; then
84         run ./Configure iphoneos-cross no-asm > "${LOGFILE}" 2>&1
85     else
86         run ./Configure iphoneos-cross  > "${LOGFILE}" 2>&1
87     fi
88
89     run make ${MAKEOPTS} >> ${LOGFILE} 2>&1
90     echo " Done. Build log saved in ${LOGFILE}"
91     run cp libcrypto.a ../../lib/libcrypto_${ARCH}.a
92     run cp libssl.a ../../lib/libssl_${ARCH}.a
93     run make clean >/dev/null 2>&1
94 }
95
96 # main
97 if [ $# -gt 0 ];then
98     INSTALLDIR=$1
99     if [ ! -d $INSTALLDIR ];then
100         echo "Install directory \"$INSTALLDIR\" does not exist"
101         exit 1
102     fi
103 fi
104
105 cd $INSTALLDIR
106 if [ ! -d openssl ];then
107     run mkdir openssl
108 fi
109 run cd openssl
110 CS=`shasum -a 256 "openssl-$OPENSSLVERSION.tar.gz" | cut -d ' ' -f1`
111 if [ ! "$CS" = "$SHA256SUM" ]; then
112     echo "Downloading OpenSSL Version $OPENSSLVERSION ..."
113     run rm -f "openssl-$OPENSSLVERSION.tar.gz"
114     run curl -o "openssl-$OPENSSLVERSION.tar.gz" https://www.openssl.org/source/openssl-$OPENSSLVERSION.tar.gz
115
116     CS=`shasum -a 256 "openssl-$OPENSSLVERSION.tar.gz" | cut -d ' ' -f1`
117     if [ ! "$CS" = "$SHA256SUM" ]; then
118     echo "Download failed or invalid checksum. Have a nice day."
119     exit 1
120     fi
121 fi
122
123 # remove old build dir
124 run rm -rf openssltmp
125 run mkdir openssltmp
126 run cd openssltmp
127
128 echo "Unpacking OpenSSL ..."
129 run tar xfz "../openssl-$OPENSSLVERSION.tar.gz"
130 if [ ! $? = 0 ]; then
131     echo "Unpacking failed."
132     exit 1
133 fi
134 echo
135
136 run cd "openssl-$OPENSSLVERSION"
137
138 case `pwd` in
139      *\ * )
140            echo "The build path (`pwd`) contains whitepsaces - fix this."
141            exit 1
142           ;;
143 esac
144
145 # Cleanup old build artifacts
146 run rm -rf ../../include
147 run mkdir -p ../../include
148
149 run rm -rf ../../lib
150 run mkdir -p ../../lib
151
152 for i in ${ARCHS}; do
153     buildArch $i
154 done
155
156 echo "Copying header files ..."
157 run cp -r include/ ../../include/
158 echo
159
160 echo "Combining to unversal binary"
161 run lipo -create ../../lib/libcrypto_*.a -o ../../lib/libcrypto.a
162 run lipo -create ../../lib/libssl_*.a -o ../../lib/libssl.a
163
164 echo "Finished. Please verify the contens of the openssl folder in \"$INSTALLDIR\""