Use arcade's version of dotnet to build (#22755)
[platform/upstream/coreclr.git] / init-tools.cmd
1 @if not defined _echo @echo off
2 setlocal
3
4 set INIT_TOOLS_LOG=%~dp0init-tools.log
5 if [%PACKAGES_DIR%]==[] set PACKAGES_DIR=%~dp0packages
6 if [%TOOLRUNTIME_DIR%]==[] set TOOLRUNTIME_DIR=%~dp0Tools
7 set DOTNET_PATH=%~dp0.dotnet\
8 if [%DOTNET_CMD%]==[] set DOTNET_CMD=%DOTNET_PATH%dotnet.exe
9 if [%BUILDTOOLS_SOURCE%]==[] set BUILDTOOLS_SOURCE=https://dotnet.myget.org/F/dotnet-buildtools/api/v3/index.json
10 set /P BUILDTOOLS_VERSION=< "%~dp0BuildToolsVersion.txt"
11 set BUILD_TOOLS_PATH=%PACKAGES_DIR%\Microsoft.DotNet.BuildTools\%BUILDTOOLS_VERSION%\lib
12 set INIT_TOOLS_RESTORE_PROJECT=%~dp0init-tools.msbuild
13 set BUILD_TOOLS_SEMAPHORE_DIR=%TOOLRUNTIME_DIR%\%BUILDTOOLS_VERSION%
14 set BUILD_TOOLS_SEMAPHORE=%BUILD_TOOLS_SEMAPHORE_DIR%\init-tools.completed
15
16 :: if force option is specified then clean the tool runtime and build tools package directory to force it to get recreated
17 if [%1]==[force] (
18   if exist "%TOOLRUNTIME_DIR%" rmdir /S /Q "%TOOLRUNTIME_DIR%"
19   if exist "%PACKAGES_DIR%\Microsoft.DotNet.BuildTools" rmdir /S /Q "%PACKAGES_DIR%\Microsoft.DotNet.BuildTools"
20 )
21
22 :: If semaphore exists do nothing
23 if exist "%BUILD_TOOLS_SEMAPHORE%" (
24   echo Tools are already initialized.
25   goto :EOF
26 )
27
28 if exist "%TOOLRUNTIME_DIR%" rmdir /S /Q "%TOOLRUNTIME_DIR%"
29
30 if exist "%DotNetBuildToolsDir%" (
31   echo Using tools from '%DotNetBuildToolsDir%'.
32   mklink /j "%TOOLRUNTIME_DIR%" "%DotNetBuildToolsDir%"
33
34   if not exist "%DOTNET_CMD%" (
35     echo ERROR: Ensure that '%DotNetBuildToolsDir%' contains the .NET Core SDK at '%DOTNET_PATH%'
36     exit /b 1
37   )
38
39   echo Done initializing tools.
40   if NOT exist "%BUILD_TOOLS_SEMAPHORE_DIR%" mkdir "%BUILD_TOOLS_SEMAPHORE_DIR%"
41   echo Using tools from '%DotNetBuildToolsDir%'. > "%BUILD_TOOLS_SEMAPHORE%"
42   exit /b 0
43 )
44
45 echo Running %0 > "%INIT_TOOLS_LOG%"
46
47 set /p DOTNET_VERSION=< "%~dp0DotnetCLIVersion.txt"
48 if exist "%DOTNET_CMD%" goto :afterdotnetrestore
49
50 REM Use x86 tools on arm64 and x86.
51 REM arm32 host is not currently supported, please crossbuild.
52 if /i "%PROCESSOR_ARCHITECTURE%" == "arm" (
53   echo "Error, arm32 arch not supported for build tools."
54   exit /b 1
55 )
56
57 if /i "%PROCESSOR_ARCHITECTURE%" == "amd64" (
58   set _Arch=x64
59   goto ArchSet
60 )
61
62 REM If this is not amd64 and not arm, then we should be running on arm64 or x86
63 REM either way we can (and should) use the x86 dotnet cli
64 REM
65 REM TODO: consume native arm64 toolset, blocked by official arm64 windows cli
66 REM     : release. See https://github.com/dotnet/coreclr/issues/19614 for more
67 REM     : information
68 set _Arch=x86
69
70 echo "init-tools.cmd: Setting arch to %_Arch% for build tools"
71
72 :ArchSet
73
74 echo Installing dotnet cli...
75 set PS_DOTNET_INSTALL_SCRIPT=". %~dp0eng\configure-toolset.ps1; . %~dp0eng\common\tools.ps1; InitializeBuildTool"
76 if NOT exist "%DOTNET_CMD%" (
77   echo running: powershell -NoProfile -ExecutionPolicy unrestricted -Command %PS_DOTNET_INSTALL_SCRIPT%
78   powershell -NoProfile -ExecutionPolicy unrestricted -Command %PS_DOTNET_INSTALL_SCRIPT%
79   if NOT exist "%DOTNET_CMD%" (
80     echo ERROR: Could not install dotnet cli correctly. 1>&2
81     goto :error
82   )
83 )
84
85 :afterdotnetrestore
86
87 REM We do not need the build tools for arm64/x86
88 if /i "%PROCESSOR_ARCHITEW6432%" == "arm64" (
89   goto :EOF
90 )
91
92 if /i "%PROCESSOR_ARCHITECTURE%" == "arm64" (
93   goto :EOF
94 )
95
96 if exist "%BUILD_TOOLS_PATH%" goto :afterbuildtoolsrestore
97 echo Restoring BuildTools version %BUILDTOOLS_VERSION%...
98 echo Running: "%DOTNET_CMD%" restore "%INIT_TOOLS_RESTORE_PROJECT%" --no-cache --packages "%PACKAGES_DIR%" --source "%BUILDTOOLS_SOURCE%" /p:BuildToolsPackageVersion=%BUILDTOOLS_VERSION% /p:ToolsDir=%TOOLRUNTIME_DIR% >> "%INIT_TOOLS_LOG%"
99 call "%DOTNET_CMD%" restore "%INIT_TOOLS_RESTORE_PROJECT%" --no-cache --packages "%PACKAGES_DIR%" --source "%BUILDTOOLS_SOURCE%" /p:BuildToolsPackageVersion=%BUILDTOOLS_VERSION% /p:ToolsDir=%TOOLRUNTIME_DIR% >> "%INIT_TOOLS_LOG%"
100 if NOT exist "%BUILD_TOOLS_PATH%\init-tools.cmd" (
101   echo ERROR: Could not restore build tools correctly. 1>&2
102   goto :error
103 )
104
105 :afterbuildtoolsrestore
106
107 :: Ask init-tools to also restore ILAsm
108 set /p ILASMCOMPILER_VERSION=< "%~dp0ILAsmVersion.txt"
109
110 echo Initializing BuildTools...
111 echo Running: "%BUILD_TOOLS_PATH%\init-tools.cmd" "%~dp0" "%DOTNET_CMD%" "%TOOLRUNTIME_DIR%" >> "%INIT_TOOLS_LOG%"
112 call "%BUILD_TOOLS_PATH%\init-tools.cmd" "%~dp0" "%DOTNET_CMD%" "%TOOLRUNTIME_DIR%" "%PACKAGES_DIR%" >> "%INIT_TOOLS_LOG%"
113 set INIT_TOOLS_ERRORLEVEL=%ERRORLEVEL%
114 if not [%INIT_TOOLS_ERRORLEVEL%]==[0] (
115   echo ERROR: An error occured when trying to initialize the tools. 1>&2
116   goto :error
117 )
118
119 :: Create semaphore file
120 echo Done initializing tools.
121 if NOT exist "%BUILD_TOOLS_SEMAPHORE_DIR%" mkdir "%BUILD_TOOLS_SEMAPHORE_DIR%"
122 echo Init-Tools.cmd completed for BuildTools Version: %BUILDTOOLS_VERSION% > "%BUILD_TOOLS_SEMAPHORE%"
123 exit /b 0
124
125 :error
126 echo Please check the detailed log that follows. 1>&2
127 type "%INIT_TOOLS_LOG%" 1>&2
128 exit /b 1