Bump bockbuild
[mono-project.git] / msvc / setup-vs-msvcbuild-env.bat
blob4c4a9257585c2d74a2b333d8fc21e76673a73f4c
1 :: Set up VS MSVC environment depending on installed VS versions.
2 :: Script will setup environment variables directly in callers environment.
4 :: Make sure we can restore current working directory after setting up environment.
5 :: Some of the VS scripts can change the current working directory.
6 set CALLER_WD=%CD%
8 :: Get path for current running script.
9 set RUN_SETUP_VS_MSVCBUILD_ENV_SCRIPT_PATH=%~dp0
11 :: NOTE, MSVC build Mono full AOT tooling currently support 64-bit AMD codegen. Below will only setup
12 :: amd64 versions of VS MSVC build environment and corresponding Clang compiler.
13 set VS_2015_TOOLCHAIN_ARCH=amd64
14 set VS_2015_VCVARS_ARCH=%VS_2015_TOOLCHAIN_ARCH%\vcvars64.bat
15 set VS_2015_CLANG_ARCH=%VS_2015_TOOLCHAIN_ARCH%
16 set VS_2017_VCVARS_ARCH=vcvars64.bat
17 set VS_2017_CLANG_ARCH=HostX64
18 set VS_2019_VCVARS_ARCH=%VS_2017_VCVARS_ARCH%
20 :: 32-bit AOT toolchains for MSVC build mono-sgen.exe is currently not supported.
21 :: set VS_2015_TOOLCHAIN_ARCH=x86
22 :: set VS_2015_VCVARS_ARCH=vcvars32.bat
23 :: set VS_2015_CLANG_ARCH=%VS_2015_TOOLCHAIN_ARCH%
24 :: set VS_2017_VCVARS_ARCH=vcvars32.bat
25 :: set VS_2017_CLANG_ARCH=HostX86
26 :: set VS_2019_VCVARS_ARCH=%VS_2017_VCVARS_ARCH%
28 set VS_CLANG_TOOLS_BIN_PATH=
30 :: VS2019/VS2017 includes vswhere.exe that can be used to locate current VS installation.
31 set VSWHERE_TOOLS_BIN=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe
33 :: Visual Studio 2015 == 14.0
34 if "%VisualStudioVersion%" == "14.0" (
35     goto SETUP_VS_2015
38 :: Visual Studio 2017 == 15.0
39 if "%VisualStudioVersion%" == "15.0" (
40     goto SETUP_VS_2017
43 :: Visual Studio 2019 == 16.0
44 if "%VisualStudioVersion%" == "16.0" (
45     goto SETUP_VS_2019
48 :SETUP_VS_2019
50 set VS_2019_VCINSTALL_DIR=
52 :: Try to locate installed VS2019 VC environment.
53 if exist "%VSWHERE_TOOLS_BIN%" (
54     for /f "tokens=*" %%a in ('"%VSWHERE_TOOLS_BIN%" -version [16.0^,17.0] -prerelease -property installationPath') do (
55         set VS_2019_VCINSTALL_DIR=%%a\VC\
56     )
59 :: Try to locate installed VS2019 Clang.
60 set VS_2019_CLANG_TOOLS_BIN_PATH=%VS_2019_VCINSTALL_DIR%Tools\llvm\bin\
61 set VS_2019_CLANG_TOOLS_BIN=%VS_2019_CLANG_TOOLS_BIN_PATH%clang.exe
62 if not exist "%VS_2019_CLANG_TOOLS_BIN%" (
63         goto SETUP_VS_2017
66 :SETUP_VS_2019_BUILD_TOOLS
68 :: Try to locate VS2019 build tools installation.
69 set VS_2019_BUILD_TOOLS_INSTALL_DIR=%ProgramFiles(x86)%\Microsoft Visual Studio\2019\BuildTools\
70 set VS_2019_BUILD_TOOLS_CMD=%VS_2019_BUILD_TOOLS_INSTALL_DIR%VC\Auxiliary\Build\%VS_2019_VCVARS_ARCH%
72 :: Setup VS2019 VC development environment using build tools installation.
73 call :setup_build_env "%VS_2019_BUILD_TOOLS_CMD%" "" "%CALLER_WD%" && (
74     set "VS_CLANG_TOOLS_BIN_PATH=%VS_2019_CLANG_TOOLS_BIN_PATH%"
75     set VS_DEFAULT_PLATFORM_TOOL_SET=v142
76     goto ON_EXIT
79 :SETUP_VS_2019_VC
81 :: Try to locate installed VS2019 VC environment.
82 set VS_2019_DEV_CMD=%VS_2019_VCINSTALL_DIR%Auxiliary\Build\%VS_2019_VCVARS_ARCH%
84 :: Setup VS2019 VC development environment using VS installation.
85 call :setup_build_env "%VS_2019_DEV_CMD%" "" "%CALLER_WD%" && (
86     set "VS_CLANG_TOOLS_BIN_PATH=%VS_2017_CLANG_TOOLS_BIN_PATH%"
87     set VS_DEFAULT_PLATFORM_TOOL_SET=v142
88     goto ON_EXIT
91 :SETUP_VS_2017
93 set VS_2017_VCINSTALL_DIR=
95 :: Try to locate installed VS2017 VC environment.
96 if exist "%VSWHERE_TOOLS_BIN%" (
97     for /f "tokens=*" %%a in ('"%VSWHERE_TOOLS_BIN%" -version [15.0^,16.0] -property installationPath') do (
98         set VS_2017_VCINSTALL_DIR=%%a\VC\
99     )
102 :: Try to locate installed VS2017 Clang.
103 SET VS_2017_CLANG_VERSION_FILE=%VS_2017_VCINSTALL_DIR%Auxiliary/Build/Microsoft.ClangC2Version.default.txt
104 if not exist "%VS_2017_CLANG_VERSION_FILE%" (
105         goto SETUP_VS_2015
108 set /p VS_2017_CLANG_VERSION=<"%VS_2017_CLANG_VERSION_FILE%"
109 set VS_2017_CLANG_TOOLS_BIN_PATH=%VS_2017_VCINSTALL_DIR%Tools\ClangC2\%VS_2017_CLANG_VERSION%\bin\%VS_2017_CLANG_ARCH%\
110 set VS_2017_CLANG_TOOLS_BIN=%VS_2017_CLANG_TOOLS_BIN_PATH%clang.exe
111 if not exist "%VS_2017_CLANG_TOOLS_BIN%" (
112         goto SETUP_VS_2015
115 :SETUP_VS_2017_BUILD_TOOLS
117 :: Try to locate VS2017 build tools installation.
118 set VS_2017_BUILD_TOOLS_INSTALL_DIR=%ProgramFiles(x86)%\Microsoft Visual Studio\2017\BuildTools\
119 set VS_2017_BUILD_TOOLS_CMD=%VS_2017_BUILD_TOOLS_INSTALL_DIR%VC\Auxiliary\Build\%VS_2017_VCVARS_ARCH%
121 :: Setup VS2017 VC development environment using build tools installation.
122 call :setup_build_env "%VS_2017_BUILD_TOOLS_CMD%" "" "%CALLER_WD%" && (
123     set "VS_CLANG_TOOLS_BIN_PATH=%VS_2017_CLANG_TOOLS_BIN_PATH%"
124     set VS_DEFAULT_PLATFORM_TOOL_SET=v141
125     goto ON_EXIT
128 :SETUP_VS_2017_VC
130 :: Try to locate installed VS2017 VC environment.
131 set VS_2017_DEV_CMD=%VS_2017_VCINSTALL_DIR%Auxiliary\Build\%VS_2017_VCVARS_ARCH%
133 :: Setup VS2017 VC development environment using VS installation.
134 call :setup_build_env "%VS_2017_DEV_CMD%" "" "%CALLER_WD%" && (
135     set "VS_CLANG_TOOLS_BIN_PATH=%VS_2017_CLANG_TOOLS_BIN_PATH%"
136     set VS_DEFAULT_PLATFORM_TOOL_SET=v141
137     goto ON_EXIT
140 :SETUP_VS_2015
142 set VS_2015_VCINSTALL_DIR=%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\VC\
144 :: Try to locate installed VS2015 Clang.
145 SET VS_2015_CLANG_TOOLS_BIN_PATH=%VS_2015_VCINSTALL_DIR%ClangC2\bin\%VS_2015_CLANG_ARCH%\
146 SET VS_2015_CLANG_TOOLS_BIN=%VS_2015_CLANG_TOOLS_BIN_PATH%clang.exe
148 if not exist "%VS_2015_CLANG_TOOLS_BIN%" (
149     goto ON_ENV_ERROR
152 :SETUP_VS_2015_BUILD_TOOLS
154 :: Try to locate VS2015 build tools installation.
155 set VS_2015_BUILD_TOOLS_INSTALL_DIR=%ProgramFiles(x86)%\Microsoft Visual C++ Build Tools\
156 set VS_2015_BUILD_TOOLS_CMD=%VS_2015_BUILD_TOOLS_INSTALL_DIR%vcbuildtools.bat
158 :: Setup VS2015 VC development environment using build tools installation.
159 call :setup_build_env "%VS_2015_BUILD_TOOLS_CMD%" "%VS_2015_TOOLCHAIN_ARCH%" "%CALLER_WD%" && (
160     set "VS_CLANG_TOOLS_BIN_PATH=%VS_2015_CLANG_TOOLS_BIN_PATH%"
161     set VS_DEFAULT_PLATFORM_TOOL_SET=v140
162     goto ON_EXIT
165 :SETUP_VS_2015_VC
167 :: Try to locate installed VS2015 VC environment.
168 set VS_2015_DEV_CMD=%VS_2015_VCINSTALL_DIR%bin\%VS_2015_VCVARS_ARCH%
170 call :setup_build_env "%VS_2015_DEV_CMD%" "" "%CALLER_WD%" && (
171     set "VS_CLANG_TOOLS_BIN_PATH=%VS_2015_CLANG_TOOLS_BIN_PATH%"
172     set VS_DEFAULT_PLATFORM_TOOL_SET=v140
173     goto ON_EXIT
176 :ON_ENV_ERROR
178 echo Warning, failed to setup VS build environment needed by VS tooling.
179 echo Incomplete build environment can cause build error's due to missing compiler,
180 echo linker and platform libraries.
182 exit /b 1
184 :ON_EXIT
186 :: Add Clang folders to PATH
187 set "PATH=%VS_CLANG_TOOLS_BIN_PATH%;%PATH%"
189 exit /b 0
191 :setup_build_env
193 :: Check if VS build environment script exists.
194 if not exist "%~1" (
195     goto setup_build_env_error
198 :: Run VS build environment script.
199 call "%~1" %~2 > NUL
201 :: Restore callers working directory in case it has been changed by VS scripts.
202 cd /d "%~3"
204 goto setup_build_env_exit
206 :setup_build_env_error
207 exit /b 1
209 :setup_build_env_exit
210 goto :EOF