-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall_llvm
More file actions
executable file
·96 lines (83 loc) · 4.44 KB
/
install_llvm
File metadata and controls
executable file
·96 lines (83 loc) · 4.44 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
#!/usr/bin/env bash
# Usage: bash install_llvm
# N.B.: VERSION_NUMBER is used only on Linux. On other platforms, the latest version will be installed.
# The script will determine the version number based on the current date and the LLVM release
# schedule, which is updated every six months as of 2026, in March and September. The version number
# in September 2025 is 21.
set -euo pipefail
echo OSTYPE: "$OSTYPE"
# N.B.: Do not quote darwin* etc., or the pattern will be interpreted as a literal string without
# expanding the wildcard *. See https://www.gnu.org/software/bash/manual/bash.html#Double-Quotes-1 :
# Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within
# the quotes, with the exception of ‘$’, ‘`’, ‘\’, and, when history expansion is enabled, ‘!’.
if [[ "$OSTYPE" == darwin* ]] ; then
echo "We are on macOS."
brew update || true
# clang is included in llvm, but flang and lld are separate packages.
brew install llvm flang lld # The latest version will be installed, which is 21 in September 2025.
elif [[ "$OSTYPE" == cygwin* || "$OSTYPE" == win* || "$OSTYPE" == msys* ]]; then
echo "We are on Windows."
choco upgrade chocolatey -y --no-progress || true
# flang, clang, and lld are supposed to be included in llvm.
choco install llvm -y --no-progress
choco upgrade llvm -y --no-progress # Update to the latest version
else # Assume Ubuntu
echo "We assume that we are on Ubuntu."
# Install prerequisites.
sudo apt update || true && sudo apt install -y curl gnupg
# According to "LLVM Release Schedule" at https://llvm.org, we assume that the version is
# updated every six months, in March and September. The version number in September 2025 is 21.
CURRENT_YEAR=$(date +%Y)
CURRENT_MONTH=$(date +%m)
CURRENT_MONTH=${CURRENT_MONTH#0} # Remove leading zero if any
echo "Current year: ${CURRENT_YEAR}, current month: ${CURRENT_MONTH}"
if [[ ${CURRENT_MONTH} -ge 9 ]]; then
VERSION_NUMBER=$((21 + (CURRENT_YEAR - 2025) * 2))
elif [[ ${CURRENT_MONTH} -ge 3 ]]; then
VERSION_NUMBER=$((21 + (CURRENT_YEAR - 2025) * 2 - 1))
else
VERSION_NUMBER=$((21 + (CURRENT_YEAR - 2026) * 2 ))
fi
echo "Using the version number ${VERSION_NUMBER} based on the current date."
# Add the LLVM repository and the GPG key.
# See https://apt.llvm.org/
UBUNTU_CODENAME=$( (lsb_release -sc 2>/dev/null || (. /etc/os-release && echo "$VERSION_CODENAME")) ) # e.g., "noble" for Ubuntu 24.04
curl -fsSL https://apt.llvm.org/llvm-snapshot.gpg.key \
| gpg --dearmor --yes \
| sudo tee /usr/share/keyrings/apt.llvm.org.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/apt.llvm.org.gpg] https://apt.llvm.org/${UBUNTU_CODENAME}/ llvm-toolchain-${UBUNTU_CODENAME}-${VERSION_NUMBER} main" \
| sudo tee /etc/apt/sources.list.d/llvm-toolchain-${VERSION_NUMBER}.list
# Install the packages (clang is needed even if we use only flang).
sudo apt update
sudo apt install -y llvm-${VERSION_NUMBER} flang-${VERSION_NUMBER} clang-${VERSION_NUMBER} lld-${VERSION_NUMBER}
# Verify the installation
for TOOL in flang clang lld; do
type "${TOOL}-${VERSION_NUMBER}" > /dev/null 2>&1 || \
{ echo "${TOOL}-${VERSION_NUMBER} not found after the installation, which probably failed." >&2 ; exit 1; }
done
# Set up symlinks pointing to tool-${VERSION_NUMBER}
for TOOL in flang clang lld; do
BIN_DIR="/usr/local/bin" # This usually comes before /usr/bin in PATH
if [[ -f "${BIN_DIR}/${TOOL}" || -L "${BIN_DIR}/${TOOL}" ]]; then
echo "${TOOL} already exists in ${BIN_DIR}:"
ls -al "${BIN_DIR}/${TOOL}"
echo "Backing ${TOOL} to ${TOOL}.bak"
sudo mv "${BIN_DIR}/${TOOL}" "${BIN_DIR}/${TOOL}.bak"
fi
echo "Creating a symbolic link named ${TOOL} pointing to ${TOOL}-${VERSION_NUMBER} in ${BIN_DIR}."
sudo ln -sf "$(command -v ${TOOL}-${VERSION_NUMBER})" "${BIN_DIR}/${TOOL}"
done
fi
# Verify installation
for TOOL in flang clang lld; do
type $TOOL > /dev/null 2>&1 || \
{ echo "$TOOL not found after the installation, which probably failed." >&2 ; exit 1; }
done
# Final messages
echo "LLVM installation completed."
for TOOL in flang clang lld; do
echo -e "\nThe path to $TOOL is:\n"
command -v "$TOOL"
echo -e "\nThe $TOOL version is:\n"
"$TOOL" --version || true
done