-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlinux-setup.sh
More file actions
executable file
·251 lines (198 loc) · 5.6 KB
/
linux-setup.sh
File metadata and controls
executable file
·251 lines (198 loc) · 5.6 KB
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
241
242
243
244
245
246
247
248
249
250
251
#!/bin/bash -e
# This is a script to perform build and install processes for the `haiku-format` tool on a
# Linux platform. Run the script with no arguments to get usage. See the accompanying
# `linux.md` file for instructions on general use.
set -e
set -o pipefail
script_bin=$0
build_dir="llvm-project/build"
install_dir="/opt/haiku-format"
llvm_project=llvm-project
function hf_usage()
{
cat <<EOF
${script_bin}
This script is designed to build the 'haiku-format' tool and install it on an
'apt'-based Linux system.
${script_bin} build
${script_bin} clean
sudo ${script_bin} install
sudo ${script_bin} uninstall
EOF
exit 1
}
function hf_confirm_yes_no()
{
while true; do
read -p "$* [y/n]: " yn
case $yn in
[Yy])
return 0
;;
[Nn])
echo "aborted" 1>&2
return 1
;;
esac
done
}
# Detects absent Debian packages that are required for build and install. If there are
# any absent then it will error to the user with the command they are required to run
# to install the missing packages.
function hf_install_dependencies_linux_gnu_apt()
{
local depends="lld clang cmake ninja-build"
local missing_depends=()
for d in $depends; do
if ! dpkg -s "${d}" &> /dev/null; then
missing_depends+=("${d}")
fi
done
if [ "${#missing_depends[@]}" -gt 0 ]; then
echo "ensure that the necessary dependencies are installed before running this script;"
echo "sudo apt install ${missing_depends[*]}"
exit 1
fi
}
# A number of source files have to be downloaded for the build process to work. This
# function will pull those down and then un-pack them into the directory where the
# build will be undertaken.
function hf_download_sources()
{
if [ $# != 1 ]; then
echo "missing llvm version to download sources"
exit 1
fi
local llvm_version="$1"
local src="${llvm_project}-${llvm_version}.src"
local uri="https://github.com/llvm/${llvm_project}/releases/download/llvmorg-${llvm_version}"
local tarball="${src}.tar.xz"
if [ -e "${tarball}" ]; then
echo "file [${tarball}] exists - can skip download"
else
echo "will download [${tarball}]"
wget -N "${uri}/${tarball}"
fi
mkdir -v "${llvm_project}"
echo -n "will extract ${a}"
tar -xJf "${tarball}" -C "${llvm_project}" --strip-components=1 "${src}"/{clang,cmake,llvm,third-party}
}
# This function will download and unpack the sources and then perform the build. It will
# not proceed if there is already a build product present in the build location.
function hf_build()
{
local llvm_version
llvm_version="$(ls -v v*.diff | tail -1 | sed -E 's/v([0-9]+(\.[0-9]+){2})\.diff/\1/')"
if [[ ! "${llvm_version}" =~ ^[0-9]+\.[0-9]+\.[0-9]+ ]]; then
echo "unable to establish the llvm version"
exit 1
fi
if [ -e "${llvm_project}" ]; then
echo "Please rerun this script after removing ${llvm_project}. You can achieve this by running;"
echo "${script_bin} clean"
exit 1
fi
echo "building for version [${llvm_version}]"
hf_install_dependencies_linux_gnu_apt
hf_download_sources "${llvm_version}"
local cmake_options="-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DLLVM_USE_LINKER=lld"
pushd "${llvm_project}"
patch -N -p1 -r - < "../v${llvm_version}.diff"
cmake -Wno-dev -S llvm -B "build" -G Ninja ${cmake_options} \
-DCLANG_ENABLE_STATIC_ANALYZER=OFF \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_APPEND_VC_REV=OFF \
-DLLVM_BUILD_TOOLS=OFF \
-DLLVM_ENABLE_PROJECTS=clang \
-DLLVM_INCLUDE_BENCHMARKS=OFF \
-DLLVM_INCLUDE_EXAMPLES=OFF \
-DLLVM_TARGETS_TO_BUILD=host
ninja -C build clang-format
popd
echo "did perform build"
}
# Removes the downloaded sources and build product from this directory (not the install).
function hf_clean()
{
if [ -d "llvm-project" ]; then
echo "deleting [llvm-project]..."
rm -rf "llvm-project"
echo "did delete [llvm-project]"
fi
for l in ./*.src.tar.xz; do
rm "${l}"
echo "deleted [${l}]"
done
echo "did perform clean"
}
# Copies the build product and a launch script into place. It will also create a
# little launch script which sets the library path.
function hf_install()
{
if [ ! -f "${build_dir}/bin/clang-format" ]; then
echo "Build product to install not found. You can run a build with;"
echo "${script_bin} build"
exit 1
fi
local install_root
if [ -f "${install_root}/bin/haiku-format" ]; then
echo "the program is already installed at [${install_root}]"
exit 1
fi
if [[ $EUID -ne 0 ]]; then
echo "run install as the root user by using 'sudo'"
exit 1
fi
mkdir -p "${install_dir}/bin"
cp -fv ${build_dir}/bin/clang-format "${install_dir}/bin/haiku-format"
strip -sv "${install_dir}/bin/haiku-format"
sed s/clang-format/haiku-format/g llvm-project/clang/tools/clang-format/git-clang-format \
> "${install_dir}/bin/git-haiku-format"
chmod -v ogu+x "${install_dir}/bin/haiku-format"
chmod -v ogu+x "${install_dir}/bin/git-haiku-format"
}
hf_uninstall()
{
if [[ $EUID -ne 0 ]]; then
echo "run uninstall as the root user by using 'sudo'"
exit 1
fi
if [ ! -d "${install_dir}" ]; then
echo "The program is not installed at [${install_dir}]"
exit 1
fi
if ! hf_confirm_yes_no "remove the haiku-format program from [${install_dir}]"; then
exit 1
fi
rm -rf "${install_dir}"
echo "did remove the 'haiku-format' program"
}
hf_main()
{
if [ $# != 1 ]; then
hf_usage
fi
if ! command -v dpkg &> /dev/null; then
echo "this script [${script_bin}] can only be used with an 'apt' based distribution of linux."
fi
local command=$1
shift
case "${command}" in
build)
hf_build
;;
install)
hf_install
;;
uninstall)
hf_uninstall
;;
clean)
hf_clean
;;
*)
echo "unknown command [$1]" 1>&2
return 1
esac
}
hf_main "$@"