c72db96660b62312e964b8d54a97b9744da014ab
[platform/upstream/coreclr.git] / build.cmd
1 @echo off
2 setlocal EnableDelayedExpansion
3
4 :: Set the default arguments for build
5 set __BuildArch=x64
6 set __BuildType=Debug
7 set __BuildOS=Windows_NT
8
9 :: Default to VS2013
10 set __VSVersion=VS2013
11 set __VSProductVersion=120
12
13 :: Set the various build properties here so that CMake and MSBuild can pick them up
14 set "__ProjectDir=%~dp0"
15 :: remove trailing slash
16 if %__ProjectDir:~-1%==\ set "__ProjectDir=%__ProjectDir:~0,-1%"
17 set "__ProjectFilesDir=%__ProjectDir%"
18 set "__SourceDir=%__ProjectDir%\src"
19 set "__PackagesDir=%__ProjectDir%\packages"
20 set "__RootBinDir=%__ProjectDir%\bin"
21 set "__LogsDir=%__RootBinDir%\Logs"
22 set __MSBCleanBuildArgs=
23
24 :Arg_Loop
25 if "%1" == "" goto ArgsDone
26 if /i "%1" == "/?" goto Usage
27 if /i "%1" == "x64"    (set __BuildArch=x64&&shift&goto Arg_Loop)
28
29 if /i "%1" == "debug"    (set __BuildType=Debug&shift&goto Arg_Loop)
30 if /i "%1" == "release"   (set __BuildType=Release&shift&goto Arg_Loop)
31
32 if /i "%1" == "clean"   (set __CleanBuild=1&shift&goto Arg_Loop)
33
34 if /i "%1" == "freebsdmscorlib" (set __MscorlibOnly=1&set __BuildOS=FreeBSD&shift&goto Arg_Loop)
35 if /i "%1" == "linuxmscorlib" (set __MscorlibOnly=1&set __BuildOS=Linux&shift&goto Arg_Loop)
36 if /i "%1" == "osxmscorlib" (set __MscorlibOnly=1&set __BuildOS=OSX&shift&goto Arg_Loop)
37 if /i "%1" == "windowsmscorlib" (set __MscorlibOnly=1&set __BuildOS=Windows_NT&shift&goto Arg_Loop)
38
39 if /i "%1" == "vs2013" (set __VSVersion=%1&set __VSProductVersion=120&shift&goto Arg_Loop)
40 if /i "%1" == "vs2015" (set __VSVersion=%1&set __VSProductVersion=140&shift&goto Arg_Loop)
41
42 echo Invalid commandline argument: %1
43 goto Usage
44
45 :ArgsDone
46
47 echo Commencing CoreCLR Repo build
48 echo.
49
50 :: Set the remaining variables based upon the determined build configuration
51 set "__BinDir=%__RootBinDir%\Product\%__BuildOS%.%__BuildArch%.%__BuildType%"
52 set "__IntermediatesDir=%__RootBinDir%\obj\%__BuildOS%.%__BuildArch%.%__BuildType%"
53 set "__PackagesBinDir=%__BinDir%\.nuget"
54 set "__ToolsDir=%__RootBinDir%\tools"
55 set "__TestBinDir=%__RootBinDir%\tests\%__BuildOS%.%__BuildArch%.%__BuildType%"
56 set "__TestIntermediatesDir=%__RootBinDir%\tests\obj\%__BuildOS%.%__BuildArch%.%__BuildType%"
57
58 :: Generate path to be set for CMAKE_INSTALL_PREFIX to contain forward slash
59 set "__CMakeBinDir=%__BinDir%"
60 set "__CMakeBinDir=%__CMakeBinDir:\=/%"
61
62 :: Configure environment if we are doing a clean build.
63 if not defined __CleanBuild goto MakeDirs
64 echo Doing a clean build
65 echo.
66
67 :: MSBuild projects would need a rebuild
68 set __MSBCleanBuildArgs=/t:rebuild
69
70 :: Cleanup the previous output for the selected configuration
71 if exist "%__BinDir%" rd /s /q "%__BinDir%"
72 if exist "%__IntermediatesDir%" rd /s /q "%__IntermediatesDir%"
73
74 if exist "%__TestBinDir%" rd /s /q "%__TestBinDir%"
75 if exist "%__TestIntermediatesDir%" rd /s /q "%__TestIntermediatesDir%"
76
77 if exist "%__LogsDir%" del /f /q "%__LogsDir%\*_%__BuildOS%__%__BuildArch%__%__BuildType%.*"
78
79 :MakeDirs
80 if not exist "%__BinDir%" md "%__BinDir%"
81 if not exist "%__IntermediatesDir%" md "%__IntermediatesDir%"
82 if not exist "%__LogsDir%" md "%__LogsDir%"
83
84 :: CMake isn't a requirement when building mscorlib only
85 if defined __MscorlibOnly goto CheckVS
86
87 :CheckPrereqs
88 :: Check prerequisites
89 echo Checking pre-requisites...
90 echo.
91 :: Eval the output from probe-win1.ps1
92 for /f "delims=" %%a in ('powershell -NoProfile -ExecutionPolicy RemoteSigned "& ""%__SourceDir%\pal\tools\probe-win.ps1"""') do %%a
93
94 goto CheckVS
95
96 :CheckVS
97 :: Check presence of VS
98 if defined VS%__VSProductVersion%COMNTOOLS goto CheckVSExistence
99 echo Visual Studio 2013+ (Community is free) is a pre-requisite to build this repository.
100 echo See: https://github.com/dotnet/coreclr/blob/master/Documentation/project-docs/developer-guide.md#prerequisites
101 exit /b 1
102
103 :CheckVSExistence
104 :: Does VS 2013 or VS 2015 really exist?
105 if exist "!VS%__VSProductVersion%COMNTOOLS!\..\IDE\devenv.exe" goto CheckMSBuild
106 echo Visual Studio 2013+ (Community is free) is a pre-requisite to build this repository.
107 echo See: https://github.com/dotnet/coreclr/blob/master/Documentation/project-docs/developer-guide.md#prerequisites
108 exit /b 1
109
110 :CheckMSBuild
111 :: Note: We've disabled node reuse because it causes file locking issues.
112 ::       The issue is that we extend the build with our own targets which
113 ::       means that that rebuilding cannot successfully delete the task
114 ::       assembly. 
115 if /i "%__VSVersion%" =="vs2015" goto MSBuild14
116 set _msbuildexe="%ProgramFiles(x86)%\MSBuild\12.0\Bin\MSBuild.exe"
117 if not exist %_msbuildexe% set _msbuildexe="%ProgramFiles%\MSBuild\12.0\Bin\MSBuild.exe"
118 if not exist %_msbuildexe% set _msbuildexe="%ProgramFiles(x86)%\MSBuild\14.0\Bin\MSBuild.exe"
119 goto :CheckMSBuild14
120 :MSBuild14
121 set _msbuildexe="%ProgramFiles(x86)%\MSBuild\14.0\Bin\MSBuild.exe"
122 set UseRoslynCompiler=true
123 :CheckMSBuild14
124 if not exist %_msbuildexe% set _msbuildexe="%ProgramFiles%\MSBuild\14.0\Bin\MSBuild.exe"
125 if not exist %_msbuildexe% echo 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
126
127 :: All set to commence the build
128
129 setlocal
130 if defined __MscorlibOnly goto PerformMScorlibBuild
131
132 echo Commencing build of native components for %__BuildOS%.%__BuildArch%.%__BuildType%
133 echo.
134
135 :: Set the environment for the native build
136 call "!VS%__VSProductVersion%COMNTOOLS!\..\..\VC\vcvarsall.bat" x86_amd64
137
138 if exist "%VSINSTALLDIR%DIA SDK" goto GenVSSolution
139 echo Error: DIA SDK is missing at "%VSINSTALLDIR%DIA SDK". ^
140 This is due to a bug in the Visual Studio installer. It does not install DIA SDK at "%VSINSTALLDIR%" but rather ^
141 at VS install location of previous version. Workaround is to copy DIA SDK folder from VS install location ^
142 of previous version to "%VSINSTALLDIR%" and then resume build.
143 :: DIA SDK not included in Express editions
144 echo Visual Studio 2013 Express does not include the DIA SDK. ^
145 You need Visual Studio 2013+ (Community is free).
146 echo See: https://github.com/dotnet/coreclr/blob/master/Documentation/project-docs/developer-guide.md#prerequisites
147 exit /b 1
148
149 :GenVSSolution
150 :: Regenerate the VS solution
151 pushd "%__IntermediatesDir%"
152 call "%__SourceDir%\pal\tools\gen-buildsys-win.bat" "%__ProjectDir%" %__VSVersion%
153 popd
154
155 :BuildComponents
156 if exist "%__IntermediatesDir%\install.vcxproj" goto BuildCoreCLR
157 echo Failed to generate native component build project!
158 exit /b 1
159
160 REM Build CoreCLR
161 :BuildCoreCLR
162 set "__CoreCLRBuildLog=%__LogsDir%\CoreCLR_%__BuildOS%__%__BuildArch%__%__BuildType%.log"
163 %_msbuildexe% "%__IntermediatesDir%\install.vcxproj" %__MSBCleanBuildArgs% /nologo /maxcpucount /nodeReuse:false /p:Configuration=%__BuildType% /p:Platform=%__BuildArch% /fileloggerparameters:Verbosity=normal;LogFile="%__CoreCLRBuildLog%"
164 IF NOT ERRORLEVEL 1 goto PerformMScorlibBuild
165 echo Native component build failed. Refer !__CoreCLRBuildLog! for details.
166 exit /b 1
167
168 :PerformMScorlibBuild
169 REM endlocal to rid us of environment changes from vcvarsall.bat
170 endlocal
171
172 REM setlocal to prepare for vsdevcmd.bat
173 setlocal
174 set __AdditionalMSBuildArgs=
175
176 rem Explicitly set Platform causes conflicts in mscorlib project files. Clear it to allow building from VS x64 Native Tools Command Prompt
177 set Platform=
178
179 if defined __MscorlibOnly set __AdditionalMSBuildArgs=/p:BuildNugetPackage=false
180
181 :: Set the environment for the managed build
182 call "!VS%__VSProductVersion%COMNTOOLS!\VsDevCmd.bat"
183 echo Commencing build of mscorlib for %__BuildOS%.%__BuildArch%.%__BuildType%
184 echo.
185 set "__MScorlibBuildLog=%__LogsDir%\MScorlib_%__BuildOS%__%__BuildArch%__%__BuildType%.log"
186 %_msbuildexe% "%__ProjectFilesDir%\build.proj" %__MSBCleanBuildArgs% /nologo /maxcpucount /verbosity:minimal /nodeReuse:false /fileloggerparameters:Verbosity=normal;LogFile="%__MScorlibBuildLog%" /p:OS=%__BuildOS% %__AdditionalMSBuildArgs%
187 IF NOT ERRORLEVEL 1 (
188   if defined __MscorlibOnly exit /b 0
189   goto CrossGenMscorlib
190 )
191
192 echo MScorlib build failed. Refer !__MScorlibBuildLog! for details.
193 exit /b 1
194
195 :CrossGenMscorlib
196 echo Generating native image of mscorlib for %__BuildOS%.%__BuildArch%.%__BuildType%
197 echo.
198 set "__CrossGenMScorlibLog=%__LogsDir%\CrossgenMScorlib_%__BuildOS%__%__BuildArch%__%__BuildType%.log"
199 %__BinDir%\crossgen.exe %__BinDir%\mscorlib.dll > "%__CrossGenMScorlibLog%" 2>&1
200 IF NOT ERRORLEVEL 1 (
201   goto PerformTestBuild
202 )
203
204 echo CrossGen mscorlib failed. Refer !__CrossGenMScorlibLog! for details.
205 exit /b 1
206
207 :PerformTestBuild
208 echo.
209 echo Commencing build of tests for %__BuildOS%.%__BuildArch%.%__BuildType%
210 echo.
211 call tests\buildtest.cmd
212 IF NOT ERRORLEVEL 1 goto SuccessfulBuild
213 echo Test binaries build failed. Refer !__MScorlibBuildLog! for details.
214 exit /b 1
215
216 :SuccessfulBuild
217 ::Build complete
218 echo Repo successfully built.
219 echo.
220 echo Product binaries are available at !__BinDir!
221 echo Test binaries are available at !__TestBinDir!
222 exit /b 0
223
224 :Usage
225 echo.
226 echo Usage:
227 echo %0 BuildArch BuildType [clean] [vsversion] where:
228 echo.
229 echo BuildArch can be: x64
230 echo BuildType can be: Debug, Release
231 echo Clean - optional argument to force a clean build.
232 echo VSVersion - optional argument to use VS2013 or VS2015 (default VS2013)
233 echo windowsmscorlib - Build mscorlib for Windows
234 echo linuxmscorlib - Build mscorlib for Linux
235 echo osxmscorlib - Build mscorlib for OS X
236 exit /b 1