1#!/bin/bash
2
3##
4 # @file contrib/build_sdk.sh
5 # @brief Builds MEGA SDK static library and static examples
6 #
7 # (c) 2013-2014 by Mega Limited, Auckland, New Zealand
8 #
9 # This file is part of the MEGA SDK - Client Access Engine.
10 #
11 # Applications using the MEGA API must present a valid application key
12 # and comply with the the rules set forth in the Terms of Service.
13 #
14 # The MEGA SDK is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 #
18 # @copyright Simplified (2-clause) BSD License.
19 #
20 # You should have received a copy of the license along with this
21 # program.
22##
23
24# Warn about /bin/sh ins't bash.
25if [ -z "$BASH_VERSION" ] ; then
26    echo "WARNING: The shell running this script isn't bash."
27fi
28
29# global vars
30verbose=1
31use_local=0
32use_dynamic=0
33disable_freeimage=0
34disable_ssl=0
35disable_zlib=0
36download_only=0
37only_build_dependencies=0
38enable_megaapi=0
39make_opts=""
40config_opts=""
41no_examples=""
42configure_only=0
43disable_posix_threads=""
44enable_sodium=0
45enable_cares=0
46enable_curl=0
47enable_libuv=0
48enable_libraw=0
49android_build=0
50readline_build=0
51enable_cryptopp=0
52disable_mediainfo=0
53incremental=0
54no_optimisation=0
55extra_openssl_params=""
56cross_compiling=0
57configure_cross_options=""
58openssl_cross_option=""
59status_dir=""
60persistent_path="/opt/persistent"
61
62on_exit_error() {
63    echo "ERROR! Please check log files. Exiting.."
64}
65
66on_exit_ok() {
67    if [ $configure_only -eq 1 ]; then
68        echo "Successfully configured MEGA SDK!"
69    elif [ $download_only -eq 1 ]; then
70        echo "Successfully downloaded MEGA SDK dependencies!"
71    elif [ $only_build_dependencies -eq 1 ]; then
72        echo "Successfully built MEGA SDK dependencies!"
73    else
74        echo "Successfully compiled MEGA SDK!"
75    fi
76}
77
78print_distro_help()
79{
80    # yum: CentOS, Fedora, RedHat
81    type yum >/dev/null 2>&1
82    local exit_code=$?
83    if [ $exit_code -eq 0 ]; then
84        echo "Please execute the following command:  sudo yum install gcc gcc-c++ libtool unzip autoconf make wget glibc-devel-static"
85        return
86    fi
87
88    # apt-get: Debian, Ubuntu
89    type apt-get >/dev/null 2>&1
90    local exit_code=$?
91    if [ $exit_code -eq 0 ]; then
92        echo "Please execute the following command:  sudo apt-get install gcc g++ libtool-bin unzip autoconf m4 make wget"
93        echo " (or 'libtool' on older Debian / Ubuntu distro versions)"
94        return
95    fi
96}
97
98check_apps()
99{
100    if [ -z "${BASH}" ]
101    then
102        echo "Please run this script with the BASH shell"
103        exit 1
104    elif [ ${BASH_VERSINFO} -lt 3 ]
105    then
106        printf "BASH version 3 or greater is required"
107        exit 1
108    fi
109
110    APPS=(bash gcc c++ libtool tar unzip autoconf make autoreconf wget automake m4)
111    for app in ${APPS[@]}; do
112        type ${app} >/dev/null 2>&1 || { echo "${app} is not installed. Please install it first and re-run the script."; print_distro_help; exit 1; }
113        hash ${app} 2>/dev/null || { echo "${app} is not installed. Please install it first and re-run the script."; print_distro_help; exit 1; }
114    done
115}
116
117package_download() {
118    local name=$1
119    local url=$2
120    local file=$local_dir/$3
121    local md5sum=$4
122
123    if [ $use_local -eq 1 ]; then
124        echo "Using local file for $name"
125        return
126    fi
127
128
129    if [ -f $file ]; then
130        rm -f $file || true
131    fi
132
133    # use packages previously downloaded in /tmp/megasdkbuild folder
134    # if not present download from URL specified
135    # if wget fails, try curl
136    mkdir -p /tmp/megasdkbuild/
137
138#    cp /srv/dependencies_manually_downloaded/$3 $file 2>/dev/null || \
139
140    if [ -e /tmp/megasdkbuild/$3 ] ; then
141        echo "Using cached file /tmp/megasdkbuild/$3"
142        cp /tmp/megasdkbuild/$3 $file
143    else
144        echo "Downloading $name to get $3"
145        wget --secure-protocol=TLSv1 --no-check-certificate -c $url -O $file --progress=bar:force -t 2 -T 30 || \
146        curl -k $url > $file || exit 1
147    fi
148
149    echo "Checking MD5SUM for $file"
150    if ! echo $md5sum \*$file | md5sum -c - ; then
151        echo "Downloading $3 again"
152        #rm /tmp/megasdkbuild/$3
153        rm $file #this prevents unexpected "The file is already fully retrieved; nothing to do."
154        wget --no-check-certificate -c $url -O $file --progress=bar:force -t 2 -T 30 || \
155        curl -k $url > $file || exit 1
156
157        echo "Checking (again) MD5SUM for $file"
158        if ! echo $md5sum \*$file | md5sum -c - ; then
159            echo "Aborting execution due to incorrect MD5SUM for $file. Expected: $md5sum. Calculated:"
160            md5sum $file
161            exit 1
162        fi
163    fi
164
165    #copy to tmp download folder for next constructions
166    cp $file /tmp/megasdkbuild/$3
167    echo "Cached package file at /tmp/megasdkbuild/$3"
168}
169
170package_extract() {
171    local name=$1
172    local file=$local_dir/$2
173    local dir=$3
174
175    echo "Extracting $name"
176
177    local filename=$(basename "$file")
178    local extension="${filename##*.}"
179
180    if [ ! -f $file ]; then
181        echo "File $file does not exist!"
182    fi
183
184    if [ -d $dir ]; then
185        rm -fr $dir || exit 1
186    fi
187
188    if [ $extension == "gz" ]; then
189        tar -xzf $file &> $name.extract.log || exit 1
190    elif [ $extension == "zip" ]; then
191        unzip $file -d $dir &> $name.extract.log || exit 1
192    else
193        echo "Unsupported extension!"
194        exit 1
195    fi
196}
197
198exitwithlog() {
199    local logname=$1
200    local exitcode=$2
201    if [ $verbose -eq 1 ]; then
202        cat $logname
203    fi
204    exit $exitcode
205}
206
207package_configure() {
208    local name=$1
209    local dir=$2
210    local install_dir="$3"
211    local params="$4"
212    local extralibs="$5"
213
214    local conf_f0="./Configure"  #OpenSSL has ./config which first guesses a cross-compiling then calls Configure.  To support reliable cross-compiling, we can call Configure directly with $CC $CXX set appropriately already
215    local conf_f1="./config"
216    local conf_f2="./configure"
217    local conf=""
218    local autogen="./autogen.sh"
219
220    echo "Configuring $name"
221
222    local cwd=$(pwd)
223    cd $dir || exit 1
224
225    if [ -f $autogen ]; then
226        $autogen
227    fi
228
229    if [ $cross_compiling -eq 1 ] && [ -f $conf_f0 ]; then
230        conf="$conf_f0 $openssl_cross_option"
231    elif [ $cross_compiling -eq 0 ] && [ -f $conf_f1 ]; then  # ./config is used to figure out which compiler; if we are cross compiling then skip that and use configure directly, $CC $CXX etc specify the tools
232        conf="$conf_f1"
233    elif [ -f $conf_f2 ]; then
234        conf="$conf_f2 $configure_cross_options"
235    else
236        local exit_code=$?
237        echo "Failed to configure $name, exit status: $exit_code"
238        exit 1
239    fi
240
241    echo -n "configuring $name with : $conf $config_opts --prefix=$install_dir $params"
242    [ -z "$extralibs" ] && echo "" || echo " LIBS=$extralibs"
243
244    if [ -z "$extralibs" ]; then
245        $conf $config_opts --prefix=$install_dir $params &> ../$name.conf.log || exitwithlog ../$name.conf.log 1
246    else
247        $conf $config_opts --prefix=$install_dir $params LIBS="$extralibs" &> ../$name.conf.log || exitwithlog ../$name.conf.log 1
248    fi
249
250    if [ $no_optimisation -eq 1 ]; then
251        # this one works for OpenSSL, other files may need to be adjusted differently
252        sed -i -e "s/\(C[XP]*FLAGS\? *=.*\)-O[1-3]/\1-O0/" ./Makefile
253    fi
254
255    cd $cwd
256}
257
258package_build() {
259    local name=$1
260    local dir=$2
261
262    if [ "$#" -eq 3 ]; then
263        local target=$3
264    else
265        local target=""
266    fi
267
268    echo "Building $name"
269
270    local cwd=$(pwd)
271    cd $dir
272
273    echo make $make_opts $target
274    make $make_opts $target &> ../$name.build.log
275
276    local exit_code=$?
277    if [ $exit_code -ne 0 ]; then
278        echo "Failed to build $name, exit status: $exit_code"
279        exitwithlog ../$name.build.log 1
280    fi
281    cd $cwd
282}
283
284package_install() {
285    local name=$1
286    local dir=$2
287    local install_dir=$3
288    local md5=$4
289
290    if [ "$#" -eq 5 ]; then
291        local target=$5
292    else
293        local target=""
294    fi
295
296    echo "Installing $name"
297
298    local cwd=$(pwd)
299    cd $dir
300
301    if [ $android_build -eq 1 ] && [[ $name == "Crypto++"* ]]; then
302        echo make install-lib $target
303        make install-lib $target &> ../$name.install.log || make install $target &> ../$name.install.log
304    else
305        echo make install $target
306        make install $target &> ../$name.install.log
307    fi
308    local exit_code=$?
309    if [ $exit_code -ne 0 ]; then
310        echo "Failed to install $name, exit status: $exit_code"
311        exitwithlog ../$name.install.log 1
312        cd $cwd
313    else
314        cd $cwd
315        echo $md5 > $status_dir/$name.success
316    fi
317
318    # some packages install libraries to "lib64" folder
319    local lib64=$install_dir/lib64
320    local lib=$install_dir/lib
321    if [ -d $lib64 ]; then
322        cp -f $lib64/* $lib/
323    fi
324}
325
326openssl_pkg() {
327    local build_dir=$1
328    local install_dir=$2
329    local name="OpenSSL"
330    local openssl_ver="1.1.1"
331    local openssl_rel="d"
332    local openssl_url="https://www.openssl.org/source/old/${openssl_ver}/openssl-$openssl_${openssl_ver}${openssl_rel}.tar.gz"
333    local openssl_md5="3be209000dbc7e1b95bcdf47980a3baa"
334    local openssl_file="openssl-${openssl_ver}${openssl_rel}.tar.gz"
335    local openssl_dir="openssl-${openssl_ver}${openssl_rel}"
336    if [ $use_dynamic -eq 1 ]; then
337    local openssl_params="--openssldir=$install_dir shared $extra_openssl_params"
338    else
339    local openssl_params="--openssldir=$install_dir no-shared $extra_openssl_params"
340    fi
341    local loc_make_opts=$make_opts
342
343    if [ $incremental -eq 1 ] && [ -e $status_dir/$name.success ] && [ `cat $status_dir/$name.success` = $openssl_md5 ]; then
344        echo "$name already built"
345        return
346    else
347        rm -f $status_dir/$name.success
348    fi
349
350    package_download $name $openssl_url $openssl_file $openssl_md5
351    if [ $download_only -eq 1 ]; then
352        return
353    fi
354
355    package_extract $name $openssl_file $openssl_dir
356
357    if [ $android_build -eq 1 ]; then
358        echo "Configuring $name"
359        local cwd=$(pwd)
360        cd $openssl_dir
361        perl -pi -e 's/install: all install_docs install_sw/install: install_docs install_sw/g' Makefile.org
362        ./config shared no-ssl2 no-ssl3 no-comp no-hw no-engine --prefix=$install_dir
363        make depend || exit 1
364        cd $cwd
365    else
366        # handle MacOS
367        if [ "$(uname)" == "Darwin" ]; then
368            # OpenSSL compiles 32bit binaries, we need to explicitly tell to use x86_64 mode
369            if [ "$(uname -m)" == "x86_64" ]; then
370                echo "Configuring $name"
371                local cwd=$(pwd)
372                cd $openssl_dir
373                ./Configure darwin64-x86_64-cc --prefix=$install_dir $openssl_params &> ../$name.conf.log || exit 1
374                cd $cwd
375            else
376                package_configure $name $openssl_dir $install_dir "$openssl_params"
377            fi
378        else
379            package_configure $name $openssl_dir $install_dir "$openssl_params" || exit 1
380        fi
381    fi
382
383    # OpenSSL has issues with parallel builds, let's use the default options
384    make_opts=""
385    package_build $name $openssl_dir
386    make_opts=$loc_make_opts
387
388    package_install $name $openssl_dir $install_dir $openssl_md5
389}
390
391cryptopp_pkg() {
392    local build_dir=$1
393    local install_dir=$2
394    local name="Crypto++"
395    local cryptopp_ver="820"
396    local cryptopp_url="http://www.cryptopp.com/cryptopp$cryptopp_ver.zip"
397    local cryptopp_md5="8a8bcb436af83e16d2227bd4ac642243"
398    local cryptopp_file="cryptopp$cryptopp_ver.zip"
399    local cryptopp_dir="cryptopp$cryptopp_ver"
400
401    if [ $incremental -eq 1 ] && [ -e $status_dir/$name.success ] && [ `cat $status_dir/$name.success` = $cryptopp_md5 ]; then
402        echo "$name already built"
403        return
404    else
405        rm -f $status_dir/$name.success
406    fi
407
408    package_download $name $cryptopp_url $cryptopp_file $cryptopp_md5
409    if [ $download_only -eq 1 ]; then
410        return
411    fi
412
413    package_extract $name $cryptopp_file $cryptopp_dir
414
415    #modify Makefile so that it does not use specific cpu architecture optimizations
416    sed "s#CXXFLAGS += -march=native#CXXFLAGS += #g" -i $cryptopp_dir/GNUmakefile
417    sed -i -e "160,165d" $cryptopp_dir/GNUmakefile
418
419    if [ $android_build -eq 1 ]; then
420        cp ${ANDROID_NDK_ROOT}/sources/android/cpufeatures/cpu-features.h $cryptopp_dir/
421        cp ${ANDROID_NDK_ROOT}/sources/android/cpufeatures/cpu-features.c $cryptopp_dir/
422        package_build $name $cryptopp_dir "static -f GNUmakefile-cross"
423        package_install $name $cryptopp_dir $install_dir $cryptopp_md5 "-f GNUmakefile-cross"
424    else
425        package_build $name $cryptopp_dir static
426        package_install $name $cryptopp_dir $install_dir $cryptopp_md5
427   fi
428}
429
430sodium_pkg() {
431    local build_dir=$1
432    local install_dir=$2
433    local name="Sodium"
434    local sodium_ver="1.0.18"
435    local sodium_url="https://github.com/jedisct1/libsodium/archive/$sodium_ver.tar.gz"
436    local sodium_md5="94a783f33ff8a97a09708bc61370d280"
437    local sodium_file="$sodium_ver.tar.gz"
438    local sodium_dir="libsodium-$sodium_ver"
439    if [ $use_dynamic -eq 1 ]; then
440        local sodium_params="--enable-shared"
441    else
442        local sodium_params="--disable-shared --enable-static --disable-pie"
443    fi
444
445    if [ $incremental -eq 1 ] && [ -e $status_dir/$name.success ] && [ `cat $status_dir/$name.success` = $sodium_md5 ]; then
446        echo "$name already built"
447        return
448    else
449        rm -f $status_dir/$name.success
450    fi
451
452    package_download $name $sodium_url $sodium_file $sodium_md5
453    if [ $download_only -eq 1 ]; then
454        return
455    fi
456
457    package_extract $name $sodium_file $sodium_dir
458    package_configure $name $sodium_dir $install_dir "$sodium_params"
459    package_build $name $sodium_dir
460    package_install $name $sodium_dir $install_dir $sodium_md5
461}
462
463libuv_pkg() {
464    local build_dir=$1
465    local install_dir=$2
466    local name="libuv"
467    local libuv_ver="1.34.2"
468    local libuv_url="https://github.com/libuv/libuv/archive/v${libuv_ver}.tar.gz"
469    local libuv_md5="5b57d93320a4aac3e66de94a1d4da98d"
470    local libuv_file="v${libuv_ver}.tar.gz"
471    local libuv_dir="libuv-$libuv_ver"
472    if [ $use_dynamic -eq 1 ]; then
473        local libuv_params="--enable-shared"
474    else
475        local libuv_params="--disable-shared --enable-static"
476    fi
477
478    if [ $incremental -eq 1 ] && [ -e $status_dir/$name.success ] && [ `cat $status_dir/$name.success` = $libuv_md5 ]; then
479        echo "$name already built"
480        return
481    else
482        rm -f $status_dir/$name.success
483    fi
484
485    package_download $name $libuv_url $libuv_file $libuv_md5
486    if [ $download_only -eq 1 ]; then
487        return
488    fi
489
490    package_extract $name $libuv_file $libuv_dir
491
492    local OLD_CFLAGS="$CFLAGS"
493
494    # linking with static library requires -fPIC
495    if [ $use_dynamic -eq 0 ]; then
496        export CFLAGS="$CFLAGS -fPIC"
497    fi
498    package_configure $name $libuv_dir $install_dir "$libuv_params"
499
500    export CFLAGS="$OLD_CFLAGS"
501
502    package_build $name $libuv_dir
503    package_install $name $libuv_dir $install_dir $libuv_md5
504}
505
506libraw_pkg() {
507    local build_dir=$1
508    local install_dir=$2
509    local name="libraw"
510    local libraw_ver="0.19.5"
511    local libraw_url="https://www.libraw.org/data/LibRaw-$libraw_ver.tar.gz"
512    local libraw_md5="865ab9a40910709ff86988e8c0a7d146"
513    local libraw_file="libraw-$libraw_ver.tar.gz"
514    local libraw_dir="LibRaw-$libraw_ver"
515    if [ $use_dynamic -eq 1 ]; then
516        local libraw_params="--enable-shared"
517    else
518        local libraw_params="--disable-shared --enable-static"
519    fi
520
521    if [ $incremental -eq 1 ] && [ -e $status_dir/$name.success ] && [ `cat $status_dir/$name.success` = $libraw_md5 ]; then
522        echo "$name already built"
523        return
524    else
525        rm -f $status_dir/$name.success
526    fi
527
528    package_download $name $libraw_url $libraw_file $libraw_md5
529    if [ $download_only -eq 1 ]; then
530        return
531    fi
532
533    package_extract $name $libraw_file $libraw_dir
534
535    local OLD_LIBS="$LIBS"
536
537    # linking with static library requires -lstdc++
538    if [ $use_dynamic -eq 0 ]; then
539        export LIBS="$LIBS -lstdc++"
540    fi
541    package_configure $name $libraw_dir $install_dir "$libraw_params"
542
543    export LIBS="$OLD_LIBS"
544
545    package_build $name $libraw_dir
546    package_install $name $libraw_dir $install_dir $libraw_md5
547}
548
549zlib_pkg() {
550    local build_dir=$1
551    local install_dir=$2
552    local name="Zlib"
553    local zlib_ver="1.2.11"
554    local zlib_url="http://zlib.net/zlib-$zlib_ver.tar.gz"
555    local zlib_md5="1c9f62f0778697a09d36121ead88e08e"
556    local zlib_file="zlib-$zlib_ver.tar.gz"
557    local zlib_dir="zlib-$zlib_ver"
558    local loc_conf_opts=$config_opts
559    local loc_configure_cross_options=$configure_cross_options
560
561    if [ $use_dynamic -eq 1 ]; then
562        local zlib_params=""
563    else
564        local zlib_params="--static"
565    fi
566
567    if [ $incremental -eq 1 ] && [ -e $status_dir/$name.success ] && [ `cat $status_dir/$name.success` = $zlib_md5 ]; then
568        echo "$name already built"
569        return
570    else
571        rm -f $status_dir/$name.success
572    fi
573
574    package_download $name $zlib_url $zlib_file $zlib_md5
575    if [ $download_only -eq 1 ]; then
576        return
577    fi
578
579    package_extract $name $zlib_file $zlib_dir
580
581    # doesn't recognize --host=xxx
582    config_opts=""
583    configure_cross_options=""
584
585    # Windows must use Makefile.gcc
586    if [ "$(expr substr $(uname -s) 1 10)" != "MINGW32_NT" ]; then
587        package_configure $name $zlib_dir $install_dir "$zlib_params"
588        package_build $name $zlib_dir
589        package_install $name $zlib_dir $install_dir $zlib_md5
590    else
591        export BINARY_PATH=$install_dir/bin
592        export INCLUDE_PATH=$install_dir/include
593        export LIBRARY_PATH=$install_dir/lib
594        package_build $name $zlib_dir "-f win32/Makefile.gcc"
595        package_install $name $zlib_dir $install_dir $zlib_md5 "-f win32/Makefile.gcc"
596        unset BINARY_PATH
597        unset INCLUDE_PATH
598        unset LIBRARY_PATH
599    fi
600    config_opts=$loc_conf_opts
601    configure_cross_options=$loc_configure_cross_options
602
603}
604
605sqlite_pkg() {
606    local build_dir=$1
607    local install_dir=$2
608    local name="SQLite"
609    local sqlite_ver="3300100"
610    local sqlite_url="http://www.sqlite.org/2019/sqlite-autoconf-$sqlite_ver.tar.gz"
611    local sqlite_md5="51252dc6bc9094ba11ab151ba650ff3c"
612    local sqlite_file="sqlite-$sqlite_ver.tar.gz"
613    local sqlite_dir="sqlite-autoconf-$sqlite_ver"
614    if [ $use_dynamic -eq 1 ]; then
615        local sqlite_params="--enable-shared"
616    else
617        local sqlite_params="--disable-shared --enable-static"
618    fi
619
620    if [ $incremental -eq 1 ] && [ -e $status_dir/$name.success ] && [ `cat $status_dir/$name.success` = $sqlite_md5 ]; then
621        echo "$name already built"
622        return
623    else
624        rm -f $status_dir/$name.success
625    fi
626
627    package_download $name $sqlite_url $sqlite_file $sqlite_md5
628    if [ $download_only -eq 1 ]; then
629        return
630    fi
631
632    package_extract $name $sqlite_file $sqlite_dir
633    package_configure $name $sqlite_dir $install_dir "$sqlite_params"
634    package_build $name $sqlite_dir
635    package_install $name $sqlite_dir $install_dir $sqlite_md5
636}
637
638cares_pkg() {
639    local build_dir=$1
640    local install_dir=$2
641    local name="c-ares"
642    local cares_ver="1.14.0"
643    local cares_url="http://c-ares.haxx.se/download/c-ares-$cares_ver.tar.gz"
644    local cares_md5="e57b37a7c46283e83c21cde234df10c7"
645    local cares_file="cares-$cares_ver.tar.gz"
646    local cares_dir="c-ares-$cares_ver"
647    if [ $use_dynamic -eq 1 ]; then
648        local cares_params="--enable-shared"
649    else
650        local cares_params="--disable-shared --enable-static"
651    fi
652
653    if [ $incremental -eq 1 ] && [ -e $status_dir/$name.success ] && [ `cat $status_dir/$name.success` = $cares_md5 ]; then
654        echo "$name already built"
655        return
656    else
657        rm -f $status_dir/$name.success
658    fi
659
660    package_download $name $cares_url $cares_file $cares_md5
661    if [ $download_only -eq 1 ]; then
662        return
663    fi
664
665    package_extract $name $cares_file $cares_dir
666    package_configure $name $cares_dir $install_dir "$cares_params"
667    package_build $name $cares_dir
668    package_install $name $cares_dir $install_dir $cares_md5
669}
670
671curl_pkg() {
672    local build_dir=$1
673    local install_dir=$2
674    local name="cURL"
675    local curl_ver="7.68.0"
676    local curl_url="http://curl.haxx.se/download/curl-$curl_ver.tar.gz"
677    local curl_md5="f68d6f716ff06d357f476ea4ea57a3d6"
678    local curl_file="curl-$curl_ver.tar.gz"
679    local curl_dir="curl-$curl_ver"
680    local openssl_flags=""
681
682    # use local or system OpenSSL
683    if [ $disable_ssl -eq 0 ]; then
684        openssl_flags="--with-ssl=$install_dir"
685    else
686        openssl_flags="--with-ssl"
687    fi
688
689    if [ $use_dynamic -eq 1 ]; then
690        local curl_params="--disable-ftp --disable-file --disable-ldap --disable-ldaps --disable-rtsp --disable-dict \
691            --disable-telnet --disable-tftp --disable-pop3 --disable-imap --disable-smtp --disable-gopher --disable-sspi \
692            --without-librtmp --without-libidn --without-libidn2 --without-libssh2 --enable-ipv6 --disable-manual --without-nghttp2 --without-libpsl \
693            --without-brotli --with-zlib=$install_dir --enable-ares=$install_dir $openssl_flags"
694    else
695        local curl_params="--disable-ftp --disable-file --disable-ldap --disable-ldaps --disable-rtsp --disable-dict \
696            --disable-telnet --disable-tftp --disable-pop3 --disable-imap --disable-smtp --disable-gopher --disable-sspi \
697            --without-librtmp --without-libidn --without-libidn2 --without-libssh2 --enable-ipv6 --disable-manual --without-nghttp2 --without-libpsl \
698            --without-brotli --disable-shared --with-zlib=$install_dir --enable-ares=$install_dir $openssl_flags"
699    fi
700
701    if [ $incremental -eq 1 ] && [ -e $status_dir/$name.success ] && [ `cat $status_dir/$name.success` = $curl_md5 ]; then
702        echo "$name already built"
703        return
704    else
705        rm -f $status_dir/$name.success
706    fi
707
708    package_download $name $curl_url $curl_file $curl_md5
709    if [ $download_only -eq 1 ]; then
710        return
711    fi
712
713    package_extract $name $curl_file $curl_dir
714    package_configure $name $curl_dir $install_dir "$curl_params" "-ldl"
715    package_build $name $curl_dir
716    package_install $name $curl_dir $install_dir $curl_md5
717}
718
719readline_pkg() {
720    local build_dir=$1
721    local install_dir=$2
722    local name="Readline"
723    local readline_ver="7.0"
724    local readline_url="https://ftp.gnu.org/gnu/readline/readline-$readline_ver.tar.gz"
725    local readline_md5="205b03a87fc83dab653b628c59b9fc91"
726    local readline_file="readline-$readline_ver.tar.gz"
727    local readline_dir="readline-$readline_ver"
728    if [ $use_dynamic -eq 1 ]; then
729        local readline_params="--enable-shared"
730    else
731        local readline_params="--disable-shared --enable-static"
732    fi
733
734    if [ $incremental -eq 1 ] && [ -e $status_dir/$name.success ] && [ `cat $status_dir/$name.success` = $readline_md5 ]; then
735        echo "$name already built"
736        return
737    else
738        rm -f $status_dir/$name.success
739    fi
740
741    package_download $name $readline_url $readline_file $readline_md5
742    if [ $download_only -eq 1 ]; then
743        return
744    fi
745
746    package_extract $name $readline_file $readline_dir
747    package_configure $name $readline_dir $install_dir "$readline_params"
748    package_build $name $readline_dir
749    package_install $name $readline_dir $install_dir $readline_md5
750}
751
752termcap_pkg() {
753    local build_dir=$1
754    local install_dir=$2
755    local name="Termcap"
756    local termcap_ver="1.3.1"
757    local termcap_url="http://ftp.gnu.org/gnu/termcap/termcap-$termcap_ver.tar.gz"
758    local termcap_md5="ffe6f86e63a3a29fa53ac645faaabdfa"
759    local termcap_file="termcap-$termcap_ver.tar.gz"
760    local termcap_dir="termcap-$termcap_ver"
761    if [ $use_dynamic -eq 1 ]; then
762        local termcap_params="--enable-shared"
763    else
764        local termcap_params="--disable-shared --enable-static"
765    fi
766
767    if [ $incremental -eq 1 ] && [ -e $status_dir/$name.success ] && [ `cat $status_dir/$name.success` = $termcap_md5 ]; then
768        echo "$name already built"
769        return
770    else
771        rm -f $status_dir/$name.success
772    fi
773
774    package_download $name $termcap_url $termcap_file $termcap_md5
775    if [ $download_only -eq 1 ]; then
776        return
777    fi
778
779    package_extract $name $termcap_file $termcap_dir
780    package_configure $name $termcap_dir $install_dir "$termcap_params"
781    package_build $name $termcap_dir
782    package_install $name $termcap_dir $install_dir $termcap_md5
783}
784
785freeimage_pkg() {
786    local build_dir=$1
787    local install_dir=$2
788    local cwd=$3
789    local name="FreeImage"
790    local freeimage_ver="3180"
791    local freeimage_url="http://downloads.sourceforge.net/freeimage/FreeImage$freeimage_ver.zip"
792    local freeimage_md5="f8ba138a3be233a3eed9c456e42e2578"
793    local freeimage_file="freeimage-$freeimage_ver.zip"
794    local freeimage_dir_extract="freeimage-$freeimage_ver"
795    local freeimage_dir="freeimage-$freeimage_ver/FreeImage"
796
797    if [ $incremental -eq 1 ] && [ -e $status_dir/$name.success ] && [ `cat $status_dir/$name.success` = $freeimage_md5 ]; then
798        echo "$name already built"
799        return
800    else
801        rm -f $status_dir/$name.success
802    fi
803
804    package_download $name $freeimage_url $freeimage_file $freeimage_md5
805    if [ $download_only -eq 1 ]; then
806        return
807    fi
808
809    package_extract $name $freeimage_file $freeimage_dir_extract
810
811    #Fix issue with powf64 redefined in mathcalls.h in glibc 2.27
812    find $freeimage_dir_extract/FreeImage/ -type f -print0 | xargs -0 sed -i "s#powf64#powf64freeimage#g"
813
814    #patch to fix problem with raw strings
815    find $freeimage_dir_extract/FreeImage/Source/LibWebP -type f -exec sed -i -e 's/"#\([A-X]\)"/" #\1 "/g' {} \;
816
817    #patch to fix problem with newest compilers
818    sed -i "s#CXXFLAGS += -D__ANSI__#CXXFLAGS += -D__ANSI__ -std=c++98#g" $freeimage_dir_extract/FreeImage/Makefile.gnu
819
820    #freeimage uses some macros with a dollarsign in the name, and also has some constants that don't fit in a long
821    #as gcc building for 32 bit linux has long as 32 bit.  Also some files have the utf-8 BOM which old gcc doesn't like
822    export CFLAGS="$CFLAGS -fdollars-in-identifiers"
823    export CXXFLAGS="$CXXFLAGS -fdollars-in-identifiers"
824
825    find $freeimage_dir/Source/OpenEXR/IlmImf/ -name "*.cpp" | xargs sed -i -e "s/0xffffffffffffffffL/0xffffffffffffffffull/"
826
827    if command -v dos2unix; then
828        find $freeimage_dir/Source/LibRawLite/internal/ -name "*.cpp" | xargs dos2unix
829        find $freeimage_dir/Source/LibRawLite/internal/ -name "*.h" | xargs dos2unix
830    else
831        echo "Command dos2unix not found, skipping some fixes for FreeImage"
832    fi
833
834    # replace Makefile on MacOS
835    if [ "$(uname)" == "Darwin" ]; then
836        cp $cwd/contrib/FreeImage.Makefile.osx $freeimage_dir/Makefile.osx
837    fi
838
839    if [ $android_build -eq 1 ]; then
840        sed -i '/#define HAVE_SEARCH_H 1/d' $freeimage_dir/Source/LibTIFF4/tif_config.h
841    fi
842
843    if [ $use_dynamic -eq 0 ]; then
844        export FREEIMAGE_LIBRARY_TYPE=STATIC
845    fi
846
847    # freeimage's LibPNG has a problem with deciding to use neon on 64 bit arm, resulting in a missing symbol
848    if [ "$ARCH" == "aarch64" ]; then
849        export CFLAGS="$CFLAGS -DPNG_ARM_NEON_OPT=0"
850    fi
851
852    if [ "$(expr substr $(uname -s) 1 10)" != "MINGW32_NT" ]; then
853        package_build $name $freeimage_dir
854        # manually copy header and library
855        cp $freeimage_dir/Dist/FreeImage.h $install_dir/include || exit 1
856        cp $freeimage_dir/Dist/libfreeimage* $install_dir/lib || exit 1
857        echo $freeimage_md5 > $status_dir/$name.success
858    # MinGW
859    else
860        package_build $name $freeimage_dir "-f Makefile.mingw"
861        # manually copy header and library
862        cp $freeimage_dir/Dist/FreeImage.h $install_dir/include || exit 1
863        # ignore if not present
864        cp $freeimage_dir/Dist/FreeImage.dll $install_dir/lib || 1
865        cp $freeimage_dir/Dist/FreeImage.lib $install_dir/lib || 1
866        cp $freeimage_dir/Dist/libFreeImage.a $install_dir/lib || 1
867        echo $freeimage_md5 > $status_dir/$name.success
868    fi
869}
870
871mediainfo_pkg() {
872    local build_dir=$1
873    local install_dir=$2
874    local cwd=$3
875    local zenlib_name="ZenLib"
876    local zenlib_ver="6694a744d82d942c4a410f25f916561270381889"
877    local zenlib_url="https://github.com/MediaArea/ZenLib/archive/${zenlib_ver}.tar.gz"
878    local zenlib_md5="02a78d1d18ce163483d8e01961f983f4"
879    local zenlib_file="$zenlib_ver.tar.gz"
880    local zenlib_dir_extract="ZenLib-$zenlib_ver"
881    local zenlib_dir="ZenLib-$zenlib_ver/Project/GNU/Library"
882
883    local mediainfolib_name="MediaInfoLib"
884    local mediainfolib_ver="4ee7f77c087b29055f48d539cd679de8de6f9c48"
885    local mediainfolib_url="https://github.com/meganz/MediaInfoLib/archive/${mediainfolib_ver}.tar.gz"
886    local mediainfolib_md5="5214341153298077b9e711c181742fe3"
887    local mediainfolib_file="$mediainfolib_ver.tar.gz"
888    local mediainfolib_dir_extract="MediaInfoLib-$mediainfolib_ver"
889    local mediainfolib_dir="MediaInfoLib-$mediainfolib_ver/Project/GNU/Library"
890
891    if [ $incremental -eq 1 ] && [ -e $status_dir/$mediainfolib_name.success ] && [ `cat $status_dir/$mediainfolib_name.success` = $mediainfolib_md5 ]; then
892        echo "$mediainfolib_name already built"
893        return
894    else
895        rm -f $status_dir/$mediainfolib_name.success
896    fi
897
898    package_download $zenlib_name $zenlib_url $zenlib_file $zenlib_md5
899    package_download $mediainfolib_name $mediainfolib_url $mediainfolib_file $mediainfolib_md5
900    if [ $download_only -eq 1 ]; then
901        return
902    fi
903
904    package_extract $zenlib_name $zenlib_file $zenlib_dir_extract
905    ln -sfr $zenlib_dir_extract $build_dir/ZenLib || ln -sf $zenlib_dir_extract $build_dir/ZenLib
906    package_extract $mediainfolib_name $mediainfolib_file $mediainfolib_dir_extract
907
908    local zenlib_params="--enable-static --disable-shared"
909
910    local mediainfolib_params="--disable-shared --enable-minimize-size --enable-minimal --disable-archive \
911    --disable-image --disable-tag --disable-text --disable-swf --disable-flv --disable-hdsf4m --disable-cdxa \
912    --disable-dpg --disable-pmp --disable-rm --disable-wtv --disable-mxf --disable-dcp --disable-aaf --disable-bdav \
913    --disable-bdmv --disable-dvdv --disable-gxf --disable-mixml --disable-skm --disable-nut --disable-tsp \
914    --disable-hls --disable-dxw --disable-dvdif --disable-dashmpd --disable-aic --disable-avsv --disable-canopus \
915    --disable-ffv1 --disable-flic --disable-huffyuv --disable-prores --disable-y4m --disable-adpcm --disable-amr \
916    --disable-amv --disable-ape --disable-au --disable-la --disable-celt --disable-midi --disable-mpc --disable-openmg \
917    --disable-pcm --disable-ps2a --disable-rkau --disable-speex --disable-tak --disable-tta --disable-twinvq \
918    --disable-references --enable-staticlibs"
919
920    if [ $disable_zlib -eq 0 ]; then
921        mediainfolib_params="$mediainfolib_params --with-libz-static"
922        mkdir -p $build_dir/Shared/Source/zlib
923        #~ ln -sfr $(find $install_dir -name zlib.a) $build_dir/Shared/Source/zlib/libz.a
924        ln -sfr $install_dir/lib/libz.a $build_dir/Shared/Source/zlib/libz.a || ln -sf $install_dir/lib/libz.a $build_dir/Shared/Source/zlib/libz.a
925        ln -sfr $install_dir/include/zlib.h $build_dir/Shared/Source/zlib/zlib.h || ln -sf $install_dir/include/zlib.h $build_dir/Shared/Source/zlib/zlib.h
926        ln -sfr $install_dir/include/zconf.h $build_dir/Shared/Source/zlib/zconf.h || ln -sf $install_dir/include/zconf.h $build_dir/Shared/Source/zlib/zconf.h
927    fi
928
929    package_configure $zenlib_name $zenlib_dir $install_dir "$zenlib_params" #TODO: tal vez install dir ha de ser ./ZenLib!!! casi 100% seguro
930    #~ package_configure $zenlib_name $zenlib_dir "$build_dir/ZenLib" "$zenlib_params" #TODO: tal vez install dir ha de ser ./ZenLib!!! casi 100% seguro
931
932    package_build $zenlib_name $zenlib_dir
933    #package_install $zenlib_name $zenlib_dir $install_dir
934    package_install $zenlib_name $zenlib_dir "$build_dir/ZenLib" $zenlib_md5
935
936    package_configure $mediainfolib_name $mediainfolib_dir $install_dir "$mediainfolib_params" #TODO: tal vez install dir ha de ser ./ZenLib!!! casi 100% seguro
937    CR=$(printf '\r')
938    cat << EOF > $build_dir/mediainfopatch
939--- MediaInfo_Config.cpp
940+++ MediaInfo_Config_new.cpp
941@@ -1083,7 +1083,7 @@
942     }$CR
943     if (Option_Lower==__T("maxml_fields"))$CR
944     {$CR
945-        #if MEDIAINFO_ADVANCED$CR
946+        #if MEDIAINFO_ADVANCED && defined(MEDIAINFO_XML_YES)$CR
947             return MAXML_Fields_Get(Value);$CR
948         #else // MEDIAINFO_ADVANCED$CR
949             return __T("advanced features are disabled due to compilation options");$CR
950@@ -2652,6 +2652,7 @@
951 #endif // MEDIAINFO_ADVANCED$CR
952 $CR
953 #if MEDIAINFO_ADVANCED$CR
954+#ifdef MEDIAINFO_XML_YES$CR
955 extern Ztring Xml_Name_Escape_0_7_78 (const Ztring &Name);$CR
956 Ztring MediaInfo_Config::MAXML_Fields_Get (const Ztring &StreamKind_String)$CR
957 {$CR
958@@ -2691,6 +2692,7 @@
959     List.Separator_Set(0, __T(","));$CR
960     return List.Read();$CR
961 }$CR
962+#endif$CR
963 #endif // MEDIAINFO_ADVANCED$CR
964 $CR
965 //***************************************************************************$CR
966EOF
967    (cd $mediainfolib_dir/../../../Source/MediaInfo; patch MediaInfo_Config.cpp < $build_dir/mediainfopatch)
968
969    package_build $mediainfolib_name $mediainfolib_dir
970
971    pushd $build_dir/ZenLib >/dev/null
972    package_install $mediainfolib_name $build_dir/$mediainfolib_dir $install_dir $mediainfolib_md5
973    popd
974
975}
976
977# we can't build vanilla ReadLine under MinGW
978readline_win_pkg() {
979    local build_dir=$1
980    local install_dir=$2
981    local name="Readline"
982    local readline_ver="5.0.1"
983    local readline_url="http://downloads.sourceforge.net/project/gnuwin32/readline/5.0-1/readline-5.0-1-bin.zip?r=&ts=1468492036&use_mirror=freefr"
984    local readline_md5="91beae8726edd7ad529f67d82153e61a"
985    local readline_file="readline-bin.zip"
986    local readline_dir="readline-bin"
987
988    if [ $incremental -eq 1 ] && [ -e $status_dir/$name.success ] && [ `cat $status_dir/$name.success` = $readline_md5 ]; then
989        echo "$name already built"
990        return
991    else
992        rm -f $status_dir/$name.success
993    fi
994
995    package_download $name $readline_url $readline_file $readline_md5
996    if [ $download_only -eq 1 ]; then
997        return
998    fi
999
1000    package_extract $name $readline_file $readline_dir
1001
1002    # manually copy binary files
1003    cp -R $readline_dir/include/* $install_dir/include/ || exit 1
1004    # fix library name
1005    cp $readline_dir/lib/libreadline.dll.a $install_dir/lib/libreadline.a || exit 1
1006    echo $readline_md5 > $status_dir/$name.success
1007}
1008
1009build_sdk() {
1010    local install_dir=$1
1011    local debug=$2
1012    local static_flags=""
1013    local readline_flags=""
1014    local freeimage_flags=""
1015    local libuv_flags=""
1016    local libraw_flags=""
1017    local megaapi_flags=""
1018    local openssl_flags=""
1019    local sodium_flags="--without-sodium"
1020    local cwd=$(pwd)
1021
1022    if [ $incremental -eq 1 ] && [ -e $status_dir/MegaSDK.success ]; then
1023        echo "MegaSDK already built"
1024        return
1025    else
1026        rm -f $status_dir/MegaSDK.success
1027    fi
1028
1029    echo "Configuring MEGA SDK"
1030
1031    ./autogen.sh || exit 1
1032
1033    # use either static build (by the default) or dynamic
1034    if [ $use_dynamic -eq 1 ]; then
1035        static_flags="--enable-shared"
1036    else
1037        static_flags="--disable-shared --enable-static"
1038    fi
1039
1040    # disable freeimage
1041    if [ $disable_freeimage -eq 0 ]; then
1042        freeimage_flags="--with-freeimage=$install_dir"
1043    else
1044        freeimage_flags="--without-freeimage"
1045    fi
1046
1047    # enable libuv
1048    if [ $enable_libuv -eq 1 ]; then
1049        libuv_flags="--with-libuv=$install_dir"
1050    else
1051        libuv_flags="--without-libuv"
1052    fi
1053
1054    # enable libraw
1055    if [ $enable_libraw -eq 1 ]; then
1056        libraw_flags="--with-libraw=$install_dir"
1057    fi
1058
1059    # use local or system MediaInfo
1060    local mediainfo_flags=""
1061    if [ $disable_mediainfo -eq 0 ]; then
1062        mediainfo_flags="--with-libzen=$install_dir --with-libmediainfo=$install_dir"
1063    fi
1064
1065    # enable megaapi
1066    if [ $enable_megaapi -eq 0 ]; then
1067        megaapi_flags="--disable-megaapi"
1068    fi
1069
1070    # add readline and termcap flags if building examples
1071    if [ $readline_build -eq 1 ] || [ -z "$no_examples" ]; then
1072        readline_flags=" \
1073            --with-readline=$install_dir \
1074            --with-termcap=$install_dir \
1075            "
1076    fi
1077
1078    if [ $disable_ssl -eq 0 ]; then
1079        openssl_flags="--with-openssl=$install_dir"
1080    fi
1081
1082    if [ $enable_sodium -eq 1 ]; then
1083        sodium_flags="--with-sodium=$install_dir"
1084    fi
1085
1086    if [ "$(expr substr $(uname -s) 1 10)" != "MINGW32_NT" ]; then
1087        local configure_flags="\
1088            $configure_cross_options \
1089            $static_flags \
1090            --disable-silent-rules \
1091            --disable-curl-checks \
1092            $megaapi_flags \
1093            $openssl_flags \
1094            --with-cryptopp=$install_dir \
1095            $sodium_flags \
1096            --with-zlib=$install_dir \
1097            --with-sqlite=$install_dir \
1098            --with-cares=$install_dir \
1099            --with-curl=$install_dir \
1100            $freeimage_flags \
1101            $libuv_flags \
1102            $libraw_flags \
1103            $readline_flags \
1104            $disable_posix_threads \
1105            $no_examples \
1106            $config_opts \
1107            $mediainfo_flags \
1108            --prefix=$install_dir \
1109            $debug"
1110        echo "running: ./configure $configure_flags"
1111        ./configure $configure_flags || exit 1
1112    # Windows (MinGW) build, uses WinHTTP instead of cURL + c-ares, without OpenSSL
1113    else
1114        ./configure \
1115            $static_flags \
1116            --disable-silent-rules \
1117            --without-openssl \
1118            $megaapi_flags \
1119            --with-cryptopp=$install_dir \
1120            $sodium_flags \
1121            --with-zlib=$install_dir \
1122            --with-sqlite=$install_dir \
1123            --without-cares \
1124            --without-curl \
1125            --with-winhttp=$cwd \
1126            $freeimage_flags \
1127            $libuv_flags \
1128            $libraw_flags \
1129            $readline_flags \
1130            $disable_posix_threads \
1131            $no_examples \
1132            $config_opts \
1133            $mediainfo_flags \
1134            --prefix=$install_dir \
1135            $debug || exit 1
1136    fi
1137
1138    echo "MEGA SDK is configured"
1139
1140    if [ $configure_only -eq 0 ]; then
1141        echo "Building MEGA SDK"
1142        make clean
1143        if [ "$(expr substr $(uname -s) 1 10)" != "MINGW32_NT" ]; then
1144            make -j9 || exit 1
1145        else
1146            make
1147        fi
1148        make install
1149        local exit_code=$?
1150        if [ $exit_code -eq 0 ]; then
1151            echo $exit_code > $status_dir/MegaSDK.success
1152        fi
1153    fi
1154}
1155
1156display_help() {
1157    local app=$(basename "$0")
1158    echo ""
1159    echo "Usage:"
1160    echo " $app [-a] [-c] [-h] [-d] [-e] [-f] [-g] [-l] [-m opts] [-n] [-o path] [-p path] [-q] [-r] [-s] [-t] [-w] [-x opts] [-y] [z]"
1161    echo ""
1162    echo "By the default this script builds static megacli executable."
1163    echo "This script can be run with numerous options to configure and build MEGA SDK."
1164    echo ""
1165    echo "Options:"
1166    echo " -a : Enable MegaApi"
1167    echo " -b : Only build dependencies"
1168    echo " -c : Configure MEGA SDK and exit, do not build it"
1169    echo " -d : Enable debug build"
1170    echo " -e : Enable cares"
1171    echo " -f : Disable FreeImage"
1172    echo " -g : Enable curl"
1173    echo " -i : Disable external media info"
1174    echo " -I : Incremental build.  Already built dependencies will be skipped"
1175    echo " -l : Use local software archive files instead of downloading"
1176    echo " -n : Disable example applications"
1177    echo " -s : Disable OpenSSL"
1178    echo " -r : Enable Android build"
1179    echo " -R : Build ReadLine too (even with example apps disabled)"
1180    echo " -t : Disable POSIX Threads support"
1181    echo " -u : Enable Sodium cryptographic library"
1182    echo " -v : Enable libuv"
1183    echo " -w : Download software archives and exit"
1184    echo " -y : Build dynamic library and executable (instead of static)"
1185    echo " -m [opts]: make options"
1186    echo " -x [opts]: configure options"
1187    echo " -X [args]: Indicate that we are cross-compiling (and so don't call any ./config scripts)"
1188    echo " -C [args]: cross-compile flags (--host etc) for 'configure' scripts (when using -X)"
1189    echo " -O [arg]: pass the os/compiler flag for OpenSSL (when using -X)"
1190    echo " -o [path]: Directory to store and look for downloaded archives"
1191    echo " -p [path]: Installation directory"
1192    echo " -q : Use Crypto++"
1193    echo " -z : Disable libz"
1194    echo " -0 : Turn off optimisations (in case of issues on old compilers)"
1195    echo ""
1196}
1197
1198main() {
1199    local cwd=$(pwd)
1200    local work_dir=$cwd"/sdk_build"
1201    local build_dir=$work_dir/"build"
1202    local install_dir=$work_dir/"install"
1203    local debug=""
1204    # by the default store archives in work_dir
1205    local_dir=$work_dir
1206    status_dir=$work_dir
1207
1208    while getopts ":habcdefgiIlm:no:p:rRsS:tuvyx:XC:O:wWqz0" opt; do
1209        case $opt in
1210            h)
1211                display_help $0
1212                exit 1
1213                ;;
1214            a)
1215                echo "* Enabling MegaApi"
1216                enable_megaapi=1
1217                ;;
1218            b)
1219                only_build_dependencies=1
1220                echo "* Building dependencies only."
1221                ;;
1222            c)
1223                echo "* Configure only"
1224                configure_only=1
1225                ;;
1226            d)
1227                echo "* DEBUG build"
1228                debug="--enable-debug"
1229                ;;
1230            e)
1231                echo "* Enabling external c-ares"
1232                enable_cares=1
1233                ;;
1234            f)
1235                echo "* Disabling external FreeImage"
1236                disable_freeimage=1
1237                ;;
1238            g)
1239                echo "* Enabling external Curl"
1240                enable_curl=1
1241                ;;
1242            i)
1243                echo "* Disabling external MediaInfo"
1244                disable_mediainfo=1
1245                ;;
1246            I)
1247                echo "* Incremental build - skipping already built/downloaded dependencies"
1248                [ -d $persistent_path ] && echo "* Using $persistent_path as a backup for dependecies."
1249                incremental=1
1250                ;;
1251            l)
1252                echo "* Using local files"
1253                use_local=1
1254                ;;
1255            m)
1256                make_opts="$OPTARG"
1257                ;;
1258            n)
1259                no_examples="--disable-examples"
1260                ;;
1261            o)
1262                local_dir=$(readlink -f $OPTARG)
1263                if [ ! -d $local_dir ]; then
1264                    mkdir -p $local_dir || exit 1
1265                fi
1266                echo "* Storing local archive files in $local_dir"
1267                ;;
1268            p)
1269                install_dir=$(readlink -f $OPTARG)
1270                echo "* Installing into $install_dir"
1271                ;;
1272            q)
1273                echo "* Enabling external Crypto++"
1274                enable_cryptopp=1
1275                ;;
1276            r)
1277                echo "* Building for Android"
1278                android_build=1
1279                ;;
1280            R)
1281                echo "* Building readline for clients"
1282                readline_build=1
1283                ;;
1284            s)
1285                echo "* Disabling OpenSSL"
1286                disable_ssl=1
1287                ;;
1288            S)
1289                echo "* extra OpenSSL config params: $OPTARG"
1290                extra_openssl_params="$OPTARG"
1291                ;;
1292            t)
1293                disable_posix_threads="--disable-posix-threads"
1294                ;;
1295            u)
1296                enable_sodium=1
1297                echo "* Enabling external Sodium."
1298                ;;
1299            v)
1300                enable_libuv=1
1301                echo "* Enabling external libuv."
1302                ;;
1303            w)
1304                download_only=1
1305                echo "* Downloading software archives only."
1306                ;;
1307            W)
1308                enable_libraw=1
1309                echo "* Enabling external libraw."
1310                ;;
1311            x)
1312                config_opts="$OPTARG"
1313                echo "* Using configuration options: $config_opts"
1314                ;;
1315            X)
1316                cross_compiling=1
1317                echo "* cross-compiling"
1318                ;;
1319            C)
1320                configure_cross_options="$OPTARG"
1321                echo "* configure cross compile options: $configure_cross_options"
1322                ;;
1323            O)
1324                openssl_cross_option="$OPTARG"
1325                echo "* OpenSSL Configure option: $openssl_cross_option"
1326                ;;
1327            y)
1328                use_dynamic=1
1329                echo "* Building dynamic library and executable."
1330                ;;
1331            z)
1332                disable_zlib=1
1333                echo "* Disabling external libz."
1334                ;;
1335            0)
1336                no_optimisation=1
1337                echo "* Disabling compiler optimisations."  # some older versions of gcc have optimisations problems with eg. OpenSSL - rsa_test suite can fail
1338                ;;
1339            \?)
1340                display_help $0
1341                exit 1
1342                ;;
1343            *)
1344                display_help $0
1345                exit 1
1346                ;;
1347        esac
1348    done
1349    shift $((OPTIND-1))
1350
1351    check_apps
1352
1353    if [ "$(expr substr $(uname -s) 1 10)" = "MINGW32_NT" ]; then
1354        if [ ! -f "$cwd/winhttp.h" -o ! -f "$cwd/winhttp.lib"  ]; then
1355            echo "ERROR! Windows build requires WinHTTP header and library to be present in MEGA SDK project folder!"
1356            echo "Please get both winhttp.h and winhttp.lib files an put them into the MEGA SDK project's root folder."
1357            exit 1
1358        fi
1359    fi
1360
1361    trap on_exit_error EXIT
1362
1363    if [ $download_only -eq 0 ]; then
1364        if [ ! -d $build_dir ]; then
1365            mkdir -p $build_dir || exit 1
1366        fi
1367        if [ ! -d $install_dir ]; then
1368            mkdir -p $install_dir || exit 1
1369        fi
1370
1371        cd $build_dir
1372    fi
1373
1374    if [ $incremental -eq 0 ]; then
1375        rm -fr *.log
1376    else
1377        # Check if backup exists on persistent path and restore previous status.
1378        if [ -d $persistent_path/sdk/ ];then
1379            echo "Recovering previous libs and staus, if any."
1380            if [ -d $persistent_path/sdk/3rdparty ]; then
1381                cp --preserve=timestamps -r $persistent_path/sdk/3rdparty/* $install_dir/
1382                sed -i "s#/[^ ]*/lib\([^[:alnum:]]\)#$install_dir/lib\1#g" $install_dir/lib*/*.la
1383            fi
1384            [ -d $persistent_path/sdk/3rdparty_status ] && cp --preserve=timestamps $persistent_path/sdk/3rdparty_status/* $status_dir/
1385        fi
1386    fi
1387
1388    export PREFIX=$install_dir
1389    local old_pkg_conf=$PKG_CONFIG_PATH
1390    export PKG_CONFIG_PATH=$install_dir/lib/pkgconfig/
1391    export LD_LIBRARY_PATH="$install_dir/lib"
1392    export LD_RUN_PATH="$install_dir/lib"
1393
1394    if [ $android_build -eq 1 ]; then
1395        echo "SYSROOT: $SYSROOT"
1396    fi
1397
1398    if [ "$(expr substr $(uname -s) 1 10)" != "MINGW32_NT" ]; then
1399        if [ $disable_ssl -eq 0 ]; then
1400            openssl_pkg $build_dir $install_dir
1401        fi
1402    fi
1403
1404    if [ $enable_cryptopp -eq 1 ]; then
1405        cryptopp_pkg $build_dir $install_dir
1406    fi
1407
1408    if [ $enable_sodium -eq 1 ]; then
1409        sodium_pkg $build_dir $install_dir
1410    fi
1411
1412    if [ $disable_zlib -eq 0 ]; then
1413        zlib_pkg $build_dir $install_dir
1414    fi
1415
1416    sqlite_pkg $build_dir $install_dir
1417
1418    if [ $enable_cares -eq 1 ]; then
1419        cares_pkg $build_dir $install_dir
1420    fi
1421
1422    if [ $enable_curl -eq 1 ]; then
1423        curl_pkg $build_dir $install_dir
1424    fi
1425
1426    if [ $enable_libuv -eq 1 ]; then
1427        libuv_pkg $build_dir $install_dir
1428    fi
1429
1430    if [ $enable_libraw -eq 1 ]; then
1431        libraw_pkg $build_dir $install_dir
1432    fi
1433
1434    if [ $disable_freeimage -eq 0 ]; then
1435        freeimage_pkg $build_dir $install_dir $cwd
1436    fi
1437
1438    if [ $disable_mediainfo -eq 0 ]; then
1439        mediainfo_pkg $build_dir $install_dir $cwd
1440    fi
1441
1442    # Build readline and termcap if no_examples isn't set
1443    if [ $readline_build -eq 1 ] || [ -z "$no_examples" ]; then
1444        if [ "$(expr substr $(uname -s) 1 10)" != "MINGW32_NT" ]; then
1445            readline_pkg $build_dir $install_dir
1446            termcap_pkg $build_dir $install_dir
1447        else
1448           readline_win_pkg  $build_dir $install_dir
1449       fi
1450    fi
1451
1452    if [ $download_only -eq 0 ] && [ $only_build_dependencies -eq 0 ]; then
1453        cd $cwd
1454
1455        #fix libtool bug (prepends some '=' to certain paths)
1456        for i in `find $install_dir -name "*.la"`; do sed -i "s#=/#/#g" $i; done
1457
1458        if [ $android_build -eq 1 ]; then
1459            export "CXXFLAGS=$CXXFLAGS -std=c++11"
1460        fi
1461        export "CXXFLAGS=$CXXFLAGS -DCRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562"
1462        build_sdk $install_dir $debug
1463    fi
1464
1465    if [ $incremental -eq 1 ]; then
1466        # Check if persistent path exists to backup current build status.
1467        if [ -d $persistent_path ];then
1468            # Creating directories to store current libraries and status.
1469            mkdir -p $persistent_path/sdk/3rdparty
1470            mkdir -p $persistent_path/sdk/3rdparty_status
1471            echo "Using $persistent_path/sdk/ as persistent path to store current build status."
1472            cp --preserve=timestamps -r $install_dir/* $persistent_path/sdk/3rdparty/
1473            rm -f $persistent_path/sdk/3rdparty_status/*
1474            cp --preserve=timestamps $status_dir/*.success $persistent_path/sdk/3rdparty_status/
1475        fi
1476    fi
1477
1478    unset PREFIX
1479    unset LD_RUN_PATH
1480    unset LD_LIBRARY_PATH
1481    export PKG_CONFIG_PATH=$old_pkg_conf
1482    trap on_exit_ok EXIT
1483}
1484
1485main "$@"
1486