[mono-dl] Don't do thread state transitions in mono_dl_fallback_register
[mono-project.git] / msvc / run-msbuild.bat
blob033739f56993ba293cf100762a5295472f67b665
1 :: Set up build environment and run execute msbuild with all supplied arguments.\r
2 \r
3 :: Arguments:\r
4 :: -------------------------------------------------------\r
5 :: %1 Visual Studio target, build|clean, default build\r
6 :: %2 Host CPU architecture, x86_64|i686, default x86_64\r
7 :: %3 Visual Studio configuration, debug|release, default release\r
8 :: %4 Additional arguments passed to msbuild, needs to be quoted if multiple.\r
9 :: -------------------------------------------------------\r
11 @echo off\r
13 setlocal\r
15 set BUILD_RESULT=1\r
17 :: Make sure we can restore current working directory after setting up environment.\r
18 :: Some of the VS scripts can change the current working directory.\r
19 set CALLER_WD=%CD%\r
21 :: Get path for current running script.\r
22 set RUN_MSBUILD_SCRIPT_PATH=%~dp0\r
24 :: If we are running from none Windows shell we will need to restore a clean PATH\r
25 :: before setting up VS MSVC build environment. If not there is a risk we will pick up\r
26 :: for example cygwin binaries when running toolchain commands not explicitly setup by VS MSVC build environment.\r
27 set HKCU_ENV_PATH=\r
28 set HKLM_ENV_PATH=\r
29 if "%SHELL%" == "/bin/bash" (\r
30     for /f "tokens=2,*" %%a in ('%WINDIR%\System32\reg.exe query "HKCU\Environment" /v "Path" ^| %WINDIR%\System32\find.exe /i "REG_"') do (\r
31         SET HKCU_ENV_PATH=%%b\r
32     )\r
33     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
34         SET HKLM_ENV_PATH=%%b\r
35     )\r
36 )\r
38 :: Restore default path, if we are running from none Windows shell.\r
39 if "%SHELL%" == "/bin/bash" (\r
40     call :restore_default_path "%HKCU_ENV_PATH%" "%HKLM_ENV_PATH%"\r
41 )\r
43 :: There is still a scenario where the default path can include cygwin\bin folder. If that's the case\r
44 :: there is still a big risk that build tools will be incorrectly resolved towards cygwin bin folder.\r
45 :: Make sure to adjust path and drop all cygwin paths.\r
46 set NEW_PATH=\r
47 call where /Q "cygpath.exe" && (\r
48     echo Warning, PATH includes cygwin bin folders. This can cause build errors due to incorrectly\r
49     echo located build tools. Build script will drop all cygwin folders from used PATH.\r
50     for %%a in ("%PATH:;=";"%") do (\r
51         if not exist "%%~a\cygpath.exe" (\r
52             call :add_to_new_path "%%~a"\r
53         )\r
54     )\r
55 )\r
57 if not "%NEW_PATH%" == "" (\r
58     set "PATH=%NEW_PATH%"\r
59 )\r
61 :: Configure all known build arguments.\r
62 set VS_BUILD_ARGS=""\r
63 set VS_TARGET=build\r
64 if /i "%~1" == "clean" (\r
65     set VS_TARGET="clean"\r
66 )\r
67 shift\r
69 set VS_PLATFORM=x64\r
70 if /i "%~1" == "i686" (\r
71     set VS_PLATFORM="Win32"\r
72 )\r
73 if /i "%~1" == "win32" (\r
74     set VS_PLATFORM="Win32"\r
75 )\r
76 shift\r
78 set VS_CONFIGURATION=Release\r
79 if /i "%~1" == "debug" (\r
80     set VS_CONFIGURATION="Debug"\r
81 )\r
82 shift\r
84 set "VS_ADDITIONAL_ARGUMENTS=/p:PlatformToolset=v140 /p:MONO_TARGET_GC=sgen"\r
85 if /i not "%~1" == "" (\r
86     set VS_ADDITIONAL_ARGUMENTS=%~1\r
87 )\r
89 :: Visual Studio 2015 == 14.0\r
90 if "%VisualStudioVersion%" == "14.0" (\r
91     goto SETUP_VS_2015\r
92 )\r
94 :: Visual Studio 2017 == 15.0\r
95 if "%VisualStudioVersion%" == "15.0" (\r
96     goto SETUP_VS_2017\r
97 )\r
99 :SETUP_VS_2015\r
101 :SETUP_VS_2015_BUILD_TOOLS\r
103 :: Try to locate VS2015 build tools installation.\r
104 set VS_2015_BUILD_TOOLS_CMD=%ProgramFiles(x86)%\Microsoft Visual C++ Build Tools\vcbuildtools_msbuild.bat\r
106 :: Setup VS2015 VC development environment using build tools installation.\r
107 call :setup_build_env "%VS_2015_BUILD_TOOLS_CMD%" "%CALLER_WD%" && (\r
108     goto ON_BUILD\r
111 :SETUP_VS_2015_VC\r
113 :: Try to locate installed VS2015 VC environment.\r
114 set VS_2015_DEV_CMD=%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\Common7\Tools\VsMSBuildCmd.bat\r
116 :: Setup VS2015 VC development environment using VS installation.\r
117 call :setup_build_env "%VS_2015_DEV_CMD%" "%CALLER_WD%" && (\r
118     goto ON_BUILD\r
121 :SETUP_VS_2017\r
123 :SETUP_VS_2017_BUILD_TOOLS\r
125 :: Try to locate VS2017 build tools installation.\r
126 set VS_2017_BUILD_TOOLS_CMD=%ProgramFiles(x86)%\Microsoft Visual Studio\2017\BuildTools\Common7\Tools\VsMSBuildCmd.bat\r
128 :: Setup VS2017 VC development environment using build tools installation.\r
129 call :setup_build_env "%VS_2017_BUILD_TOOLS_CMD%" "%CALLER_WD%" && (\r
130     goto ON_BUILD\r
133 :SETUP_VS_2017_VC\r
135 :: VS2017 includes vswhere.exe that can be used to locate current VS2017 installation.\r
136 set VSWHERE_TOOLS_BIN=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe\r
137 set VS_2017_DEV_CMD=\r
139 :: Try to locate installed VS2017 VC environment.\r
140 if exist "%VSWHERE_TOOLS_BIN%" (\r
141     for /f "tokens=*" %%a in ('"%VSWHERE_TOOLS_BIN%" -latest -property installationPath') do (\r
142         set VS_2017_DEV_CMD=%%a\Common7\Tools\VsMSBuildCmd.bat\r
143     )\r
146 :: Setup VS2017 VC development environment using VS installation.\r
147 call :setup_build_env "%VS_2017_DEV_CMD%" "%CALLER_WD%" && (\r
148     goto ON_BUILD\r
151 :ON_ENV_ERROR\r
153 echo Warning, failed to setup build environment needed by msbuild.exe.\r
154 echo Incomplete build environment can cause build error's due to missing compiler, linker and platform libraries.\r
156 :ON_BUILD\r
158 set VS_BUILD_ARGS=/p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% %VS_ADDITIONAL_ARGUMENTS% /t:%VS_TARGET%\r
159 call msbuild.exe %VS_BUILD_ARGS% "%RUN_MSBUILD_SCRIPT_PATH%mono.sln" && (\r
160     set BUILD_RESULT=0\r
161 ) || (\r
162     set BUILD_RESULT=1\r
163     if not %ERRORLEVEL% == 0 (\r
164         set BUILD_RESULT=%ERRORLEVEL%\r
165     )\r
168 exit /b %BUILD_RESULT%\r
170 :setup_build_env\r
172 :: Check if VS build environment script exists.\r
173 if not exist "%~1" (\r
174     goto setup_build_env_error\r
177 :: Run VS build environment script.\r
178 call "%~1" > NUL\r
180 :: Restore callers working directory in case it has been changed by VS scripts.\r
181 cd /d "%~2"\r
183 goto setup_build_env_exit\r
185 :setup_build_env_error\r
186 exit /b 1\r
188 :setup_build_env_exit\r
189 goto :EOF\r
191 :restore_default_path\r
193 :: Restore default PATH.\r
194 if not "%~2" == "" (\r
195     if not "%~1" == "" (\r
196         set "PATH=%~2;%~1"\r
197     ) else (\r
198         set "PATH=%~2"\r
199     )\r
202 goto :EOF\r
204 :add_to_new_path\r
206 if "%NEW_PATH%" == "" (\r
207     set "NEW_PATH=%~1"\r
208 ) else (\r
209     SET "NEW_PATH=%NEW_PATH%;%~1"\r
212 goto :EOF\r
214 @echo on\r