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