forked from dotnet/jitutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.sh
executable file
·240 lines (194 loc) · 6.77 KB
/
bootstrap.sh
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env bash
# Bootstrap the jitutils tools:
# 1. If this script is run not from within the jitutils directory (e.g., you've downloaded
# a copy of this file directly), then "git clone" the jitutils project first. Otherwise,
# if we can tell we're being run from within an existing "git clone", don't do that.
# 2. Build the jitutils tools.
# 3. Download (if necessary) clang-format and clang-tidy (used by the jit-format tool).
function get_host_os {
# Use uname to determine what the OS is.
OSName=$(uname -s)
case $OSName in
Linux)
__HostOS=Linux
;;
Darwin)
__HostOS=OSX
;;
FreeBSD)
__HostOS=FreeBSD
;;
OpenBSD)
__HostOS=OpenBSD
;;
NetBSD)
__HostOS=NetBSD
;;
SunOS)
__HostOS=SunOS
;;
*)
echo "Unsupported OS $OSName detected, configuring as if for Linux"
__HostOS=Linux
;;
esac
}
_machineHasCurl=
function validate_url {
if (( _machineHasCurl == 1 )); then
status="$(curl -sLIo /dev/null "$1" -w '%{http_code}\n')"
else
response=($(wget -S --spider "$1" 2>&1 | grep "HTTP/"))
status="${response[1]}"
fi
if (( status >= 200 || status < 400 )); then
return 0;
else
return 1;
fi
}
function download_tools {
echo "Installing clang ${clangVersion} tools"
# Do we have wget or curl?
if command -v curl 2>/dev/null; then
_machineHasCurl=1
elif ! command -v wget 2>/dev/null; then
echo "Error: curl or wget not found; not downloading clang-format and clang-tidy."
return 1
fi
# Figure out which version to download. Use the "RID:" value from "dotnet --info".
# It looks like one of these:
# RID: osx-arm64
# RID: linux-x64
# RID: osx.10.12-x64
# RID: ubuntu.22.04-x64
#
# If the RID doesn't work directly, it could be used to pick the appropriate platform.
# Note that we currently don't support osx-x64 clang tools.
clangToolsRootUrl="https://clrjit2.blob.core.windows.net/clang-tools"
clangPlatform="$(dotnet --info | grep 'RID:')"
clangPlatform="${clangPlatform##*RID:* }"
# override common RIDs with compatible version so we don't need to upload binaries for each RID
case $clangPlatform in
osx*-x64)
echo "clang-tidy/clang-format are not supported on osx-x64."
return 0
;;
ubuntu.*-x64)
clangPlatform=linux-x64
;;
esac
clangFormatUrl="${clangToolsRootUrl}/${clangVersion}/${clangPlatform}/clang-format"
if validate_url "$clangFormatUrl" > /dev/null; then
echo "Downloading clang-format from ${clangFormatUrl} to bin directory"
if (( _machineHasCurl == 1 )); then
curl --retry 4 --progress-bar --location --fail "$clangFormatUrl" -o bin/clang-format
else
wget --tries 4 --progress=dot:giga "$clangFormatUrl" -O bin/clang-format
fi
chmod 751 bin/clang-format
else
echo "clang-format not found here: $clangFormatUrl"
fi
clangTidyUrl="${clangToolsRootUrl}/${clangVersion}/${clangPlatform}/clang-tidy"
if validate_url "$clangTidyUrl" > /dev/null; then
echo "Downloading clang-tidy from ${clangTidyUrl} to bin directory"
if (( _machineHasCurl == 1 )); then
curl --retry 4 --progress-bar --location --fail "$clangTidyUrl" -o bin/clang-tidy
else
wget --tries 4 --progress=dot:giga "$clangTidyUrl" -O bin/clang-tidy
fi
chmod 751 bin/clang-tidy
else
echo "clang-tidy not found here: $clangTidyUrl"
fi
if [ ! -f bin/clang-format -o ! -f bin/clang-tidy ]; then
echo "Either clang-tidy or clang-format was not installed. Please install and put them on the PATH to use jit-format."
echo "Tools can be found at https://github.com/llvm/llvm-project/releases/tag/llvmorg-${clangVersion}"
return 1
fi
return 0
}
# Start the non-functions.
__ErrMsgPrefix="ERROR: "
get_host_os
# Check if our required tools exist.
if ! hash dotnet 2>/dev/null; then
echo "${__ErrMsgPrefix}Can't find dotnet! Please install from https://dot.net and add to PATH."
exit 1
fi
if ! hash git 2>/dev/null; then
echo "${__ErrMsgPrefix}Can't find git! Please add to PATH."
exit 1
fi
# Are we already in the dotnet/jitutils repo? Or do we need to clone it? We look for build.cmd
# in the directory this script was invoked from.
# Obtain the location of the bash script.
__root="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Check if the bootstrap script is in the root of the jitutils repo.
# By default, we're going to clone the repo.
__clone_repo=1
if [ -e ${__root}/build.cmd ]; then
# We found build.cmd. But make sure it's the root of the repo.
__root="$( cd ${__root} && git rev-parse --show-toplevel )"
pushd ${__root} >/dev/null
git remote -v | grep "/jitutils" >/dev/null
if [ $? == 0 ]; then
# We've proven that we're at the root of the jitutils repo.
__clone_repo=0
fi
popd >/dev/null
fi
if [ ${__clone_repo} == 1 ]; then
git clone https://github.com/dotnet/jitutils.git
exit_code=$?
if [ $exit_code != 0 ]; then
echo "${__ErrMsgPrefix}git clone failed."
exit $exit_code
fi
if [ ! -d ${__root}/jitutils ]; then
echo "${__ErrMsgPrefix}can't find ${__root}/jitutils."
exit 1
fi
pushd ${__root}/jitutils >/dev/null
else
pushd . >/dev/null
fi
# Pull in needed packages. This works globally (due to jitutils.sln).
dotnet restore
exit_code=$?
if [ $exit_code != 0 ]; then
echo "${__ErrMsgPrefix}Failed to restore packages."
exit $exit_code
fi
# Build and publish all the utilities
./build.sh -p
exit_code=$?
if [ $exit_code != 0 ]; then
echo "${__ErrMsgPrefix}Build failed."
exit $exit_code
fi
# Check for the formatting tools, and download them if necessary.
clangVersion="17.0.6"
exit_code=0
if ! hash clang-format 2>/dev/null || ! hash clang-tidy 2>/dev/null; then
download_tools
exit_code=$?
else
if ! clang-format --version | grep -q "version ${clangVersion}" || ! clang-tidy --version | grep -q "version ${clangVersion}"; then
echo "jit-format requires clang-format and clang-tidy version ${clangVersion}. Currently installed: "
clang-format --version
clang-tidy --version
download_tools
exit_code=$?
fi
fi
if [ $exit_code != 0 ]; then
echo "${__ErrMsgPrefix}Failed to download clang-format and clang-tidy."
exit $exit_code
fi
popd >/dev/null
echo "Adding ${__root}/jitutils/bin to PATH"
export PATH=$PATH:${__root}/jitutils/bin
echo "Done setting up!"
exit 0