-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathprerequisites.bash
More file actions
executable file
·345 lines (311 loc) · 12 KB
/
Copy pathprerequisites.bash
File metadata and controls
executable file
·345 lines (311 loc) · 12 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env bash
# ---------------------------------------------------------------------------
# Hayroll dependency installer
# ---------------------------------------------------------------------------
set -euo pipefail
ROOT_DIR=${ROOT_DIR:-"${PWD}"} # default to current dir if not overridden
INSTALL_DIR=$(realpath "${ROOT_DIR}/dependencies") # normalize the path
# --- third-party folders we expect under $INSTALL_DIR ----------------------------------
THIRD_PARTY_DIRS=(Maki tree-sitter tree-sitter-c_preproc c2rust z3 libmcs)
# --- Git URLs + tags -------------------------------------------------------------------
C2RUST_GIT="https://github.com/immunant/c2rust.git"
C2RUST_TAG="v0.20.0"
MAKI_GIT="https://github.com/UW-HARVEST/Maki.git"
MAKI_TAG="0.1.7"
TS_GIT="https://github.com/tree-sitter/tree-sitter.git"
TS_TAG="v0.25.10"
TSC_PREPROC_GIT="https://github.com/UW-HARVEST/tree-sitter-c_preproc.git"
TSC_PREPROC_TAG="0.1.7"
Z3_GIT="https://github.com/Z3Prover/z3.git"
Z3_VERSION="4.13.4"
Z3_TAG="z3-${Z3_VERSION}"
LIBMCS_GIT="https://gitlab.com/gtd-gmbh/libmcs.git"
LIBMCS_TAG="1.2.0"
# --- Parse arguments --------------------------------------------------------
USE_LATEST=false
SUDO=sudo
LLVM_VERSION=""
while [[ $# -gt 0 ]]; do
case "$1" in
--latest)
USE_LATEST=true
shift
;;
--no-sudo)
SUDO=
shift
;;
--llvm-version)
if [[ -n "${2:-}" ]]; then
LLVM_VERSION="$2"
shift 2
else
echo "Error: --llvm-version requires a version number"
exit 1
fi
;;
--llvm-version=*)
LLVM_VERSION="${1#*=}"
shift
;;
-h | --help)
echo "Usage: $0 [--latest] [--no-sudo] [--llvm-version VERSION] [-h|--help]"
echo
echo "Options:"
echo " --latest Fetch the latest main/HEAD versions of Maki and tree-sitter-c_preproc."
echo " --no-sudo Run the script without using sudo for package installation."
echo " --llvm-version VER Specify LLVM/Clang version to install (default: use system default version)."
echo " -h, --help Show this help message."
exit 0
;;
*)
echo "Unknown option: $1"
echo "Use -h or --help for usage information."
exit 1
;;
esac
done
echo "=========================================================="
echo "Hayroll prerequisites installer"
echo "Target root directory : ${INSTALL_DIR}"
if [[ -n "${LLVM_VERSION}" ]]; then
echo "LLVM/Clang version : ${LLVM_VERSION}"
else
echo "LLVM/Clang version : system default"
fi
echo
echo "The following directories will be created / updated:"
for d in "${THIRD_PARTY_DIRS[@]}"; do
echo " - ${INSTALL_DIR}/${d}"
done
echo "=========================================================="
mkdir -p "${INSTALL_DIR}"
cd "${INSTALL_DIR}"
# Create a temporary directory for logs
LOG_DIR=$(mktemp -d /tmp/hayroll-prereq-logs-XXXXXX)
git_clone_or_checkout() {
local dir=$1 url=$2 tag=$3
if [[ -d "${dir}/.git" ]]; then
if [[ "${tag}" == "main" ]]; then
git -C "${dir}" fetch origin main --quiet
local target_ref="origin/main"
else
git -C "${dir}" fetch --tags --quiet
local target_ref="${tag}"
fi
if [[ "$(git -C "${dir}" rev-parse HEAD)" != "$(git -C "${dir}" rev-parse "${target_ref}")" ]]; then
echo "[*] ${dir} exists – syncing to latest ${target_ref}"
git -C "${dir}" reset --hard "${target_ref}" --quiet
fi
elif [[ -d "${dir}" ]]; then
echo "[*] ${dir} already present (vendored), skipping clone"
else
echo "[*] Cloning ${url} into ${dir}"
git clone --quiet "${url}" "${dir}"
git -C "${dir}" checkout --quiet "${tag}"
fi
}
check_version() {
local pkg=$1
local min_version=$2
local max_version=$3
local installed_version
installed_version=$(dpkg-query -W -f='${Version}' "$pkg" 2> /dev/null || echo "0")
if [[ "$installed_version" == "0" ]]; then
echo "Warning: $pkg is not installed."
return
fi
# Extract major version number for comparison, handling Debian epoch format
local installed_major
# Remove epoch (e.g., "1:" from "1:17.0.6-9ubuntu1") and extract major version
local version_without_epoch="${installed_version#*:}"
installed_major=$(echo "$version_without_epoch" | grep -oE '^[0-9]+' || echo "0")
if dpkg --compare-versions "$installed_major" lt "$min_version" || dpkg --compare-versions "$installed_major" gt "$max_version"; then
echo "Warning: $pkg version in [$min_version, $max_version] is recommended. Installed version: $installed_version"
fi
}
run_quiet() {
local log="$LOG_DIR/$1"
shift
echo "Running command: $* > $log"
if ! "$@" > "$log" 2>&1; then
echo "Error: Command failed: $*"
echo "========== Output from $log =========="
cat "$log"
echo "======================================"
exit 1
fi
}
ensure_uv() {
if command -v uv > /dev/null 2>&1; then
echo "[*] uv already installed, version: $(uv --version | head -n 1)"
return
fi
echo "[*] Installing uv via standalone installer"
if command -v curl > /dev/null 2>&1; then
run_quiet uv-install.log bash -c 'curl -LsSf https://astral.sh/uv/install.sh | sh'
elif command -v wget > /dev/null 2>&1; then
run_quiet uv-install.log bash -c 'wget -qO- https://astral.sh/uv/install.sh | sh'
else
echo "Error: neither curl nor wget was found. Please install one of them and re-run."
exit 1
fi
local uv_bin_dir="${HOME}/.local/bin"
if [[ ":${PATH}:" != *":${uv_bin_dir}:"* ]]; then
export PATH="${uv_bin_dir}:${PATH}"
fi
if ! command -v uv > /dev/null 2>&1; then
echo "Error: uv installation failed. Ensure ${uv_bin_dir} is in your PATH."
exit 1
fi
echo "[*] uv installed successfully"
}
# Generate LLVM package names based on whether version is specified
if [[ -n "${LLVM_VERSION}" ]]; then
CLANG_PKG="clang-${LLVM_VERSION}"
LIBCLANG_PKG="libclang-${LLVM_VERSION}-dev"
LLVM_PKG="llvm-${LLVM_VERSION}"
LLVM_DEV_PKG="llvm-${LLVM_VERSION}-dev"
LLVM_CONFIG_EXE="llvm-config-${LLVM_VERSION}"
else
CLANG_PKG="clang"
LIBCLANG_PKG="libclang-dev"
LLVM_PKG="llvm"
LLVM_DEV_PKG="llvm-dev"
LLVM_CONFIG_EXE="llvm-config"
fi
apt_packages="\
build-essential git cmake ninja-build pkg-config python3 \
libspdlog-dev libboost-stacktrace-dev \
${CLANG_PKG} ${LIBCLANG_PKG} ${LLVM_PKG} ${LLVM_DEV_PKG} \
curl autoconf automake libtool bear"
need_apt_install=no
# shellcheck disable=SC2086
for apt_package in ${apt_packages}; do
if ! /usr/bin/dpkg-query --show "$apt_package" > /dev/null 2>&1; then
need_apt_install=yes
fi
done
if [[ "${need_apt_install}" == "yes" ]]; then
echo "[*] Installing system packages via apt"
run_quiet apt-get.log ${SUDO} apt-get update
# shellcheck disable=SC2086
DEBIAN_FRONTEND=noninteractive run_quiet apt-install.log ${SUDO} apt-get install -y --no-install-recommends ${apt_packages}
fi
# Check versions of installed LLVM packages
if [[ -n "${LLVM_VERSION}" ]]; then
check_version "${CLANG_PKG}" 17 19
check_version "${LIBCLANG_PKG}" 17 19
check_version "${LLVM_PKG}" 17 19
check_version "${LLVM_DEV_PKG}" 17 19
else
check_version clang 17 19
check_version libclang-dev 17 19
check_version llvm 17 19
check_version llvm-dev 17 19
fi
# --- Rust tool-chain (for c2rust & Maki) -------------------------------------
if ! command -v rustc > /dev/null 2>&1; then
echo "Error: rustc (Rust tool-chain) not found."
echo "Please install Rust by referring to https://www.rust-lang.org/tools/install"
echo "Then restart this script."
exit 1
else
echo "[*] rustc found, version: $(rustc --version)"
rust_version=$(rustc --version | awk '{print $2}')
rust_major=${rust_version%%.*}
rust_minor=$(echo "${rust_version}" | cut -d. -f2)
if [[ ${rust_major} -lt 1 || (${rust_major} -eq 1 && ${rust_minor} -lt 84) ]]; then
echo "Error: Rust 1.84+ required."
echo "Please run: rustup update stable"
exit 1
fi
fi
# --- C2Rust ------------------------------------------------------------------
if ! command -v c2rust > /dev/null 2>&1; then
echo "[*] Installing c2rust ${C2RUST_TAG}"
export LLVM_CONFIG_PATH="${LLVM_CONFIG_EXE}"
run_quiet c2rust-install.log cargo install --git "${C2RUST_GIT}" --tag "${C2RUST_TAG}" --locked c2rust
else
echo "[*] c2rust already installed, version: $(c2rust --version)"
fi
# --- Z3 ----------------------------------------------------------------------
# z3 takes forever to build, so install through z3-solver, the Python wrapper,
# which is published by z3 for each release.
ensure_uv
if uv tool list 2> /dev/null | grep -q "z3-solver ${Z3_VERSION}"; then
echo "[*] z3-solver ${Z3_VERSION} already installed via uv"
else
run_quiet z3-solver-install.log uv tool install --force z3-solver@${Z3_VERSION}
fi
git_clone_or_checkout "z3" "${Z3_GIT}" "${Z3_TAG}"
pushd z3 > /dev/null
echo "[*] Installing Z3 with z3-solver prebuilt"
mkdir -p build && cd build
run_quiet z3-cmake.log cmake -DCMAKE_BUILD_TYPE=Release -DZ3_BUILD_PYTHON_BINDINGS=OFF ..
# run_quiet z3-make.log make -j"$(nproc)"
# Copy `libz3.so` and `z3` from `z3-solver` to `build/` so that installation works.
ln -sf "$(uv tool dir)"/z3-solver/lib/python*/site-packages/z3/lib/libz3.so .
ln -sf libz3.so "libz3.so.$(echo "${Z3_VERSION}" | awk -F. '{print $1 "." $2}')" # ${major}.${minor}
ln -sf libz3.so "libz3.so.${Z3_VERSION}".0 # ${major}.${minor}.${patch}.0
ln -sf "$(uv tool dir)/z3-solver/bin/z3" .
run_quiet z3-install.log ${SUDO} cmake --install .
popd > /dev/null
# --- tree-sitter core --------------------------------------------------------
git_clone_or_checkout "tree-sitter" "${TS_GIT}" "${TS_TAG}"
echo "[*] Building tree-sitter core"
run_quiet tree-sitter-make.log make -C tree-sitter -j"$(nproc)"
# --- tree-sitter-c_preproc ---------------------------------------------------
if [[ "${USE_LATEST}" == true ]]; then
echo "[*] Fetching latest tree-sitter-c_preproc (main/HEAD)"
git_clone_or_checkout "tree-sitter-c_preproc" "${TSC_PREPROC_GIT}" "main"
else
git_clone_or_checkout "tree-sitter-c_preproc" "${TSC_PREPROC_GIT}" "${TSC_PREPROC_TAG}"
fi
echo "[*] Building tree-sitter-c_preproc"
run_quiet tsc-preproc-make.log make -C tree-sitter-c_preproc -j"$(nproc)"
# --- Maki --------------------------------------------------------------------
if [[ "${USE_LATEST}" == true ]]; then
echo "[*] Fetching latest Maki (main/HEAD)"
git_clone_or_checkout "Maki" "${MAKI_GIT}" "main"
else
git_clone_or_checkout "Maki" "${MAKI_GIT}" "${MAKI_TAG}"
fi
pushd Maki > /dev/null
echo "[*] Building Maki"
mkdir -p build && cd build
MAKI_CMAKE_ARGS=()
if [[ -n "${LLVM_VERSION}" ]]; then
LLVM_CMAKE_DIR=$("${LLVM_CONFIG_EXE}" --cmakedir)
MAKI_CMAKE_ARGS=(-DLLVM_DIR="${LLVM_CMAKE_DIR}" -DClang_DIR="${LLVM_CMAKE_DIR%/llvm}/clang")
fi
run_quiet maki-cmake.log cmake .. "${MAKI_CMAKE_ARGS[@]}"
run_quiet maki-make.log make -j"$(nproc)"
popd > /dev/null
# --- LibmCS ------------------------------------------------------------------
git_clone_or_checkout "libmcs" "${LIBMCS_GIT}" "${LIBMCS_TAG}"
pushd libmcs > /dev/null
if [[ ! -f lib/libmcs.a && ! -f build/libmcs.a ]]; then
echo "[*] Building LibmCS"
run_quiet libmcs-configure.log ./configure \
--cross-compile="" \
--compilation-flags="" \
--disable-denormal-handling \
--disable-long-double-procedures \
--disable-complex-procedures \
--little-endian
run_quiet libmcs-make.log make -j"$(nproc)"
fi
popd > /dev/null
# Check if ~/.cargo/bin is in PATH
echo "[*] Checking if \$HOME/.cargo/bin is in PATH"
if ! echo "$PATH" | grep -q "$HOME/.cargo/bin"; then
echo "=========================================================="
echo "Warning: \$HOME/.cargo/bin is not in your PATH."
echo "Please add the following line to your shell configuration file (e.g., ~/.bashrc or ~/.zshrc):"
echo "export PATH=\"\$HOME/.cargo/bin:\$PATH\""
echo "=========================================================="
fi
echo "=========================================================="
echo "All prerequisites installed."
echo "=========================================================="