Merge pull request #12011 from pgavlin/FixTestNugetRuntimeId
[platform/upstream/coreclr.git] / build.cmd
1 @if not defined _echo @echo off
2 setlocal EnableDelayedExpansion EnableExtensions
3
4 echo Starting Build at %TIME%
5 set __ThisScriptFull="%~f0"
6
7 :: Default to highest Visual Studio version available
8 ::
9 :: For VS2015 (and prior), only a single instance is allowed to be installed on a box
10 :: and VS140COMNTOOLS is set as a global environment variable by the installer. This
11 :: allows users to locate where the instance of VS2015 is installed.
12 ::
13 :: For VS2017, multiple instances can be installed on the same box SxS and VS150COMNTOOLS
14 :: is no longer set as a global environment variable and is instead only set if the user
15 :: has launched the VS2017 Developer Command Prompt.
16 ::
17 :: Following this logic, we will default to the VS2017 toolset if VS150COMNTOOLS tools is
18 :: set, as this indicates the user is running from the VS2017 Developer Command Prompt and
19 :: is already configured to use that toolset. Otherwise, we will fallback to using the VS2015
20 :: toolset if it is installed. Finally, we will fail the script if no supported VS instance
21 :: can be found.
22 if defined VS150COMNTOOLS (
23   set "__VSToolsRoot=%VS150COMNTOOLS%"
24   set "__VCToolsRoot=%VS150COMNTOOLS%\..\..\VC\Auxiliary\Build"
25   set __VSVersion=vs2017
26 ) else (
27   set "__VSToolsRoot=%VS140COMNTOOLS%"
28   set "__VCToolsRoot=%VS140COMNTOOLS%\..\..\VC"
29   set __VSVersion=vs2015
30 )
31
32 :: Note that the msbuild project files (specifically, dir.proj) will use the following variables, if set:
33 ::      __BuildArch         -- default: x64
34 ::      __BuildType         -- default: Debug
35 ::      __BuildOS           -- default: Windows_NT
36 ::      __ProjectDir        -- default: directory of the dir.props file
37 ::      __SourceDir         -- default: %__ProjectDir%\src\
38 ::      __PackagesDir       -- default: %__ProjectDir%\packages\
39 ::      __RootBinDir        -- default: %__ProjectDir%\bin\
40 ::      __BinDir            -- default: %__RootBinDir%\%__BuildOS%.%__BuildArch.%__BuildType%\
41 ::      __IntermediatesDir
42 ::      __PackagesBinDir    -- default: %__BinDir%\.nuget
43 ::      __TestWorkingDir    -- default: %__RootBinDir%\tests\%__BuildOS%.%__BuildArch.%__BuildType%\
44 ::
45 :: Thus, these variables are not simply internal to this script!
46
47 :: Set the default arguments for build
48 set __BuildArch=x64
49 set __BuildType=Debug
50 set __BuildOS=Windows_NT
51
52 :: Define a prefix for most output progress messages that come from this script. That makes
53 :: it easier to see where these are coming from. Note that there is a trailing space here.
54 set "__MsgPrefix=BUILD: "
55
56 :: Set the various build properties here so that CMake and MSBuild can pick them up
57 set "__ProjectDir=%~dp0"
58 :: remove trailing slash
59 if %__ProjectDir:~-1%==\ set "__ProjectDir=%__ProjectDir:~0,-1%"
60 set "__ProjectFilesDir=%__ProjectDir%"
61 set "__SourceDir=%__ProjectDir%\src"
62 set "__PackagesDir=%__ProjectDir%\packages"
63 set "__RootBinDir=%__ProjectDir%\bin"
64 set "__LogsDir=%__RootBinDir%\Logs"
65 set "__PgoOptDataVersion="
66 set "__IbcOptDataVersion="
67
68 set __BuildAll=
69
70 set __BuildArchX64=0
71 set __BuildArchX86=0
72 set __BuildArchArm=0
73 set __BuildArchArm64=0
74
75 set __BuildTypeDebug=0
76 set __BuildTypeChecked=0
77 set __BuildTypeRelease=0
78 set __BuildStandaloneGC="-DFEATURE_STANDALONE_GC=0"
79 set __BuildStandaloneGCOnly="-DFEATURE_STANDALONE_GC_ONLY=0"
80
81 set __PgoInstrument=0
82 set __IbcTuning=
83
84 REM __PassThroughArgs is a set of things that will be passed through to nested calls to build.cmd
85 REM when using "all".
86 set __PassThroughArgs=
87
88 REM __UnprocessedBuildArgs are args that we pass to msbuild (e.g. /p:__BuildArch=x64)
89 set "__args= %*"
90 set processedArgs=
91 set __UnprocessedBuildArgs=
92 set __RunArgs=
93
94 set __BuildCoreLib=1
95 set __BuildNative=1
96 set __BuildTests=1
97 set __BuildPackages=1
98 set __BuildNativeCoreLib=1
99 set __RestoreOptData=1
100
101 :Arg_Loop
102 if "%1" == "" goto ArgsDone
103
104 if /i "%1" == "-?"    goto Usage
105 if /i "%1" == "-h"    goto Usage
106 if /i "%1" == "-help" goto Usage
107
108 if /i "%1" == "all"                 (set __BuildAll=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
109 if /i "%1" == "x64"                 (set __BuildArchX64=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
110 if /i "%1" == "x86"                 (set __BuildArchX86=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
111 if /i "%1" == "arm"                 (set __BuildArchArm=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
112 if /i "%1" == "arm64"               (set __BuildArchArm64=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
113
114 if /i "%1" == "debug"               (set __BuildTypeDebug=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
115 if /i "%1" == "checked"             (set __BuildTypeChecked=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
116 if /i "%1" == "release"             (set __BuildTypeRelease=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
117
118 REM All arguments after this point will be passed through directly to build.cmd on nested invocations
119 REM using the "all" argument, and must be added to the __PassThroughArgs variable.
120 if [!__PassThroughArgs!]==[] (
121     set __PassThroughArgs=%1
122 ) else (
123     set __PassThroughArgs=%__PassThroughArgs% %1
124 )
125
126 if /i "%1" == "freebsdmscorlib"     (set __BuildNativeCoreLib=0&set __BuildNative=0&set __BuildTests=0&set __BuildPackages=0&set __BuildOS=FreeBSD&set __SkipNugetPackage=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
127 if /i "%1" == "linuxmscorlib"       (set __BuildNativeCoreLib=0&set __BuildNative=0&set __BuildTests=0&set __BuildPackages=0&set __BuildOS=Linux&set __SkipNugetPackage=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
128 if /i "%1" == "netbsdmscorlib"      (set __BuildNativeCoreLib=0&set __BuildNative=0&set __BuildTests=0&set __BuildPackages=0&set __BuildOS=NetBSD&set __SkipNugetPackage=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
129 if /i "%1" == "osxmscorlib"         (set __BuildNativeCoreLib=0&set __BuildNative=0&set __BuildTests=0&set __BuildPackages=0&set __BuildOS=OSX&set __SkipNugetPackage=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
130 if /i "%1" == "windowsmscorlib"     (set __BuildNativeCoreLib=0&set __BuildNative=0&set __BuildTests=0&set __BuildPackages=0&set __BuildOS=Windows_NT&set __SkipNugetPackage=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
131 if /i "%1" == "nativemscorlib"      (set __BuildNativeCoreLib=1&set __BuildCoreLib=0&set __BuildNative=0&set __BuildTests=0&set __BuildPackages=0&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
132 if /i "%1" == "configureonly"       (set __ConfigureOnly=1&set __BuildNative=1&set __BuildNativeCoreLib=0&set __BuildCoreLib=0&set __BuildTests=0&set __BuildPackages=0&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
133 if /i "%1" == "skipconfigure"       (set __SkipConfigure=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
134 if /i "%1" == "skipmscorlib"        (set __BuildCoreLib=0&set __BuildNativeCoreLib=0&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
135 if /i "%1" == "skipnative"          (set __BuildNative=0&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
136 if /i "%1" == "skiptests"           (set __BuildTests=0&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
137 if /i "%1" == "skipbuildpackages"   (set __BuildPackages=0&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
138 if /i "%1" == "skiprestoreoptdata"  (set __RestoreOptData=0&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
139 if /i "%1" == "usenmakemakefiles"   (set __NMakeMakefiles=1&set __ConfigureOnly=1&set __BuildNative=1&set __BuildNativeCoreLib=0&set __BuildCoreLib=0&set __BuildTests=0&set __BuildPackages=0&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
140 if /i "%1" == "pgoinstrument"       (set __PgoInstrument=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
141 if /i "%1" == "ibcinstrument"       (set __IbcTuning=/Tuning&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
142 if /i "%1" == "toolset_dir"         (set __ToolsetDir=%2&set __PassThroughArgs=%__PassThroughArgs% %2&set processedArgs=!processedArgs! %1 %2&shift&shift&goto Arg_Loop)
143 if /i "%1" == "buildstandalonegc"   (
144     set __BuildStandaloneGC="-DFEATURE_STANDALONE_GC=1"
145     set __BuildStandaloneGCOnly="-DFEATURE_STANDALONE_GC_ONLY=1"
146     set processedArgs=!processedArgs! %1
147     shift&goto Arg_Loop
148 )
149
150 @REM The following can be deleted once the CI system that passes it is updated to not pass it.
151 if /i "%1" == "altjitcrossgen"      (set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
152
153 if [!processedArgs!]==[] (
154   call set __UnprocessedBuildArgs=!__args!
155 ) else (
156   call set __UnprocessedBuildArgs=%%__args:*!processedArgs!=%%
157 )
158
159 :ArgsDone
160
161 if defined __BuildAll goto BuildAll
162
163 set /A __TotalSpecifiedBuildArch=__BuildArchX64 + __BuildArchX86 + __BuildArchArm + __BuildArchArm64
164 if %__TotalSpecifiedBuildArch% GTR 1 (
165     echo Error: more than one build architecture specified, but "all" not specified.
166     goto Usage
167 )
168
169 if %__BuildArchX64%==1      set __BuildArch=x64
170 if %__BuildArchX86%==1      set __BuildArch=x86
171 if %__BuildArchArm%==1 (
172     set __BuildArch=arm
173     set __CrossArch=x86
174 )
175 if %__BuildArchArm64%==1 (
176     set __BuildArch=arm64
177     set __CrossArch=x64
178 )
179
180 set /A __TotalSpecifiedBuildType=__BuildTypeDebug + __BuildTypeChecked + __BuildTypeRelease
181 if %__TotalSpecifiedBuildType% GTR 1 (
182     echo Error: more than one build type specified, but "all" not specified.
183     goto Usage
184 )
185
186 if %__BuildTypeDebug%==1    set __BuildType=Debug
187 if %__BuildTypeChecked%==1  set __BuildType=Checked
188 if %__BuildTypeRelease%==1  set __BuildType=Release
189
190 set __RunArgs=-BuildOS=%__BuildOS% -BuildType=%__BuildType% -BuildArch=%__BuildArch%
191
192 :: Set the remaining variables based upon the determined build configuration
193 set "__BinDir=%__RootBinDir%\Product\%__BuildOS%.%__BuildArch%.%__BuildType%"
194 set "__IntermediatesDir=%__RootBinDir%\obj\%__BuildOS%.%__BuildArch%.%__BuildType%"
195 if "%__NMakeMakefiles%"=="1" (set "__IntermediatesDir=%__RootBinDir%\nmakeobj\%__BuildOS%.%__BuildArch%.%__BuildType%")
196 set "__PackagesBinDir=%__BinDir%\.nuget"
197 set "__TestRootDir=%__RootBinDir%\tests"
198 set "__TestBinDir=%__TestRootDir%\%__BuildOS%.%__BuildArch%.%__BuildType%"
199 set "__TestIntermediatesDir=%__RootBinDir%\tests\obj\%__BuildOS%.%__BuildArch%.%__BuildType%"
200 set "__CrossComponentBinDir=%__BinDir%"
201 set "__CrossCompIntermediatesDir=%__IntermediatesDir%\crossgen"
202
203 if NOT "%__CrossArch%" == "" set __CrossComponentBinDir=%__CrossComponentBinDir%\%__CrossArch%
204 set "__CrossGenCoreLibLog=%__LogsDir%\CrossgenCoreLib_%__BuildOS%__%__BuildArch%__%__BuildType%.log"
205 set "__CrossgenExe=%__CrossComponentBinDir%\crossgen.exe"
206
207 :: Generate path to be set for CMAKE_INSTALL_PREFIX to contain forward slash
208 set "__CMakeBinDir=%__BinDir%"
209 set "__CMakeBinDir=%__CMakeBinDir:\=/%"
210
211 if not exist "%__BinDir%"           md "%__BinDir%"
212 if not exist "%__IntermediatesDir%" md "%__IntermediatesDir%"
213 if not exist "%__LogsDir%"          md "%__LogsDir%"
214
215 REM It is convinient to have your Nuget search path include the location where the build
216 REM will plass packages.  However nuget used during the build will fail if that directory 
217 REM does not exist.   Avoid this in at least one case by agressively creating the directory. 
218 if not exist "%__BinDir%\.nuget\pkg"           md "%__BinDir%\.nuget\pkg"
219
220 echo %__MsgPrefix%Commencing CoreCLR Repo build
221
222 :: Set the remaining variables based upon the determined build configuration
223
224 echo %__MsgPrefix%Checking prerequisites
225 :: Eval the output from probe-win1.ps1
226 for /f "delims=" %%a in ('powershell -NoProfile -ExecutionPolicy ByPass "& ""%__SourceDir%\pal\tools\probe-win.ps1"""') do %%a
227
228 REM =========================================================================================
229 REM ===
230 REM === Start the build steps
231 REM ===
232 REM =========================================================================================
233
234 echo %__MsgPrefix%Using environment: "%__VSToolsRoot%\VsDevCmd.bat"
235 call                                 "%__VSToolsRoot%\VsDevCmd.bat"
236
237 @call %__ProjectDir%\run.cmd build -Project=%__ProjectDir%\build.proj -generateHeaderWindows -NativeVersionHeaderFile="%__RootBinDir%\obj\_version.h" %__RunArgs% %__UnprocessedBuildArgs% 
238
239 REM =========================================================================================
240 REM ===
241 REM === Restore optimization profile data
242 REM ===
243 REM =========================================================================================
244
245 if %__RestoreOptData% EQU 1 if %__BuildTypeRelease% EQU 1 (
246     echo %__MsgPrefix%Restoring the OptimizationData Package
247     @call %__ProjectDir%\run.cmd sync -optdata
248     if not !errorlevel! == 0 (
249         echo %__MsgPrefix%Error: Failed to restore the optimization data package.
250         exit /b 1
251     )
252 )
253
254 REM Parse the optdata package versions out of msbuild so that we can pass them on to CMake
255 set DotNetCli=%__ProjectDir%\Tools\dotnetcli\dotnet.exe
256 if not exist "%DotNetCli%" (
257     echo Assertion failed: dotnet.exe not found at path "%DotNetCli%"
258     exit /b 1
259 )
260 set OptDataProjectFilePath=%__ProjectDir%\src\.nuget\optdata\optdata.csproj
261 for /f "tokens=*" %%s in ('%DotNetCli% msbuild "%OptDataProjectFilePath%" /t:DumpPgoDataPackageVersion /nologo') do @(
262     set __PgoOptDataVersion=%%s
263 )
264 for /f "tokens=*" %%s in ('%DotNetCli% msbuild "%OptDataProjectFilePath%" /t:DumpIbcDataPackageVersion /nologo') do @(
265     set __IbcOptDataVersion=%%s
266 )
267
268 REM =========================================================================================
269 REM ===
270 REM === Build the CLR VM
271 REM ===
272 REM =========================================================================================
273
274 if %__BuildNative% EQU 1 ( 
275     echo %__MsgPrefix%Commencing build of native components for %__BuildOS%.%__BuildArch%.%__BuildType%
276
277         set nativePlatfromArgs=-platform=%__BuildArch%
278     if /i "%__BuildArch%" == "arm64" ( set nativePlatfromArgs=-useEnv )
279
280     set __MsbuildLog=/flp:Verbosity=normal;LogFile="%__LogsDir%\CoreCLR_%__BuildOS%__%__BuildArch%__%__BuildType%.log"
281     set __MsbuildWrn=/flp1:WarningsOnly;LogFile="%__LogsDir%\CoreCLR_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn"
282     set __MsbuildErr=/flp2:ErrorsOnly;LogFile="%__LogsDir%\CoreCLR_%__BuildOS%__%__BuildArch%__%__BuildType%.err"
283
284     if /i "%__BuildArch%" == "arm64" ( 
285         rem arm64 builds currently use private toolset which has not been released yet
286         REM TODO, remove once the toolset is open.
287         call :PrivateToolSet
288         goto GenVSSolution
289     )
290
291     :: Set the environment for the native build
292     set __VCBuildArch=x86_amd64
293     if /i "%__BuildArch%" == "x86" ( set __VCBuildArch=x86 )
294     if /i "%__BuildArch%" == "arm" (
295         set __VCBuildArch=x86_arm
296         
297         REM Make CMake pick the highest installed version in the 10.0.* range
298         set ___SDKVersion="-DCMAKE_SYSTEM_VERSION=10.0"
299     )
300
301     echo %__MsgPrefix%Using environment: "%__VCToolsRoot%\vcvarsall.bat" !__VCBuildArch!
302     call                                 "%__VCToolsRoot%\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     if defined __SkipConfigure goto SkipConfigure
313
314     echo %__MsgPrefix%Regenerating the Visual Studio solution
315
316     pushd "%__IntermediatesDir%"
317     set __ExtraCmakeArgs=!___SDKVersion! "-DCLR_CMAKE_TARGET_OS=%__BuildOs%" "-DCLR_CMAKE_PACKAGES_DIR=%__PackagesDir%" "-DCLR_CMAKE_PGO_INSTRUMENT=%__PgoInstrument%" "-DCLR_CMAKE_OPTDATA_VERSION=%__PgoOptDataVersion%"
318     call "%__SourceDir%\pal\tools\gen-buildsys-win.bat" "%__ProjectDir%" %__VSVersion% %__BuildArch% %__BuildStandaloneGC% %__BuildStandaloneGCOnly% !__ExtraCmakeArgs!
319         @if defined _echo @echo on
320     popd
321 :SkipConfigure
322     if defined __ConfigureOnly goto SkipNativeBuild
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     @call %__ProjectDir%\run.cmd build -Project=%__IntermediatesDir%\install.vcxproj -MsBuildLog=!__MsbuildLog! -MsBuildWrn=!__MsbuildWrn! -MsBuildErr=!__MsbuildErr! -configuration=%__BuildType% %nativePlatfromArgs% %__RunArgs% %__UnprocessedBuildArgs%
330
331     if not !errorlevel! == 0 (
332         echo %__MsgPrefix%Error: native component build failed. Refer to the build log files for details:
333         echo     "%__LogsDir%\CoreCLR_%__BuildOS%__%__BuildArch%__%__BuildType%.log"
334         echo     "%__LogsDir%\CoreCLR_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn"
335         echo     "%__LogsDir%\CoreCLR_%__BuildOS%__%__BuildArch%__%__BuildType%.err"
336         exit /b 1
337     )   
338 )
339 :SkipNativeBuild
340
341 REM =========================================================================================
342 REM ===
343 REM === Build Cross-Architecture Native Components (if applicable)
344 REM ===
345 REM =========================================================================================
346
347 if /i "%__BuildArch%"=="arm64" (
348     set __DoCrossArchBuild=1
349     )
350
351 if /i "%__BuildArch%"=="arm" (
352     set __DoCrossArchBuild=1
353     )
354
355 if /i "%__DoCrossArchBuild%"=="1" (
356
357     echo %__MsgPrefix%Commencing build of cross architecture native components for %__BuildOS%.%__BuildArch%.%__BuildType%
358
359     :: Set the environment for the native build
360     set __VCBuildArch=x86_amd64
361     if /i "%__CrossArch%" == "x86" ( set __VCBuildArch=x86 )
362     @call "%__VCToolsRoot%\vcvarsall.bat" !__VCBuildArch!
363     @if defined _echo @echo on
364
365     if not exist "%__CrossCompIntermediatesDir%" md "%__CrossCompIntermediatesDir%"
366     if defined __SkipConfigure goto SkipConfigureCrossBuild
367
368     pushd "%__CrossCompIntermediatesDir%"
369     set __CMakeBinDir=%__CrossComponentBinDir%
370     set "__CMakeBinDir=!__CMakeBinDir:\=/!"
371     set __ExtraCmakeArgs="-DCLR_CROSS_COMPONENTS_BUILD=1" "-DCLR_CMAKE_TARGET_ARCH=%__BuildArch%" "-DCLR_CMAKE_TARGET_OS=%__BuildOs%" "-DCLR_CMAKE_PACKAGES_DIR=%__PackagesDir%" "-DCLR_CMAKE_PGO_INSTRUMENT=%__PgoInstrument%" "-DCLR_CMAKE_OPTDATA_VERSION=%__PgoOptDataVersion%"
372     call "%__SourceDir%\pal\tools\gen-buildsys-win.bat" "%__ProjectDir%" %__VSVersion% %__CrossArch% !__ExtraCmakeArgs!
373     @if defined _echo @echo on
374     popd
375 :SkipConfigureCrossBuild
376     if not exist "%__CrossCompIntermediatesDir%\install.vcxproj" (
377         echo %__MsgPrefix%Error: failed to generate cross-arch components build project!
378         exit /b 1
379     )
380
381     if defined __ConfigureOnly goto SkipCrossCompBuild
382
383     echo %__MsgPrefix%Invoking msbuild
384
385     set __MsbuildLog=/flp:Verbosity=normal;LogFile="%__LogsDir%\Cross_%__BuildOS%__%__BuildArch%__%__BuildType%.log"
386     set __MsbuildWrn=/flp1:WarningsOnly;LogFile="%__LogsDir%\Cross_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn"
387     set __MsbuildErr=/flp2:ErrorsOnly;LogFile="%__LogsDir%\Cross_%__BuildOS%__%__BuildArch%__%__BuildType%.err"
388     @call %__ProjectDir%\run.cmd build -Project=%__CrossCompIntermediatesDir%\install.vcxproj -configuration=%__BuildType% -platform=%__CrossArch% -MsBuildLog=!__MsbuildLog! -MsBuildWrn=!__MsbuildWrn! -MsBuildErr=!__MsbuildErr! %__RunArgs% %__UnprocessedBuildArgs%
389     if not !errorlevel! == 0 (
390         echo %__MsgPrefix%Error: cross-arch components build failed. Refer to the build log files for details:
391         echo     "%__LogsDir%\Cross_%__BuildOS%__%__BuildArch%__%__BuildType%.log"
392         echo     "%__LogsDir%\Cross_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn"
393         echo     "%__LogsDir%\Cross_%__BuildOS%__%__BuildArch%__%__BuildType%.err"
394         exit /b 1
395     )    
396 )
397
398 :SkipCrossCompBuild
399
400 REM =========================================================================================
401 REM ===
402 REM === CoreLib and NuGet package build section.
403 REM ===
404 REM =========================================================================================
405
406 if %__BuildCoreLib% EQU 1 (  
407         
408         echo %__MsgPrefix%Commencing build of System.Private.CoreLib for %__BuildOS%.%__BuildArch%.%__BuildType%
409     rem Explicitly set Platform causes conflicts in CoreLib project files. Clear it to allow building from VS x64 Native Tools Command Prompt
410     set Platform=
411
412     set __MsbuildLog=/flp:Verbosity=normal;LogFile="%__LogsDir%\System.Private.CoreLib_%__BuildOS%__%__BuildArch%__%__BuildType%.log"
413     set __MsbuildWrn=/flp1:WarningsOnly;LogFile="%__LogsDir%\System.Private.CoreLib_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn"
414     set __MsbuildErr=/flp2:ErrorsOnly;LogFile="%__LogsDir%\System.Private.CoreLib_%__BuildOS%__%__BuildArch%__%__BuildType%.err"
415
416     set __ExtraBuildArgs=
417     if not defined __IbcTuning (
418       set __ExtraBuildArgs=!__ExtraBuildArgs! -OptimizationDataDir="%__PackagesDir%/optimization.%__BuildOS%-%__BuildArch%.IBC.CoreCLR/%__IbcOptDataVersion%/data/"
419       set __ExtraBuildArgs=!__ExtraBuildArgs! -EnableProfileGuidedOptimization=true
420     )
421
422     if /i "%__BuildArch%" == "arm64" (
423                 set __nugetBuildArgs=-buildNugetPackage=false
424     ) else if "%__SkipNugetPackage%" == "1" (
425                 set __nugetBuildArgs=-buildNugetPackage=false
426     ) else (
427                 set __nugetBuildArgs=-buildNugetPackage=true
428         )
429
430     @call %__ProjectDir%\run.cmd build -Project=%__ProjectDir%\build.proj -MsBuildLog=!__MsbuildLog! -MsBuildWrn=!__MsbuildWrn! -MsBuildErr=!__MsbuildErr! !__nugetBuildArgs! %__RunArgs% !__ExtraBuildArgs! %__UnprocessedBuildArgs%
431     if not !errorlevel! == 0 (
432         echo %__MsgPrefix%Error: System.Private.CoreLib build failed. Refer to the build log files for details:
433         echo     "%__LogsDir%\System.Private.CoreLib_%__BuildOS%__%__BuildArch%__%__BuildType%.log"
434         echo     "%__LogsDir%\System.Private.CoreLib_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn"
435         echo     "%__LogsDir%\System.Private.CoreLib_%__BuildOS%__%__BuildArch%__%__BuildType%.err"
436         exit /b 1
437     )
438 )
439
440 REM Need diasymreader.dll on your path for /CreatePdb
441 set PATH=%PATH%;%WinDir%\Microsoft.Net\Framework64\V4.0.30319;%WinDir%\Microsoft.Net\Framework\V4.0.30319
442
443 if %__BuildNativeCoreLib% EQU 1 (
444     echo %__MsgPrefix%Generating native image of System.Private.CoreLib for %__BuildOS%.%__BuildArch%.%__BuildType%
445
446     echo "%__CrossgenExe%" %__IbcTuning% /Platform_Assemblies_Paths "%__BinDir%"\IL /out "%__BinDir%\System.Private.CoreLib.dll" "%__BinDir%\IL\System.Private.CoreLib.dll"
447     "%__CrossgenExe%" %__IbcTuning% /Platform_Assemblies_Paths "%__BinDir%"\IL /out "%__BinDir%\System.Private.CoreLib.dll" "%__BinDir%\IL\System.Private.CoreLib.dll" > "%__CrossGenCoreLibLog%" 2>&1
448     if NOT !errorlevel! == 0 (
449         echo %__MsgPrefix%Error: CrossGen System.Private.CoreLib build failed. Refer to %__CrossGenCoreLibLog%
450         :: Put it in the same log, helpful for Jenkins
451         type %__CrossGenCoreLibLog%
452         goto CrossgenFailure
453     )
454     "%__CrossgenExe%" /Platform_Assemblies_Paths "%__BinDir%" /CreatePdb "%__BinDir%\PDB" "%__BinDir%\System.Private.CoreLib.dll" >> "%__CrossGenCoreLibLog%" 2>&1
455     if NOT !errorlevel! == 0 (
456         echo %__MsgPrefix%Error: CrossGen /CreatePdb System.Private.CoreLib build failed. Refer to %__CrossGenCoreLibLog%
457         :: Put it in the same log, helpful for Jenkins
458         type %__CrossGenCoreLibLog%
459         goto CrossgenFailure
460     )
461 )
462
463 if %__BuildPackages% EQU 1 (
464     echo %__MsgPrefix%Building Packages for %__BuildOS%.%__BuildArch%.%__BuildType%
465
466     set __MsbuildLog=/flp:Verbosity=normal;LogFile="%__LogsDir%\Nuget_%__BuildOS%__%__BuildArch%__%__BuildType%.log"
467         set __MsbuildWrn=/flp1:WarningsOnly;LogFile="%__LogsDir%\Nuget_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn"
468         set __MsbuildErr=/flp2:ErrorsOnly;LogFile="%__LogsDir%\Nuget_%__BuildOS%__%__BuildArch%__%__BuildType%.err"
469
470     REM The conditions as to what to build are captured in the builds file.
471     @call %__ProjectDir%\run.cmd build -Project=%__SourceDir%\.nuget\packages.builds -platform=%__BuildArch% -MsBuildLog=!__MsbuildLog! -MsBuildWrn=!__MsbuildWrn! -MsBuildErr=!__MsbuildErr! %__RunArgs% %__UnprocessedBuildArgs%
472
473     if not !errorlevel! == 0 (
474         echo %__MsgPrefix%Error: Nuget package generation failed build failed. Refer to the build log files for details:
475         echo     "%__LogsDir%\Nuget_%__BuildOS%__%__BuildArch%__%__BuildType%.log"
476         echo     "%__LogsDir%\Nuget_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn"
477         echo     "%__LogsDir%\Nuget_%__BuildOS%__%__BuildArch%__%__BuildType%.err"
478         exit /b 1
479     )
480 )
481
482 REM =========================================================================================
483 REM ===
484 REM === Test build section
485 REM ===
486 REM =========================================================================================
487
488 if %__BuildTests% EQU 1 (
489     echo %__MsgPrefix%Commencing build of tests for %__BuildOS%.%__BuildArch%.%__BuildType%
490
491     REM Construct the arguments to pass to the test build script.
492
493     rem arm64 builds currently use private toolset which has not been released yet
494     REM TODO, remove once the toolset is open.
495     if /i "%__BuildArch%" == "arm64" call :PrivateToolSet 
496
497     echo "%__ProjectDir%\build-test.cmd %__BuildArch% %__BuildType% %__UnprocessedBuildArgs%"
498     @call %__ProjectDir%\build-test.cmd %__BuildArch% %__BuildType% %__UnprocessedBuildArgs%
499
500     if not !errorlevel! == 0 (
501         REM buildtest.cmd has already emitted an error message and mentioned the build log file to examine.
502         exit /b 1
503     )
504 )
505
506 REM =========================================================================================
507 REM ===
508 REM === All builds complete!
509 REM ===
510 REM =========================================================================================
511
512 echo %__MsgPrefix%Repo successfully built.  Finished at %TIME%
513 echo %__MsgPrefix%Product binaries are available at !__BinDir!
514 if %__BuildTests% EQU 1 (
515     echo %__MsgPrefix%Test binaries are available at !__TestBinDir!
516 )
517 exit /b 0
518
519 REM =========================================================================================
520 REM ===
521 REM === Handle the "all" case.
522 REM ===
523 REM =========================================================================================
524
525 :BuildAll
526
527 set __BuildArchList=
528
529 set /A __TotalSpecifiedBuildArch=__BuildArchX64 + __BuildArchX86 + __BuildArchArm + __BuildArchArm64
530 if %__TotalSpecifiedBuildArch% EQU 0 (
531     REM Nothing specified means we want to build all architectures.
532     set __BuildArchList=x64 x86 arm arm64
533 )
534
535 REM Otherwise, add all the specified architectures to the list.
536
537 if %__BuildArchX64%==1      set __BuildArchList=%__BuildArchList% x64
538 if %__BuildArchX86%==1      set __BuildArchList=%__BuildArchList% x86
539 if %__BuildArchArm%==1      set __BuildArchList=%__BuildArchList% arm
540 if %__BuildArchArm64%==1    set __BuildArchList=%__BuildArchList% arm64
541
542 set __BuildTypeList=
543
544 set /A __TotalSpecifiedBuildType=__BuildTypeDebug + __BuildTypeChecked + __BuildTypeRelease
545 if %__TotalSpecifiedBuildType% EQU 0 (
546     REM Nothing specified means we want to build all build types.
547     set __BuildTypeList=Debug Checked Release
548 )
549
550 if %__BuildTypeDebug%==1    set __BuildTypeList=%__BuildTypeList% Debug
551 if %__BuildTypeChecked%==1  set __BuildTypeList=%__BuildTypeList% Checked
552 if %__BuildTypeRelease%==1  set __BuildTypeList=%__BuildTypeList% Release
553
554 REM Create a temporary file to collect build results. We always build all flavors specified, and
555 REM report a summary of the results at the end.
556
557 set __AllBuildSuccess=true
558 set __BuildResultFile=%TEMP%\build-all-summary-%RANDOM%.txt
559 if exist %__BuildResultFile% del /f /q %__BuildResultFile%
560
561 for %%i in (%__BuildArchList%) do (
562     for %%j in (%__BuildTypeList%) do (
563         call :BuildOne %%i %%j
564     )
565 )
566
567 if %__AllBuildSuccess%==true (
568     echo %__MsgPrefix%All builds succeeded!
569     exit /b 0
570 ) else (
571     echo %__MsgPrefix%Builds failed:
572     type %__BuildResultFile%
573     del /f /q %__BuildResultFile%
574     exit /b 1
575 )
576
577 REM This code is unreachable, but leaving it nonetheless, just in case things change.
578 exit /b 99
579
580 :BuildOne
581 set __BuildArch=%1
582 set __BuildType=%2
583 set __NextCmd=call %__ThisScriptFull% %__BuildArch% %__BuildType% %__PassThroughArgs%
584 echo %__MsgPrefix%Invoking: %__NextCmd%
585 %__NextCmd%
586 if not !errorlevel! == 0 (
587     echo %__MsgPrefix%    %__BuildArch% %__BuildType% %__PassThroughArgs% >> %__BuildResultFile%
588     set __AllBuildSuccess=false
589 )
590 exit /b 0
591
592 REM =========================================================================================
593 REM ===
594 REM === Helper routines
595 REM ===
596 REM =========================================================================================
597
598 :CrossgenFailure
599 exit /b 1
600
601 :Usage
602 echo.
603 echo Build the CoreCLR repo.
604 echo.
605 echo Usage:
606 echo     build.cmd [option1] [option2]
607 echo or:
608 echo     build.cmd all [option1] [option2] -- ...
609 echo.
610 echo All arguments are optional. The options are:
611 echo.
612 echo.-? -h -help: view this message.
613 echo all: Builds all configurations and platforms.
614 echo Build architecture: one of x64, x86, arm, arm64 ^(default: x64^).
615 echo Build type: one of Debug, Checked, Release ^(default: Debug^).
616 echo -- ... : all arguments following this tag will be passed directly to msbuild.
617 echo mscorlib version: one of freebsdmscorlib, linuxmscorlib, netbsdmscorlib, osxmscorlib,
618 echo     or windowsmscorlib. If one of these is passed, only System.Private.CoreLib is built,
619 echo     for the specified platform ^(FreeBSD, Linux, NetBSD, OS X or Windows,
620 echo     respectively^).
621 echo     add nativemscorlib to go further and build the native image for designated mscorlib.
622 echo toolset_dir ^<dir^> : set the toolset directory -- Arm64 use only. Required for Arm64 builds.
623 echo pgoinstrument: generate instrumented code for profile guided optimization enabled binaries.
624 echo ibcinstrument: generate IBC-tuning-enabled native images when invoking crossgen.
625 echo configureonly: skip all builds; only run CMake ^(default: CMake and builds are run^)
626 echo skipconfigure: skip CMake ^(default: CMake is run^)
627 echo skipmscorlib: skip building System.Private.CoreLib ^(default: System.Private.CoreLib is built^).
628 echo skipnative: skip building native components ^(default: native components are built^).
629 echo skiptests: skip building tests ^(default: tests are built^).
630 echo skipbuildpackages: skip building nuget packages ^(default: packages are built^).
631 echo skiprestoreoptdata: skip restoring optimization data used by profile-based optimizations.
632 echo buildstandalonegc: builds the GC in a standalone mode.
633 echo -skiprestore: skip restoring packages ^(default: packages are restored during build^).
634 echo -disableoss: Disable Open Source Signing for System.Private.CoreLib.
635 echo -priority=^<N^> : specify a set of test that will be built and run, with priority N.
636 echo -sequential: force a non-parallel build ^(default is to build in parallel
637 echo     using all processors^).
638 echo -officialbuildid=^<ID^>: specify the official build ID to be used by this build.
639 echo -Rebuild: passes /t:rebuild to the build projects.
640 echo portable : build for portable RID.
641 echo.
642 echo If "all" is specified, then all build architectures and types are built. If, in addition,
643 echo one or more build architectures or types is specified, then only those build architectures
644 echo and types are built.
645 echo.
646 echo For example:
647 echo     build all
648 echo        -- builds all architectures, and all build types per architecture
649 echo     build all x86
650 echo        -- builds all build types for x86
651 echo     build all x64 x86 Checked Release
652 echo        -- builds x64 and x86 architectures, Checked and Release build types for each
653 exit /b 1
654
655 :NoDIA
656 echo Error: DIA SDK is missing at "%VSINSTALLDIR%DIA SDK". ^
657 This is due to a bug in the Visual Studio installer. It does not install DIA SDK at "%VSINSTALLDIR%" but rather ^
658 at the install location of previous Visual Studio version. The workaround is to copy the DIA SDK folder from the Visual Studio install location ^
659 of the previous version to "%VSINSTALLDIR%" and then build.
660 :: DIA SDK not included in Express editions
661 echo Visual Studio Express does not include the DIA SDK. ^
662 You need Visual Studio 2015 or 2017 (Community is free).
663 echo See: https://github.com/dotnet/coreclr/blob/master/Documentation/project-docs/developer-guide.md#prerequisites
664 exit /b 1
665
666 :PrivateToolSet
667
668 echo %__MsgPrefix% Setting Up the usage of __ToolsetDir:%__ToolsetDir%
669
670 if /i "%__ToolsetDir%" == "" (
671     echo %__MsgPrefix%Error: A toolset directory is required for the Arm64 Windows build. Use the toolset_dir argument.
672     exit /b 1
673 )
674
675 if not exist "%__ToolsetDir%"\buildenv_arm64.cmd goto :Not_EWDK
676 call "%__ToolsetDir%"\buildenv_arm64.cmd
677 exit /b 0
678
679 :Not_EWDK
680 set PATH=%__ToolsetDir%\VC_sdk\bin;%PATH%
681 set LIB=%__ToolsetDir%\VC_sdk\lib\arm64;%__ToolsetDir%\sdpublic\sdk\lib\arm64
682 set INCLUDE=^
683 %__ToolsetDir%\VC_sdk\inc;^
684 %__ToolsetDir%\sdpublic\sdk\inc;^
685 %__ToolsetDir%\sdpublic\shared\inc;^
686 %__ToolsetDir%\sdpublic\shared\inc\minwin;^
687 %__ToolsetDir%\sdpublic\sdk\inc\ucrt;^
688 %__ToolsetDir%\sdpublic\sdk\inc\minwin;^
689 %__ToolsetDir%\sdpublic\sdk\inc\mincore;^
690 %__ToolsetDir%\sdpublic\sdk\inc\abi;^
691 %__ToolsetDir%\sdpublic\sdk\inc\clientcore;^
692 %__ToolsetDir%\diasdk\include
693 exit /b 0