1 :: Set up VS MSVC environment when running MSVC build mono-sgen.exe with all supplied arguments.
\r
2 :: Simplify the setup of VS and MSVC toolchain, when running Mono AOT compiler
\r
3 :: since it need to locate correct compiler and OS libraries as well as clang.exe and link.exe
\r
4 :: from VS setup for the corresponding architecture.
\r
10 set EXECUTE_RESULT=1
\r
12 :: Make sure we can restore current working directory after setting up environment.
\r
13 :: Some of the VS scripts can change the current working directory.
\r
16 :: Get path for current running script.
\r
17 set RUN_MONO_SGEN_MSVC_SCRIPT_PATH=%~dp0
\r
19 :: If we are running from none Windows shell we will need to restore a clean PATH
\r
20 :: before setting up VS MSVC build environment. If not there is a risk we will pick up
\r
21 :: for example cygwin binaries when running toolchain commands not explicitly setup by VS MSVC build environment.
\r
24 if "%SHELL%" == "/bin/bash" (
\r
25 for /f "tokens=2,*" %%a in ('%WINDIR%\System32\reg.exe query "HKCU\Environment" /v "Path" ^| %WINDIR%\System32\find.exe /i "REG_"') do (
\r
26 SET HKCU_ENV_PATH=%%b
\r
28 for /f "tokens=2,*" %%a in ('%WINDIR%\System32\reg.exe query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "Path" ^| %WINDIR%\System32\find.exe /i "REG_"') do (
\r
29 SET HKLM_ENV_PATH=%%b
\r
33 :: Restore default path, if we are running from none Windows shell.
\r
34 if "%SHELL%" == "/bin/bash" (
\r
35 call :restore_default_path "%HKCU_ENV_PATH%" "%HKLM_ENV_PATH%"
\r
38 :: NOTE, MSVC build mono-sgen.exe AOT compiler currently support 64-bit AMD codegen. Below will only setup
\r
39 :: amd64 versions of VS MSVC build environment and corresponding ClangC2 compiler.
\r
41 set VS_2015_TOOLCHAIN_ARCH=amd64
\r
42 set VS_2015_VCVARS_ARCH=%VS_2015_TOOLCHAIN_ARCH%\vcvars64.bat
\r
43 set VS_2015_CLANGC2_ARCH=%VS_2015_TOOLCHAIN_ARCH%
\r
44 set VS_2017_VCVARS_ARCH=vcvars64.bat
\r
45 set VS_2017_CLANGC2_ARCH=HostX64
\r
47 :: 32-bit AOT toolchains for MSVC build mono-sgen.exe is currently not supported.
\r
48 :: set VS_2015_TOOLCHAIN_ARCH=x86
\r
49 :: set VS_2015_VCVARS_ARCH=vcvars32.bat
\r
50 :: set VS_2015_CLANGC2_ARCH=%VS_2015_TOOLCHAIN_ARCH%
\r
51 :: set VS_2017_VCVARS_ARCH=vcvars32.bat
\r
52 :: set VS_2017_CLANGC2_ARCH=HostX86
\r
54 set MONO_AS_AOT_COMPILER=0
\r
55 set VS_CLANGC2_TOOLS_BIN_PATH=
\r
57 :: Optimization, check if we need to setup full build environment, only needed when running mono-sgen.exe as AOT compiler.
\r
58 echo.%* | findstr /c:"--aot=" > nul && (
\r
59 set MONO_AS_AOT_COMPILER=1
\r
62 if %MONO_AS_AOT_COMPILER% == 1 (
\r
66 :: mono-sgen.exe not invoked as a AOT compiler, no need to setup full build environment.
\r
69 :: Try setting up VS MSVC build environment.
\r
72 :: Optimization, check if we have something that looks like a MSVC build environment already available.
\r
73 if /i not "%VCINSTALLDIR%" == "" (
\r
74 if /i not "%INCLUDE%" == "" (
\r
75 if /i not "%LIB%" == "" (
\r
81 :: Visual Studio 2015 == 14.0
\r
82 if "%VisualStudioVersion%" == "14.0" (
\r
86 :: Visual Studio 2017 == 15.0
\r
87 if "%VisualStudioVersion%" == "15.0" (
\r
93 set VS_2015_VCINSTALL_DIR=%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\VC\
\r
95 :: Try to locate installed VS2015 Clang/C2.
\r
96 SET VS_2015_CLANGC2_TOOLS_BIN_PATH=%VS_2015_VCINSTALL_DIR%ClangC2\bin\%VS_2015_CLANGC2_ARCH%\
\r
97 SET VS_2015_CLANGC2_TOOLS_BIN=%VS_2015_CLANGC2_TOOLS_BIN_PATH%clang.exe
\r
99 if not exist "%VS_2015_CLANGC2_TOOLS_BIN%" (
\r
103 :SETUP_VS_2015_BUILD_TOOLS
\r
105 :: Try to locate VS2015 build tools installation.
\r
106 set VS_2015_BUILD_TOOLS_INSTALL_DIR=%ProgramFiles(x86)%\Microsoft Visual C++ Build Tools\
\r
107 set VS_2015_BUILD_TOOLS_CMD=%VS_2015_BUILD_TOOLS_INSTALL_DIR%vcbuildtools.bat
\r
109 :: Setup VS2015 VC development environment using build tools installation.
\r
110 call :setup_build_env "%VS_2015_BUILD_TOOLS_CMD%" "%VS_2015_TOOLCHAIN_ARCH%" "%CALLER_WD%" && (
\r
111 set "VS_CLANGC2_TOOLS_BIN_PATH=%VS_2015_CLANGC2_TOOLS_BIN_PATH%"
\r
117 :: Try to locate installed VS2015 VC environment.
\r
118 set VS_2015_DEV_CMD=%VS_2015_VCINSTALL_DIR%bin\%VS_2015_VCVARS_ARCH%
\r
120 call :setup_build_env "%VS_2015_DEV_CMD%" "" "%CALLER_WD%" && (
\r
121 set "VS_CLANGC2_TOOLS_BIN_PATH=%VS_2015_CLANGC2_TOOLS_BIN_PATH%"
\r
127 :: VS2017 includes vswhere.exe that can be used to locate current VS2017 installation.
\r
128 set VSWHERE_TOOLS_BIN=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe
\r
129 set VS_2017_VCINSTALL_DIR=
\r
131 :: Try to locate installed VS2017 VC environment.
\r
132 if exist "%VSWHERE_TOOLS_BIN%" (
\r
133 for /f "tokens=*" %%a in ('"%VSWHERE_TOOLS_BIN%" -latest -property installationPath') do (
\r
134 set VS_2017_VCINSTALL_DIR=%%a\VC\
\r
138 :: Try to locate installed VS2017 Clang/C2.
\r
139 SET VS_2017_CLANGC2_VERSION_FILE=%VS_2017_VCINSTALL_DIR%Auxiliary/Build/Microsoft.ClangC2Version.default.txt
\r
140 if not exist "%VS_2017_CLANGC2_VERSION_FILE%" (
\r
144 set /p VS_2017_CLANGC2_VERSION=<"%VS_2017_CLANGC2_VERSION_FILE%"
\r
145 set VS_2017_CLANGC2_TOOLS_BIN_PATH=%VS_2017_VCINSTALL_DIR%Tools\ClangC2\%VS_2017_CLANGC2_VERSION%\bin\%VS_2017_CLANGC2_ARCH%\
\r
146 set VS_2017_CLANGC2_TOOLS_BIN=%VS_2017_CLANGC2_TOOLS_BIN_PATH%clang.exe
\r
147 if not exist "%VS_2017_CLANGC2_TOOLS_BIN%" (
\r
151 :SETUP_VS_2017_BUILD_TOOLS
\r
153 :: Try to locate VS2017 build tools installation.
\r
154 set VS_2017_BUILD_TOOLS_INSTALL_DIR=%ProgramFiles(x86)%\Microsoft Visual Studio\2017\BuildTools\
\r
155 set VS_2017_BUILD_TOOLS_CMD=%VS_2017_BUILD_TOOLS_INSTALL_DIR%VC\Auxiliary\Build\%VS_2017_VCVARS_ARCH%
\r
157 :: Setup VS2017 VC development environment using build tools installation.
\r
158 call :setup_build_env "%VS_2017_BUILD_TOOLS_CMD%" "" "%CALLER_WD%" && (
\r
159 set "VS_CLANGC2_TOOLS_BIN_PATH=%VS_2017_CLANGC2_TOOLS_BIN_PATH%"
\r
165 :: Try to locate installed VS2017 VC environment.
\r
166 set VS_2017_DEV_CMD=%VS_2017_VCINSTALL_DIR%Auxiliary\Build\%VS_2017_VCVARS_ARCH%
\r
168 :: Setup VS2017 VC development environment using VS installation.
\r
169 call :setup_build_env "%VS_2017_DEV_CMD%" "" "%CALLER_WD%" && (
\r
170 set "VS_CLANGC2_TOOLS_BIN_PATH=%VS_2017_CLANGC2_TOOLS_BIN_PATH%"
\r
176 echo Warning, failed to setup build environment needed by MSVC build mono-sgen.exe running as an AOT compiler.
\r
177 echo Incomplete build environment can cause AOT compiler build due to missing compiler, linker and platform libraries.
\r
181 :: Add mono.sgen.exe (needed for optional LLVM tooling) and ClangC2 folders to PATH
\r
182 set "PATH=%RUN_MONO_SGEN_MSVC_SCRIPT_PATH%;%VS_CLANGC2_TOOLS_BIN_PATH%;%PATH%"
\r
184 call "%RUN_MONO_SGEN_MSVC_SCRIPT_PATH%mono-sgen.exe" %* && (
\r
185 set EXCEUTE_RESULT=0
\r
187 set EXCEUTE_RESULT=1
\r
188 if not %ERRORLEVEL% == 0 (
\r
189 set EXCEUTE_RESULT=%ERRORLEVEL%
\r
193 exit /b %EXCEUTE_RESULT%
\r
197 :: Check if VS build environment script exists.
\r
198 if not exist "%~1" (
\r
199 goto setup_build_env_error
\r
202 :: Run VS build environment script.
\r
203 call "%~1" %~2 > NUL
\r
205 :: Restore callers working directory in case it has been changed by VS scripts.
\r
208 goto setup_build_env_exit
\r
210 :setup_build_env_error
\r
213 :setup_build_env_exit
\r
216 :restore_default_path
\r
218 :: Restore default PATH.
\r
219 if not "%~2" == "" (
\r
220 if not "%~1" == "" (
\r