Merge pull request #2665 from janvorli/fix-pal-test-time-measurement
[platform/upstream/coreclr.git] / build.cmd
1 @if not defined __echo @echo off
2 setlocal EnableDelayedExpansion EnableExtensions
3
4 set __ThisScriptShort=%0
5 set __ThisScriptFull="%~f0"
6
7 :: Note that the msbuild project files (specifically, dir.proj) will use the following variables, if set:
8 ::      __BuildArch         -- default: x64
9 ::      __BuildType         -- default: Debug
10 ::      __BuildOS           -- default: Windows_NT
11 ::      __ProjectDir        -- default: directory of the dir.props file
12 ::      __SourceDir         -- default: %__ProjectDir%\src\
13 ::      __PackagesDir       -- default: %__ProjectDir%\packages\
14 ::      __RootBinDir        -- default: %__ProjectDir%\bin\
15 ::      __BinDir            -- default: %__RootBinDir%\%__BuildOS%.%__BuildArch.%__BuildType%\
16 ::      __IntermediatesDir
17 ::      __PackagesBinDir    -- default: %__BinDir%\.nuget
18 ::      __TestWorkingDir    -- default: %__RootBinDir%\tests\%__BuildOS%.%__BuildArch.%__BuildType%\
19 ::
20 :: Thus, these variables are not simply internal to this script!
21 ::
22 :: The UseRoslynCompiler variable is used by src\mscorlib\GenerateCompilerResponseFile.targets.
23
24 :: Set the default arguments for build
25 set __BuildArch=x64
26 set __BuildType=Debug
27 set __BuildOS=Windows_NT
28
29 :: Default to highest Visual Studio version available
30 set __VSVersion=vs2015
31
32 if defined VS120COMNTOOLS set __VSVersion=vs2013
33 if defined VS140COMNTOOLS set __VSVersion=vs2015
34
35 :: Define a prefix for most output progress messages that come from this script. That makes
36 :: it easier to see where these are coming from. Note that there is a trailing space here.
37 set __MsgPrefix=BUILD: 
38
39 :: Set the various build properties here so that CMake and MSBuild can pick them up
40 set "__ProjectDir=%~dp0"
41 :: remove trailing slash
42 if %__ProjectDir:~-1%==\ set "__ProjectDir=%__ProjectDir:~0,-1%"
43 set "__ProjectFilesDir=%__ProjectDir%"
44 set "__SourceDir=%__ProjectDir%\src"
45 set "__PackagesDir=%__ProjectDir%\packages"
46 set "__RootBinDir=%__ProjectDir%\bin"
47 set "__LogsDir=%__RootBinDir%\Logs"
48
49 set __CleanBuild=
50 set __MscorlibOnly=
51 set __ConfigureOnly=
52 set __SkipConfigure=
53 set __SkipMscorlibBuild=
54 set __SkipNativeBuild=
55 set __SkipTestBuild=
56 set __DoCrossgen=
57 set __BuildSequential=
58 set __msbuildCleanBuildArgs=
59 set __msbuildExtraArgs=
60
61 set __BuildAll=
62
63 set __BuildArchX64=0
64 set __BuildArchX86=0
65 set __BuildArchArm=0
66 set __BuildArchArm64=0
67
68 set __BuildTypeDebug=0
69 set __BuildTypeChecked=0
70 set __BuildTypeRelease=0
71
72 REM __PassThroughArgs is a set of things that will be passed through to nested calls to build.cmd
73 REM when using "all".
74 set __PassThroughArgs=
75
76 :Arg_Loop
77 if "%1" == "" goto ArgsDone
78
79 if /i "%1" == "/?"    goto Usage
80 if /i "%1" == "-?"    goto Usage
81 if /i "%1" == "/h"    goto Usage
82 if /i "%1" == "-h"    goto Usage
83 if /i "%1" == "/help" goto Usage
84 if /i "%1" == "-help" goto Usage
85
86 if /i "%1" == "all"                 (set __BuildAll=1&shift&goto Arg_Loop)
87
88 if /i "%1" == "x64"                 (set __BuildArchX64=1&shift&goto Arg_Loop)
89 if /i "%1" == "x86"                 (set __BuildArchX86=1&shift&goto Arg_Loop)
90 if /i "%1" == "arm"                 (set __BuildArchArm=1&shift&goto Arg_Loop)
91 if /i "%1" == "arm64"               (set __BuildArchArm64=1&shift&goto Arg_Loop)
92
93 if /i "%1" == "debug"               (set __BuildTypeDebug=1&shift&goto Arg_Loop)
94 if /i "%1" == "checked"             (set __BuildTypeChecked=1&shift&goto Arg_Loop)
95 if /i "%1" == "release"             (set __BuildTypeRelease=1&shift&goto Arg_Loop)
96
97 REM All arguments after this point will be passed through directly to build.cmd on nested invocations
98 REM using the "all" argument, and must be added to the __PassThroughArgs variable.
99 set __PassThroughArgs=%__PassThroughArgs% %1
100
101 if /i "%1" == "clean"               (set __CleanBuild=1&shift&goto Arg_Loop)
102
103 if /i "%1" == "freebsdmscorlib"     (set __MscorlibOnly=1&set __BuildOS=FreeBSD&shift&goto Arg_Loop)
104 if /i "%1" == "linuxmscorlib"       (set __MscorlibOnly=1&set __BuildOS=Linux&shift&goto Arg_Loop)
105 if /i "%1" == "osxmscorlib"         (set __MscorlibOnly=1&set __BuildOS=OSX&shift&goto Arg_Loop)
106 if /i "%1" == "windowsmscorlib"     (set __MscorlibOnly=1&set __BuildOS=Windows_NT&shift&goto Arg_Loop)
107
108 if /i "%1" == "vs2013"              (set __VSVersion=%1&shift&goto Arg_Loop)
109 if /i "%1" == "vs2015"              (set __VSVersion=%1&shift&goto Arg_Loop)
110 if /i "%1" == "configureonly"       (set __ConfigureOnly=1&set __SkipMscorlibBuild=1&set __SkipTestBuild=1&shift&goto Arg_Loop)
111 if /i "%1" == "skipconfigure"       (set __SkipConfigure=1&shift&goto Arg_Loop)
112 if /i "%1" == "skipmscorlib"        (set __SkipMscorlibBuild=1&shift&goto Arg_Loop)
113 if /i "%1" == "skipnative"          (set __SkipNativeBuild=1&shift&goto Arg_Loop)
114 if /i "%1" == "skiptests"           (set __SkipTestBuild=1&shift&goto Arg_Loop)
115 if /i "%1" == "docrossgen"          (set __DoCrossgen=1&shift&goto Arg_Loop)
116 if /i "%1" == "sequential"          (set __BuildSequential=1&shift&goto Arg_Loop)
117 if /i "%1" == "priority"            (set __TestPriority=%2&set __PassThroughArgs=%__PassThroughArgs% %2&shift&shift&goto Arg_Loop)
118
119 @REM For backwards compatibility, continue accepting "skiptestbuild", which was the original name of the option.
120 if /i "%1" == "skiptestbuild"       (set __SkipTestBuild=1&shift&goto Arg_Loop)
121
122 @REM It was initially /toolset_dir. Not sure why, since it doesn't match the other usage.
123 if /i "%1" == "/toolset_dir"        (set __ToolsetDir=%2&set __PassThroughArgs=%__PassThroughArgs% %2&shift&shift&goto Arg_Loop)
124 if /i "%1" == "toolset_dir"         (set __ToolsetDir=%2&set __PassThroughArgs=%__PassThroughArgs% %2&shift&shift&goto Arg_Loop)
125
126 if /i not "%1" == "msbuildargs" goto SkipMsbuildArgs
127 :: All the rest of the args will be collected and passed directly to msbuild.
128 :CollectMsbuildArgs
129 shift
130 if "%1"=="" goto ArgsDone
131 set __msbuildExtraArgs=%__msbuildExtraArgs% %1
132 set __PassThroughArgs=%__PassThroughArgs% %1
133 goto CollectMsbuildArgs
134 :SkipMsbuildArgs
135
136 echo Invalid command-line argument: %1
137 goto Usage
138
139 :ArgsDone
140
141 if defined __ConfigureOnly if defined __SkipConfigure (
142     echo "Error: option 'configureonly' is incompatible with 'skipconfigure'
143     goto Usage
144 )
145
146 if defined __SkipMscorlibBuild if defined __MscorlibOnly (
147     echo Error: option 'skipmscorlib' is incompatible with 'freebsdmscorlib', 'linuxmscorlib', 'osxmscorlib', and 'windowsmscorlib'.
148     goto Usage
149 )
150
151 if defined __BuildAll goto BuildAll
152
153 set /A __TotalSpecifiedBuildArch=__BuildArchX64 + __BuildArchX86 + __BuildArchArm + __BuildArchArm64
154 if %__TotalSpecifiedBuildArch% GTR 1 (
155     echo Error: more than one build architecture specified, but "all" not specified.
156     goto Usage
157 )
158
159 if %__BuildArchX64%==1      set __BuildArch=x64
160 if %__BuildArchX86%==1      set __BuildArch=x86
161 if %__BuildArchArm%==1      set __BuildArch=arm
162 if %__BuildArchArm64%==1    set __BuildArch=arm64
163
164 set /A __TotalSpecifiedBuildType=__BuildTypeDebug + __BuildTypeChecked + __BuildTypeRelease
165 if %__TotalSpecifiedBuildType% GTR 1 (
166     echo Error: more than one build type specified, but "all" not specified.
167     goto Usage
168 )
169
170 if %__BuildTypeDebug%==1    set __BuildType=Debug
171 if %__BuildTypeChecked%==1  set __BuildType=Checked
172 if %__BuildTypeRelease%==1  set __BuildType=Release
173
174 echo %__MsgPrefix%Commencing CoreCLR Repo build
175
176 :: Set the remaining variables based upon the determined build configuration
177 set "__BinDir=%__RootBinDir%\Product\%__BuildOS%.%__BuildArch%.%__BuildType%"
178 set "__IntermediatesDir=%__RootBinDir%\obj\%__BuildOS%.%__BuildArch%.%__BuildType%"
179 set "__PackagesBinDir=%__BinDir%\.nuget"
180 set "__TestBinDir=%__RootBinDir%\tests\%__BuildOS%.%__BuildArch%.%__BuildType%"
181 set "__TestIntermediatesDir=%__RootBinDir%\tests\obj\%__BuildOS%.%__BuildArch%.%__BuildType%"
182
183 :: Use this variable to locate dynamically generated files; the actual location though will be different.
184 set "__GeneratedIntermediatesDir=%__IntermediatesDir%\Generated_latest"
185
186 :: Generate path to be set for CMAKE_INSTALL_PREFIX to contain forward slash
187 set "__CMakeBinDir=%__BinDir%"
188 set "__CMakeBinDir=%__CMakeBinDir:\=/%"
189
190 :: Configure environment if we are doing a clean build.
191 if not defined __CleanBuild goto SkipCleanBuild
192 echo %__MsgPrefix%Doing a clean build
193
194 :: MSBuild projects would need a rebuild
195 set __msbuildCleanBuildArgs=/t:rebuild
196
197 :: Cleanup the previous output for the selected configuration
198 if exist "%__BinDir%"               rd /s /q "%__BinDir%"
199 if exist "%__IntermediatesDir%"     rd /s /q "%__IntermediatesDir%"
200 if exist "%__TestBinDir%"           rd /s /q "%__TestBinDir%"
201 if exist "%__TestIntermediatesDir%" rd /s /q "%__TestIntermediatesDir%"
202 if exist "%__LogsDir%"              del /f /q "%__LogsDir%\*_%__BuildOS%__%__BuildArch%__%__BuildType%.*"
203
204 :SkipCleanBuild
205
206 if not exist "%__BinDir%"           md "%__BinDir%"
207 if not exist "%__IntermediatesDir%" md "%__IntermediatesDir%"
208 if not exist "%__LogsDir%"          md "%__LogsDir%"
209
210 :: CMake isn't a requirement when building mscorlib only
211 if defined __MscorlibOnly goto CheckVS
212
213 echo %__MsgPrefix%Checking prerequisites
214
215 :: Eval the output from probe-win1.ps1
216 for /f "delims=" %%a in ('powershell -NoProfile -ExecutionPolicy RemoteSigned "& ""%__SourceDir%\pal\tools\probe-win.ps1"""') do %%a
217
218 :CheckVS
219
220 set __VSProductVersion=
221 if /i "%__VSVersion%" == "vs2013" set __VSProductVersion=120
222 if /i "%__VSVersion%" == "vs2015" set __VSProductVersion=140
223
224 :: Check presence of VS
225 if not defined VS%__VSProductVersion%COMNTOOLS goto NoVS
226
227 set __VSToolsRoot=!VS%__VSProductVersion%COMNTOOLS!
228 if %__VSToolsRoot:~-1%==\ set "__VSToolsRoot=%__VSToolsRoot:~0,-1%"
229
230 :: Does VS really exist?
231 if not exist "%__VSToolsRoot%\..\IDE\devenv.exe"      goto NoVS
232 if not exist "%__VSToolsRoot%\..\..\VC\vcvarsall.bat" goto NoVS
233 if not exist "%__VSToolsRoot%\VsDevCmd.bat"           goto NoVS
234
235 if /i "%__VSVersion%" =="vs2015" goto MSBuild14
236 set _msbuildexe="%ProgramFiles(x86)%\MSBuild\12.0\Bin\MSBuild.exe"
237 if not exist %_msbuildexe% set _msbuildexe="%ProgramFiles%\MSBuild\12.0\Bin\MSBuild.exe"
238 if not exist %_msbuildexe% set _msbuildexe="%ProgramFiles(x86)%\MSBuild\14.0\Bin\MSBuild.exe"
239 goto :CheckMSBuild14
240 :MSBuild14
241 set _msbuildexe="%ProgramFiles(x86)%\MSBuild\14.0\Bin\MSBuild.exe"
242 set UseRoslynCompiler=true
243 :CheckMSBuild14
244 if not exist %_msbuildexe% set _msbuildexe="%ProgramFiles%\MSBuild\14.0\Bin\MSBuild.exe"
245 if not exist %_msbuildexe% echo %__MsgPrefix%Error: Could not find MSBuild.exe.  Please see https://github.com/dotnet/coreclr/blob/master/Documentation/project-docs/developer-guide.md for build instructions. && exit /b 1
246
247 :: Note: We've disabled node reuse because it causes file locking issues.
248 ::       The issue is that we extend the build with our own targets which
249 ::       means that that rebuilding cannot successfully delete the task
250 ::       assembly. 
251 set __msbuildCommonArgs=/nologo /nodeReuse:false %__msbuildCleanBuildArgs% %__msbuildExtraArgs%
252
253 if not defined __BuildSequential (
254     set __msbuildCommonArgs=%__msbuildCommonArgs% /maxcpucount
255 )
256
257 REM =========================================================================================
258 REM ===
259 REM === Start the build steps
260 REM ===
261 REM =========================================================================================
262
263 if defined __MscorlibOnly goto PerformMScorlibBuild
264
265 if defined __SkipNativeBuild (
266     echo %__MsgPrefix%Skipping native components build
267     goto SkipNativeBuild
268 )
269
270 echo %__MsgPrefix%Commencing build of native components for %__BuildOS%.%__BuildArch%.%__BuildType%
271
272 REM Use setlocal to restrict environment changes form vcvarsall.bat and more to just this native components build section.
273 setlocal EnableDelayedExpansion EnableExtensions
274
275 if /i not "%__BuildArch%" == "arm64" goto NotArm64Build
276
277 rem arm64 builds currently use private toolset which has not been released yet
278 REM TODO, remove once the toolset is open.
279
280 if /i "%__ToolsetDir%" == "" (
281     echo %__MsgPrefix%Error: A toolset directory is required for the Arm64 Windows build. Use the toolset_dir argument.
282     exit /b 1
283 )
284
285 set PATH=%PATH%;%__ToolsetDir%\cpp\bin
286 set LIB=%__ToolsetDir%\OS\lib;%__ToolsetDir%\cpp\lib
287 set INCLUDE=^
288 %__ToolsetDir%\cpp\inc;^
289 %__ToolsetDir%\OS\inc\Windows;^
290 %__ToolsetDir%\OS\inc\Windows\crt;^
291 %__ToolsetDir%\cpp\inc\vc;^
292 %__ToolsetDir%\OS\inc\win8
293
294 goto GenVSSolution
295
296 :NotArm64Build
297
298 :: Set the environment for the native build
299 set __VCBuildArch=x86_amd64
300 if /i "%__BuildArch%" == "x86" (set __VCBuildArch=x86)
301 echo %__MsgPrefix%Using environment: "%__VSToolsRoot%\..\..\VC\vcvarsall.bat" %__VCBuildArch%
302 call                                 "%__VSToolsRoot%\..\..\VC\vcvarsall.bat" %__VCBuildArch%
303 @if defined __echo @echo on
304
305 if not defined VSINSTALLDIR (
306     echo %__MsgPrefix%Error: VSINSTALLDIR variable not defined.
307     exit /b 1
308 )
309 if not exist "%VSINSTALLDIR%DIA SDK" goto NoDIA
310
311 :GenVSSolution
312
313 if defined __SkipConfigure goto SkipConfigure
314
315 echo %__MsgPrefix%Regenerating the Visual Studio solution
316
317 pushd "%__IntermediatesDir%"
318 call "%__SourceDir%\pal\tools\gen-buildsys-win.bat" "%__ProjectDir%" %__VSVersion% %__BuildArch%
319 @if defined __echo @echo on
320 popd
321
322 :SkipConfigure
323
324 if not exist "%__IntermediatesDir%\install.vcxproj" (
325     echo %__MsgPrefix%Error: failed to generate native component build project!
326     exit /b 1
327 )
328
329 REM =========================================================================================
330 REM ===
331 REM === Build ETW infrastructure
332 REM ===
333 REM =========================================================================================
334
335 echo %__MsgPrefix%Generating ETW files
336
337 if not defined PythonPath (
338     echo %__MsgPrefix%Error: Couldn't find Python.
339     exit /b 1
340 )
341
342 :: Ensure there are no stale files in the Generated Directory
343 if exist "%__GeneratedIntermediatesDir%" rd /s /q "%__GeneratedIntermediatesDir%"
344 md "%__GeneratedIntermediatesDir%"
345 md "%__GeneratedIntermediatesDir%\inc"
346 set "genetw=%__SourceDir%\scripts\genWinEtw.py"
347
348 mc -h "%__GeneratedIntermediatesDir%\inc" -r "%__GeneratedIntermediatesDir%" -b -co -um -p FireEtw "%__SourceDir%\VM\ClrEtwAll.man"
349 IF ERRORLEVEL 1 goto FailedToGenEtwMetadata
350
351 "%PythonPath%"  "%genetw%" --man "%__SourceDir%\VM\ClrEtwAll.man" --exc "%__SourceDir%\VM\ClrEtwAllMeta.lst" --eventheader "%__GeneratedIntermediatesDir%\inc\ClrEtwAll.h" --macroheader "%__GeneratedIntermediatesDir%\inc\clretwallmain.h" --dummy "%__GeneratedIntermediatesDir%\inc\etmdummy.h"
352 IF  ERRORLEVEL 1 goto FailedToGenEtwMetadata
353
354 :: Do not use this variable; it is used below to support incremental build.
355 set "__GeneratedIntermediatesDirPresent=%__IntermediatesDir%\Generated"
356 "%PythonPath%" -c "import sys;sys.path.insert(0,r\"%__SourceDir%\scripts\"); from Utilities import *;UpdateDirectory(r\"%__GeneratedIntermediatesDirPresent%\",r\"%__GeneratedIntermediatesDir%\")"
357 IF  ERRORLEVEL 1 goto FailedToGenEtwMetadata
358
359 set __GeneratedIntermediatesDir="%__GeneratedIntermediatesDirPresent%"
360 goto DoneGenEtwMetadata
361
362 :FailedToGenEtwMetadata
363 echo %__MsgPrefix%Error: failed to generate ETW files.
364 exit /b %ERRORLEVEL%
365
366 :DoneGenEtwMetadata
367
368 REM =========================================================================================
369 REM ===
370 REM === Build the CLR VM
371 REM ===
372 REM =========================================================================================
373
374 if defined __ConfigureOnly goto SkipNativeBuild
375
376 echo %__MsgPrefix%Invoking msbuild
377
378 set "__BuildLog=%__LogsDir%\CoreCLR_%__BuildOS%__%__BuildArch%__%__BuildType%.log"
379 set "__BuildWrn=%__LogsDir%\CoreCLR_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn"
380 set "__BuildErr=%__LogsDir%\CoreCLR_%__BuildOS%__%__BuildArch%__%__BuildType%.err"
381 set __msbuildLogArgs=^
382 /fileloggerparameters:Verbosity=normal;LogFile="%__BuildLog%" ^
383 /fileloggerparameters1:WarningsOnly;LogFile="%__BuildWrn%" ^
384 /fileloggerparameters2:ErrorsOnly;LogFile="%__BuildErr%" ^
385 /consoleloggerparameters:Summary ^
386 /verbosity:minimal
387
388 set __msbuildArgs="%__IntermediatesDir%\install.vcxproj" %__msbuildCommonArgs% %__msbuildLogArgs% /p:Configuration=%__BuildType%
389
390 if /i "%__BuildArch%" == "arm64" (  
391     REM TODO, remove once we have msbuild support for this platform.
392     set __msbuildArgs=%__msbuildArgs% /p:UseEnv=true
393 ) else (
394     set __msbuildArgs=%__msbuildArgs% /p:Platform=%__BuildArch%
395 )
396
397 %_msbuildexe% %__msbuildArgs%
398 if errorlevel 1 (
399     echo %__MsgPrefix%Error: native component build failed. Refer to the build log files for details:
400     echo     %__BuildLog%
401     echo     %__BuildWrn%
402     echo     %__BuildErr%
403     exit /b 1
404 )
405
406 REM endlocal to rid us of environment changes from vcvarsall.bat
407 endlocal
408
409 :SkipNativeBuild
410
411 REM =========================================================================================
412 REM ===
413 REM === Mscorlib build section.
414 REM ===
415 REM =========================================================================================
416
417 :PerformMScorlibBuild
418 if defined __SkipMscorlibBuild (
419     echo %__MsgPrefix%Skipping Mscorlib build
420     goto SkipMscorlibBuild
421 )
422
423 echo %__MsgPrefix%Commencing build of mscorlib for %__BuildOS%.%__BuildArch%.%__BuildType%
424
425 REM setlocal to prepare for vsdevcmd.bat
426 setlocal EnableDelayedExpansion EnableExtensions
427
428 rem Explicitly set Platform causes conflicts in mscorlib project files. Clear it to allow building from VS x64 Native Tools Command Prompt
429 set Platform=
430
431 :: Set the environment for the managed build
432 echo %__MsgPrefix%Using environment: "%__VSToolsRoot%\VsDevCmd.bat"
433 call                                 "%__VSToolsRoot%\VsDevCmd.bat"
434
435 set "__BuildLog=%__LogsDir%\MScorlib_%__BuildOS%__%__BuildArch%__%__BuildType%.log"
436 set "__BuildWrn=%__LogsDir%\MScorlib_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn"
437 set "__BuildErr=%__LogsDir%\MScorlib_%__BuildOS%__%__BuildArch%__%__BuildType%.err"
438 set __msbuildLogArgs=^
439 /fileloggerparameters:Verbosity=normal;LogFile="%__BuildLog%" ^
440 /fileloggerparameters1:WarningsOnly;LogFile="%__BuildWrn%" ^
441 /fileloggerparameters2:ErrorsOnly;LogFile="%__BuildErr%" ^
442 /consoleloggerparameters:Summary ^
443 /verbosity:minimal
444
445 set __msbuildArgs="%__ProjectFilesDir%\build.proj" %__msbuildCommonArgs% %__msbuildLogArgs%
446
447 set __BuildNugetPackage=true
448 if defined __MscorlibOnly       set __BuildNugetPackage=false
449 if /i "%__BuildArch%" =="arm64" set __BuildNugetPackage=false
450 if %__BuildNugetPackage%==false set __msbuildArgs=%__msbuildArgs% /p:BuildNugetPackage=false
451
452 %_msbuildexe% %__msbuildArgs%
453 if errorlevel 1 (
454     echo %__MsgPrefix%Error: MScorlib build failed. Refer to the build log files for details:
455     echo     %__BuildLog%
456     echo     %__BuildWrn%
457     echo     %__BuildErr%
458     exit /b 1
459 )
460
461 if defined __MscorlibOnly (
462     echo %__MsgPrefix%Mscorlib successfully built.
463     exit /b 0
464 )
465
466 REM Consider doing crossgen build of mscorlib.
467
468 if /i "%__BuildArch%" == "x86" (
469     if not defined __DoCrossgen (
470         echo %__MsgPrefix%Skipping Crossgen
471         goto SkipCrossGenBuild
472     )
473 )
474
475 if /i "%__BuildArch%" == "arm64" (
476     if not defined __DoCrossgen (
477         echo %__MsgPrefix%Skipping Crossgen
478         goto SkipCrossGenBuild
479     )
480 )
481
482 echo %__MsgPrefix%Generating native image of mscorlib for %__BuildOS%.%__BuildArch%.%__BuildType%
483
484 set "__CrossGenMScorlibLog=%__LogsDir%\CrossgenMScorlib_%__BuildOS%__%__BuildArch%__%__BuildType%.log"
485 "%__BinDir%\crossgen.exe" "%__BinDir%\mscorlib.dll" > "%__CrossGenMScorlibLog%" 2>&1
486 if errorlevel 1 (
487     echo %__MsgPrefix%Error: CrossGen mscorlib build failed. Refer to the build log file for details:
488     echo     %__CrossGenMScorlibLog%
489     exit /b 1
490 )
491
492 :SkipCrossGenBuild
493
494 REM endlocal to rid us of environment changes from vsdevenv.bat
495 endlocal
496
497 :SkipMscorlibBuild
498
499 REM =========================================================================================
500 REM ===
501 REM === Test build section
502 REM ===
503 REM =========================================================================================
504
505 if defined __SkipTestBuild (
506     echo %__MsgPrefix%Skipping test build
507     goto SkipTestBuild
508 )
509
510 echo %__MsgPrefix%Commencing build of tests for %__BuildOS%.%__BuildArch%.%__BuildType%
511
512 REM Construct the arguments to pass to the test build script.
513
514 set __BuildtestArgs=%__BuildArch% %__BuildType% %__VSVersion%
515
516 if defined __CleanBuild (
517     set "__BuildtestArgs=%__BuildtestArgs% clean"
518 )
519
520 if defined __BuildSequential (
521     set "__BuildtestArgs=%__BuildtestArgs% sequential"
522 )
523
524 if defined __TestPriority (
525     set "__BuildtestArgs=%__BuildtestArgs% Priority %__TestPriority%"
526 )
527
528 call %__ProjectDir%\tests\buildtest.cmd %__BuildtestArgs%
529
530 if errorlevel 1 (
531     REM buildtest.cmd has already emitted an error message and mentioned the build log file to examine.
532     exit /b 1
533 )
534
535 :SkipTestBuild
536
537 REM =========================================================================================
538 REM ===
539 REM === All builds complete!
540 REM ===
541 REM =========================================================================================
542
543 echo %__MsgPrefix%Repo successfully built.
544 echo %__MsgPrefix%Product binaries are available at !__BinDir!
545 if not defined __SkipTestBuild (
546     echo %__MsgPrefix%Test binaries are available at !__TestBinDir!
547 )
548 exit /b 0
549
550 REM =========================================================================================
551 REM ===
552 REM === Handle the "all" case.
553 REM ===
554 REM =========================================================================================
555
556 :BuildAll
557
558 set __BuildArchList=
559
560 set /A __TotalSpecifiedBuildArch=__BuildArchX64 + __BuildArchX86 + __BuildArchArm + __BuildArchArm64
561 if %__TotalSpecifiedBuildArch% EQU 0 (
562     REM Nothing specified means we want to build all architectures.
563     set __BuildArchList=x64 x86 arm arm64
564 )
565
566 REM Otherwise, add all the specified architectures to the list.
567
568 if %__BuildArchX64%==1      set __BuildArchList=%__BuildArchList% x64
569 if %__BuildArchX86%==1      set __BuildArchList=%__BuildArchList% x86
570 if %__BuildArchArm%==1      set __BuildArchList=%__BuildArchList% arm
571 if %__BuildArchArm64%==1    set __BuildArchList=%__BuildArchList% arm64
572
573 set __BuildTypeList=
574
575 set /A __TotalSpecifiedBuildType=__BuildTypeDebug + __BuildTypeChecked + __BuildTypeRelease
576 if %__TotalSpecifiedBuildType% EQU 0 (
577     REM Nothing specified means we want to build all build types.
578     set __BuildTypeList=Debug Checked Release
579 )
580
581 if %__BuildTypeDebug%==1    set __BuildTypeList=%__BuildTypeList% Debug
582 if %__BuildTypeChecked%==1  set __BuildTypeList=%__BuildTypeList% Checked
583 if %__BuildTypeRelease%==1  set __BuildTypeList=%__BuildTypeList% Release
584
585 REM Create a temporary file to collect build results. We always build all flavors specified, and
586 REM report a summary of the results at the end.
587
588 set __AllBuildSuccess=true
589 set __BuildResultFile=%TEMP%\build-all-summary-%RANDOM%.txt
590 if exist %__BuildResultFile% del /f /q %__BuildResultFile%
591
592 for %%i in (%__BuildArchList%) do (
593     for %%j in (%__BuildTypeList%) do (
594         call :BuildOne %%i %%j
595     )
596 )
597
598 if %__AllBuildSuccess%==true (
599     echo %__MsgPrefix%All builds succeeded!
600     exit /b 0
601 ) else (
602     echo %__MsgPrefix%Builds failed:
603     type %__BuildResultFile%
604     del /f /q %__BuildResultFile%
605     exit /b 1
606 )
607
608 REM This code is unreachable, but leaving it nonetheless, just in case things change.
609 exit /b 99
610
611 :BuildOne
612 set __BuildArch=%1
613 set __BuildType=%2
614 set __NextCmd=call %__ThisScriptFull% %__BuildArch% %__BuildType% %__PassThroughArgs%
615 echo %__MsgPrefix%Invoking: %__NextCmd%
616 %__NextCmd%
617 if errorlevel 1 (
618     echo %__MsgPrefix%    %__BuildArch% %__BuildType% %__PassThroughArgs% >> %__BuildResultFile%
619     set __AllBuildSuccess=false
620 )
621 exit /b 0
622
623 REM =========================================================================================
624 REM ===
625 REM === Helper routines
626 REM ===
627 REM =========================================================================================
628
629 :Usage
630 echo.
631 echo Build the CoreCLR repo.
632 echo.
633 echo Usage:
634 echo     %__ThisScriptShort% [option1] [option2] ...
635 echo or:
636 echo     %__ThisScriptShort% all [option1] [option2] ...
637 echo.
638 echo All arguments are optional. The options are:
639 echo.
640 echo./? -? /h -h /help -help: view this message.
641 echo Build architecture: one of x64, x86, arm, arm64 ^(default: x64^).
642 echo Build type: one of Debug, Checked, Release ^(default: Debug^).
643 echo Visual Studio version: one of VS2013 or VS2015 to force using a particular
644 echo     Visual Studio version ^(default: VS2015^).
645 echo clean: force a clean build ^(default is to perform an incremental build^).
646 echo docrossgen: do a crossgen build of mscorlib.
647 echo msbuildargs ... : all arguments following this tag will be passed directly to msbuild.
648 echo mscorlib version: one of freebsdmscorlib, linuxmscorlib, osxmscorlib,
649 echo     or windowsmscorlib. If one of these is passed, only mscorlib is built,
650 echo     for the specified platform ^(FreeBSD, Linux, OS X, or Windows,
651 echo     respectively^).
652 echo priority ^<N^> : specify a set of test that will be built and run, with priority N.
653 echo sequential: force a non-parallel build ^(default is to build in parallel
654 echo     using all processors^).
655 echo configureonly: skip all builds; only run CMake ^(default: CMake and builds are run^)
656 echo skipconfigure: skip CMake ^(default: CMake is run^)
657 echo skipmscorlib: skip building mscorlib ^(default: mscorlib is built^).
658 echo skipnative: skip building native components ^(default: native components are built^).
659 echo skiptests: skip building tests ^(default: tests are built^).
660 echo toolset_dir ^<dir^> : set the toolset directory -- Arm64 use only. Required for Arm64 builds.
661 echo.
662 echo If "all" is specified, then all build architectures and types are built. If, in addition,
663 echo one or more build architectures or types is specified, then only those build architectures
664 echo and types are built.
665 echo.
666 echo For example:
667 echo     build all
668 echo        -- builds all architectures, and all build types per architecture
669 echo     build all x86
670 echo        -- builds all build types for x86
671 echo     build all x64 x86 Checked Release
672 echo        -- builds x64 and x86 architectures, Checked and Release build types for each
673 exit /b 1
674
675 :NoVS
676 echo Visual Studio 2013+ ^(Community is free^) is a prerequisite to build this repository.
677 echo See: https://github.com/dotnet/coreclr/blob/master/Documentation/project-docs/developer-guide.md#prerequisites
678 exit /b 1
679
680 :NoDIA
681 echo Error: DIA SDK is missing at "%VSINSTALLDIR%DIA SDK". ^
682 This is due to a bug in the Visual Studio installer. It does not install DIA SDK at "%VSINSTALLDIR%" but rather ^
683 at the install location of previous Visual Studio version. The workaround is to copy the DIA SDK folder from the Visual Studio install location ^
684 of the previous version to "%VSINSTALLDIR%" and then build.
685 :: DIA SDK not included in Express editions
686 echo Visual Studio 2013 Express does not include the DIA SDK. ^
687 You need Visual Studio 2013+ (Community is free).
688 echo See: https://github.com/dotnet/coreclr/blob/master/Documentation/project-docs/developer-guide.md#prerequisites
689 exit /b 1