1*7bdf38e5Schristos#!/bin/bash 2*7bdf38e5Schristos 3*7bdf38e5Schristosset -e 4*7bdf38e5Schristos 5*7bdf38e5Schristos# The purpose of this script is to install build dependencies and set 6*7bdf38e5Schristos# $build_env to a function that sets appropriate environment variables, 7*7bdf38e5Schristos# to enable (mingw32|mingw64) environment if we want to compile with gcc, or 8*7bdf38e5Schristos# (mingw32|mingw64) + vcvarsall.bat if we want to compile with cl.exe 9*7bdf38e5Schristos 10*7bdf38e5Schristosif [[ "$TRAVIS_OS_NAME" != "windows" ]]; then 11*7bdf38e5Schristos echo "Incorrect \$TRAVIS_OS_NAME: expected windows, got $TRAVIS_OS_NAME" 12*7bdf38e5Schristos exit 1 13*7bdf38e5Schristosfi 14*7bdf38e5Schristos 15*7bdf38e5Schristos[[ ! -f C:/tools/msys64/msys2_shell.cmd ]] && rm -rf C:/tools/msys64 16*7bdf38e5Schristoschoco uninstall -y mingw 17*7bdf38e5Schristoschoco upgrade --no-progress -y msys2 18*7bdf38e5Schristos 19*7bdf38e5Schristosmsys_shell_cmd="cmd //C RefreshEnv.cmd && set MSYS=winsymlinks:nativestrict && C:\\tools\\msys64\\msys2_shell.cmd" 20*7bdf38e5Schristos 21*7bdf38e5Schristosmsys2() { $msys_shell_cmd -defterm -no-start -msys2 -c "$*"; } 22*7bdf38e5Schristosmingw32() { $msys_shell_cmd -defterm -no-start -mingw32 -c "$*"; } 23*7bdf38e5Schristosmingw64() { $msys_shell_cmd -defterm -no-start -mingw64 -c "$*"; } 24*7bdf38e5Schristos 25*7bdf38e5Schristosif [[ "$CROSS_COMPILE_32BIT" == "yes" ]]; then 26*7bdf38e5Schristos mingw=mingw32 27*7bdf38e5Schristos mingw_gcc_package_arch=i686 28*7bdf38e5Schristoselse 29*7bdf38e5Schristos mingw=mingw64 30*7bdf38e5Schristos mingw_gcc_package_arch=x86_64 31*7bdf38e5Schristosfi 32*7bdf38e5Schristos 33*7bdf38e5Schristosif [[ "$CC" == *"gcc"* ]]; then 34*7bdf38e5Schristos $mingw pacman -S --noconfirm --needed \ 35*7bdf38e5Schristos autotools \ 36*7bdf38e5Schristos git \ 37*7bdf38e5Schristos mingw-w64-${mingw_gcc_package_arch}-make \ 38*7bdf38e5Schristos mingw-w64-${mingw_gcc_package_arch}-gcc \ 39*7bdf38e5Schristos mingw-w64-${mingw_gcc_package_arch}-binutils 40*7bdf38e5Schristos build_env=$mingw 41*7bdf38e5Schristoselif [[ "$CC" == *"cl"* ]]; then 42*7bdf38e5Schristos $mingw pacman -S --noconfirm --needed \ 43*7bdf38e5Schristos autotools \ 44*7bdf38e5Schristos git \ 45*7bdf38e5Schristos mingw-w64-${mingw_gcc_package_arch}-make \ 46*7bdf38e5Schristos mingw-w64-${mingw_gcc_package_arch}-binutils 47*7bdf38e5Schristos 48*7bdf38e5Schristos # In order to use MSVC compiler (cl.exe), we need to correctly set some environment 49*7bdf38e5Schristos # variables, namely PATH, INCLUDE, LIB and LIBPATH. The correct values of these 50*7bdf38e5Schristos # variables are set by a batch script "vcvarsall.bat". The code below generates 51*7bdf38e5Schristos # a batch script that calls "vcvarsall.bat" and prints the environment variables. 52*7bdf38e5Schristos # 53*7bdf38e5Schristos # Then, those environment variables are transformed from cmd to bash format and put 54*7bdf38e5Schristos # into a script $apply_vsenv. If cl.exe needs to be used from bash, one can 55*7bdf38e5Schristos # 'source $apply_vsenv' and it will apply the environment variables needed for cl.exe 56*7bdf38e5Schristos # to be located and function correctly. 57*7bdf38e5Schristos # 58*7bdf38e5Schristos # At last, a function "mingw_with_msvc_vars" is generated which forwards user input 59*7bdf38e5Schristos # into a correct mingw (32 or 64) subshell that automatically performs 'source $apply_vsenv', 60*7bdf38e5Schristos # making it possible for autotools to discover and use cl.exe. 61*7bdf38e5Schristos vcvarsall="vcvarsall.tmp.bat" 62*7bdf38e5Schristos echo "@echo off" > $vcvarsall 63*7bdf38e5Schristos echo "call \"c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\\\vcvarsall.bat\" $USE_MSVC" >> $vcvarsall 64*7bdf38e5Schristos echo "set" >> $vcvarsall 65*7bdf38e5Schristos 66*7bdf38e5Schristos apply_vsenv="./apply_vsenv.sh" 67*7bdf38e5Schristos cmd //C $vcvarsall | grep -E "^PATH=" | sed -n -e 's/\(.*\)=\(.*\)/export \1=$PATH:"\2"/g' \ 68*7bdf38e5Schristos -e 's/\([a-zA-Z]\):[\\\/]/\/\1\//g' \ 69*7bdf38e5Schristos -e 's/\\/\//g' \ 70*7bdf38e5Schristos -e 's/;\//:\//gp' > $apply_vsenv 71*7bdf38e5Schristos cmd //C $vcvarsall | grep -E "^(INCLUDE|LIB|LIBPATH)=" | sed -n -e 's/\(.*\)=\(.*\)/export \1="\2"/gp' >> $apply_vsenv 72*7bdf38e5Schristos 73*7bdf38e5Schristos cat $apply_vsenv 74*7bdf38e5Schristos mingw_with_msvc_vars() { $msys_shell_cmd -defterm -no-start -$mingw -c "source $apply_vsenv && ""$*"; } 75*7bdf38e5Schristos build_env=mingw_with_msvc_vars 76*7bdf38e5Schristos 77*7bdf38e5Schristos rm -f $vcvarsall 78*7bdf38e5Schristoselse 79*7bdf38e5Schristos echo "Unknown C compiler: $CC" 80*7bdf38e5Schristos exit 1 81*7bdf38e5Schristosfi 82*7bdf38e5Schristos 83*7bdf38e5Schristosecho "Build environment function: $build_env" 84