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