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