-
Notifications
You must be signed in to change notification settings - Fork 60
/
bootstrap.cmd
195 lines (148 loc) · 6.21 KB
/
bootstrap.cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
@echo off
REM Bootstrap the jitutils tools:
REM 1. If this script is run not from within the jitutils directory (e.g., you've downloaded
REM a copy of this file directly), then "git clone" the jitutils project first. Otherwise,
REM if we can tell we're being run from within an existing "git clone", don't do that.
REM 2. Build the jitutils tools.
REM 3. Download (if necessary) clang-format.exe and clang-tidy.exe (used by the jit-format tool).
set __ExitCode=0
where /q dotnet.exe
if %errorlevel% NEQ 0 echo Can't find dotnet.exe! Please install this ^(e.g., from https://www.microsoft.com/net/core^) and add dotnet.exe to PATH&set __ExitCode=1&goto :script_exit
where /q git.exe
if %errorlevel% NEQ 0 echo Can't find git.exe! Please add to PATH&set __ExitCode=1&goto :script_exit
set __root=%~dp0
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
REM Are we already in the dotnet/jitutils repo? Or do we need to clone it? We look for build.cmd
REM in the current directory (which is the directory this script was invoked from).
if not exist %__root%build.cmd goto clone_jitutils
pushd %__root%
REM Check if build.cmd is in the root of the repo.
set __tempfile=%TEMP%\gittemp-%RANDOM%.txt
git rev-parse --show-toplevel >%__tempfile% 2>&1
if errorlevel 1 (
echo Error: git failure:
type %__tempfile%
echo Cloning jitutils repo.
del %__tempfile%
popd
goto clone_jitutils
)
set /P gitroot=<%__tempfile%
del %__tempfile%
set gitroot=%gitroot:/=\%
if not %gitroot:~-1%==\ set gitroot=%gitroot%\
if /I not %__root%==%gitroot% (
echo It doesn't looks like bootstrap.cmd is at the root of the repo.
echo Cloning jitutils repo.
popd
goto clone_jitutils
)
REM Is this actually the jitutils repo?
git remote -v | findstr /i /c:"/jitutils" >nul
if errorlevel 1 (
echo It doesn't looks like we're in the jitutils repo.
echo Cloning jitutils repo.
popd
goto clone_jitutils
)
REM Now go ahead and build it.
call :build_jitutils
popd
REM If the build failed, we need to check it here, before the "endlocal".
if %__ExitCode% NEQ 0 goto :script_exit
:: Add utilites to the current path, but only if not already there
endlocal
call :AddToPath %__root%bin
set __root=
goto :script_exit
REM ===================================================================
REM == This is top-level, not a "call".
REM ==
:clone_jitutils
pushd %__root%
:: Clone the jitutils repo
git clone https://github.com/dotnet/jitutils.git
if errorlevel 1 echo ERROR: clone failed.&set __ExitCode=1&goto :script_exit
if not exist .\jitutils echo ERROR: can't find jitutils directory.&set __ExitCode=1goto :script_exit
pushd .\jitutils
call :build_jitutils
popd
popd
REM If the build failed, we need to check it here, before the "endlocal".
if %__ExitCode% NEQ 0 goto :script_exit
:: Add utilites to the current path
endlocal
call :AddToPath %__root%jitutils\bin
set __root=
goto :script_exit
REM ===================================================================
:build_jitutils
if not exist .\build.cmd echo Can't find build.cmd.&set __ExitCode=1&goto :eof
:: Pull in needed packages. This works globally (due to jitutils.sln).
dotnet restore
if errorlevel 1 echo ERROR: dotnet restore failed.&set __ExitCode=1&goto :eof
:: Build and publish all the utilities
call .\build.cmd -p
if errorlevel 1 echo ERROR: build failed.&set __ExitCode=1&goto :eof
:: Check to see if clang-format and clang-tidy are available. Since we're going
:: to add the 'bin' directory to the path, and they most likely already live there
:: if you're running bootstrap.cmd not for the first time, add 'bin' to the path
:: here (within the setlocal scope for now) before checking for them. Add 'bin'
:: at the end of the path, to prefer other user downloaded versions, if any.
set _clang_version=17.0.6
set PATH=%PATH%;.\bin
where /Q clang-format
IF %errorlevel% NEQ 0 GOTO DownloadTools
where /Q clang-tidy
IF %errorlevel% NEQ 0 GOTO DownloadTools
REM We found the tools on the path; now make sure the versions are good.
clang-format --version | findstr /c:"version %_clang_version%" > NUL
IF %errorlevel% EQU 0 GOTO build_done
echo jit-format requires clang-format and clang-tidy version %_clang_version%. Currently installed:
clang-format --version
clang-tidy --version
echo Please install version %_clang_version% and put the tools on the PATH to use jit-format.
echo Tools can be found at https://github.com/llvm/llvm-project/releases/tag/llvmorg-%_clang_version%
:DownloadTools
echo Downloading formatting tools
set _azdo_root=https://clrjit2.blob.core.windows.net/clang-tools
set _platform=windows-x64
set _azdo_dir=%_azdo_root%/%_clang_version%/%_platform%
call :download_url clang-format "%_azdo_dir%/clang-format.exe" bin\clang-format.exe
if %__ExitCode% NEQ 0 goto :eof
call :download_url clang-tidy "%_azdo_dir%/clang-tidy.exe" bin\clang-tidy.exe
if %__ExitCode% NEQ 0 goto :eof
:build_done
goto :eof
REM ===================================================================
REM Download helper, with retry on failure. Args:
REM %1 - friendly name of download
REM %2 - URL to download
REM %3 - target of download
REM
REM We'd like to use Invoke-WebRequest `-MaximumRetryCount 8 -RetryIntervalSec 15`, but those aren't
REM available on all versions of PowerShell.
:download_url
set _friendly_name=%1
set _url=%2
set _target_file=%3
set _retry_count=1
:start_download
echo call powershell Invoke-WebRequest -Uri %_url% -OutFile %_target_file%
call powershell Invoke-WebRequest -Uri %_url% -OutFile %_target_file%
if %errorlevel% EQU 0 goto :eof
echo ERROR: attempt #%_retry_count% failed to download %_friendly_name%.
if %_retry_count% GEQ 4 echo ERROR: giving up attempting to download %_friendly_name%.&set __ExitCode=1&goto :eof
echo Waiting 15 seconds to retry...
powershell -command "Start-Sleep -Seconds 15"
set /A _retry_count=_retry_count + 1
goto start_download
REM ===================================================================
:script_exit
REM Note, this entire line gets variable expanded before execution, so the "exit" use of %__ExitCode% gets expanded before it is cleared during execution.
set __ExitCode=&exit /b %__ExitCode%
REM ===================================================================
:AddToPath
set PATH | findstr /i %1 >nul
if %errorlevel% NEQ 0 set PATH=%PATH%;%1
goto :eof