before_install.sh 3.2 KB

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