Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save grabthar/9acb66ea3afe199039e89dcf396e9c3a to your computer and use it in GitHub Desktop.
Save grabthar/9acb66ea3afe199039e89dcf396e9c3a to your computer and use it in GitHub Desktop.
# Add to ~/.bashrc
# call load_msenv [year] from a shell to load the enviroment
function load_msenv() {
local msversion_year=2019
if [ $# -gt 0 ]; then
msversion_year=$1
fi
case $msversion_year in
2017)
msversion_prod=15
;;
2019)
msversion_prod=16
;;
*)
>&2 printf "Invalid version year"
return 1
esac
msversion_prod_p1=$(($msversion_prod+1))
local VSWHERE='C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe'
installPath=$("$VSWHERE" -products '*' -version "[$msversion_prod,$msversion_prod_p1\)" -property installationPath)
local vcvarsall="${installPath}\\VC\\Auxiliary\\Build\\vcvarsall.bat"
local msenv="$HOME/.msenv_${msversion_year}_bash"
if [ ! -f "$msenv" ]; then
local msenvbatch="__print_ms_env.bat"
echo "@echo off" > "$msenvbatch"
echo "call \"${vcvarsall}\" x64" >> "$msenvbatch"
echo "set" >> "$msenvbatch"
cmd "/C $msenvbatch" > "$msenv.tmp"
rm -f "$msenvbatch"
grep -E '^PATH=' "$msenv.tmp" | \
sed \
-e 's/\(.*\)=\(.*\)/export \1="\2"/g' \
-e 's/\([a-zA-Z]\):[\\\/]/\/\1\//g' \
-e 's/\\/\//g' \
-e 's/;\//:\//g' \
> "$msenv"
# Don't mess with CL compilation env
grep -E '^(INCLUDE|LIB|LIBPATH)=' "$msenv.tmp" | \
sed \
-e 's/\(.*\)=\(.*\)/export \1="\2"/g' \
>> "$msenv"
rm "$msenv.tmp"
fi
source "$msenv"
}
export -f load_msenv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment