|
| 1 | +#!/bin/bash |
| 2 | +################################################################################ |
| 3 | +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +# See https://llvm.org/LICENSE.txt for license information. |
| 5 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +################################################################################ |
| 7 | +# |
| 8 | +# This script will install the llvm toolchain on the different |
| 9 | +# Debian and Ubuntu versions |
| 10 | + |
| 11 | +set -eux |
| 12 | + |
| 13 | +usage() { |
| 14 | + set +x |
| 15 | + echo "Usage: $0 [llvm_major_version] [all] [OPTIONS]" 1>&2 |
| 16 | + echo -e "all\t\t\tInstall all packages." 1>&2 |
| 17 | + echo -e "-n=code_name\t\tSpecifies the distro codename, for example bionic" 1>&2 |
| 18 | + echo -e "-h\t\t\tPrints this help." 1>&2 |
| 19 | + echo -e "-m=repo_base_url\tSpecifies the base URL from which to download." 1>&2 |
| 20 | + exit 1; |
| 21 | +} |
| 22 | + |
| 23 | +CURRENT_LLVM_STABLE=19 |
| 24 | +BASE_URL="http://apt.llvm.org" |
| 25 | + |
| 26 | +NEW_DEBIAN_DISTROS=("trixie" "unstable") |
| 27 | +# Set default values for commandline arguments |
| 28 | +# We default to the current stable branch of LLVM |
| 29 | +LLVM_VERSION=$CURRENT_LLVM_STABLE |
| 30 | +ALL=0 |
| 31 | +DISTRO=$(lsb_release -is) |
| 32 | +VERSION_CODENAME=$(lsb_release -cs) |
| 33 | +VERSION=$(lsb_release -sr) |
| 34 | +UBUNTU_CODENAME="" |
| 35 | +CODENAME_FROM_ARGUMENTS="" |
| 36 | +# Obtain VERSION_CODENAME and UBUNTU_CODENAME (for Ubuntu and its derivatives) |
| 37 | +source /etc/os-release |
| 38 | +DISTRO=${DISTRO,,} |
| 39 | + |
| 40 | +# Check for required tools |
| 41 | + |
| 42 | +# Check if this is a new Debian distro |
| 43 | +is_new_debian=0 |
| 44 | +if [[ "${DISTRO}" == "debian" ]]; then |
| 45 | + for new_distro in "${NEW_DEBIAN_DISTROS[@]}"; do |
| 46 | + if [[ "${VERSION_CODENAME}" == "${new_distro}" ]]; then |
| 47 | + is_new_debian=1 |
| 48 | + break |
| 49 | + fi |
| 50 | + done |
| 51 | +fi |
| 52 | + |
| 53 | +# Check for required tools |
| 54 | +needed_binaries=(lsb_release wget gpg) |
| 55 | +# add-apt-repository is not needed for newer Debian distros |
| 56 | +if [[ $is_new_debian -eq 0 ]]; then |
| 57 | + needed_binaries+=(add-apt-repository) |
| 58 | +fi |
| 59 | + |
| 60 | +missing_binaries=() |
| 61 | +using_curl= |
| 62 | +for binary in "${needed_binaries[@]}"; do |
| 63 | + if ! command -v $binary &>/dev/null ; then |
| 64 | + if [[ "$binary" == "wget" ]] && command -v curl &>/dev/null; then |
| 65 | + using_curl=1 |
| 66 | + continue |
| 67 | + fi |
| 68 | + missing_binaries+=($binary) |
| 69 | + fi |
| 70 | +done |
| 71 | + |
| 72 | +if [[ ${#missing_binaries[@]} -gt 0 ]] ; then |
| 73 | + echo "You are missing some tools this script requires: ${missing_binaries[@]}" |
| 74 | + echo "(hint: apt install lsb-release wget software-properties-common gnupg)" |
| 75 | + echo "curl is also supported" |
| 76 | + exit 4 |
| 77 | +fi |
| 78 | + |
| 79 | +case ${DISTRO} in |
| 80 | + debian) |
| 81 | + # Debian Trixie has a workaround because of |
| 82 | + # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1038383 |
| 83 | + if [[ "${VERSION}" == "unstable" ]] || [[ "${VERSION}" == "testing" ]] || [[ "${VERSION_CODENAME}" == "trixie" ]]; then |
| 84 | + CODENAME=unstable |
| 85 | + LINKNAME= |
| 86 | + else |
| 87 | + # "stable" Debian release |
| 88 | + CODENAME=${VERSION_CODENAME} |
| 89 | + LINKNAME=-${CODENAME} |
| 90 | + fi |
| 91 | + ;; |
| 92 | + *) |
| 93 | + # ubuntu and its derivatives |
| 94 | + if [[ -n "${UBUNTU_CODENAME}" ]]; then |
| 95 | + CODENAME=${UBUNTU_CODENAME} |
| 96 | + if [[ -n "${CODENAME}" ]]; then |
| 97 | + LINKNAME=-${CODENAME} |
| 98 | + fi |
| 99 | + fi |
| 100 | + ;; |
| 101 | +esac |
| 102 | + |
| 103 | +# read optional command line arguments |
| 104 | +if [ "$#" -ge 1 ] && [ "${1::1}" != "-" ]; then |
| 105 | + if [ "$1" != "all" ]; then |
| 106 | + LLVM_VERSION=$1 |
| 107 | + else |
| 108 | + # special case for ./llvm.sh all |
| 109 | + ALL=1 |
| 110 | + fi |
| 111 | + OPTIND=2 |
| 112 | + if [ "$#" -ge 2 ]; then |
| 113 | + if [ "$2" == "all" ]; then |
| 114 | + # Install all packages |
| 115 | + ALL=1 |
| 116 | + OPTIND=3 |
| 117 | + fi |
| 118 | + fi |
| 119 | +fi |
| 120 | + |
| 121 | +while getopts ":hm:n:" arg; do |
| 122 | + case $arg in |
| 123 | + h) |
| 124 | + usage |
| 125 | + ;; |
| 126 | + m) |
| 127 | + BASE_URL=${OPTARG} |
| 128 | + ;; |
| 129 | + n) |
| 130 | + CODENAME=${OPTARG} |
| 131 | + if [[ "${CODENAME}" == "unstable" ]]; then |
| 132 | + # link name does not apply to unstable repository |
| 133 | + LINKNAME= |
| 134 | + else |
| 135 | + LINKNAME=-${CODENAME} |
| 136 | + fi |
| 137 | + CODENAME_FROM_ARGUMENTS="true" |
| 138 | + ;; |
| 139 | + esac |
| 140 | +done |
| 141 | + |
| 142 | +if [[ $EUID -ne 0 ]]; then |
| 143 | + echo "This script must be run as root!" |
| 144 | + exit 1 |
| 145 | +fi |
| 146 | + |
| 147 | +declare -A LLVM_VERSION_PATTERNS |
| 148 | +LLVM_VERSION_PATTERNS[9]="-9" |
| 149 | +LLVM_VERSION_PATTERNS[10]="-10" |
| 150 | +LLVM_VERSION_PATTERNS[11]="-11" |
| 151 | +LLVM_VERSION_PATTERNS[12]="-12" |
| 152 | +LLVM_VERSION_PATTERNS[13]="-13" |
| 153 | +LLVM_VERSION_PATTERNS[14]="-14" |
| 154 | +LLVM_VERSION_PATTERNS[15]="-15" |
| 155 | +LLVM_VERSION_PATTERNS[16]="-16" |
| 156 | +LLVM_VERSION_PATTERNS[17]="-17" |
| 157 | +LLVM_VERSION_PATTERNS[18]="-18" |
| 158 | +LLVM_VERSION_PATTERNS[19]="-19" |
| 159 | +LLVM_VERSION_PATTERNS[20]="-20" |
| 160 | +LLVM_VERSION_PATTERNS[21]="" |
| 161 | + |
| 162 | +if [ ! ${LLVM_VERSION_PATTERNS[$LLVM_VERSION]+_} ]; then |
| 163 | + echo "This script does not support LLVM version $LLVM_VERSION" |
| 164 | + exit 3 |
| 165 | +fi |
| 166 | + |
| 167 | +LLVM_VERSION_STRING=${LLVM_VERSION_PATTERNS[$LLVM_VERSION]} |
| 168 | + |
| 169 | +# join the repository name |
| 170 | +if [[ -n "${CODENAME}" ]]; then |
| 171 | + REPO_NAME="deb ${BASE_URL}/${CODENAME}/ llvm-toolchain${LINKNAME}${LLVM_VERSION_STRING} main" |
| 172 | + # check if the repository exists for the distro and version |
| 173 | + if ! wget -q --method=HEAD ${BASE_URL}/${CODENAME} &> /dev/null && \ |
| 174 | + ! curl -sSLI -XHEAD ${BASE_URL}/${CODENAME} &> /dev/null; then |
| 175 | + if [[ -n "${CODENAME_FROM_ARGUMENTS}" ]]; then |
| 176 | + echo "Specified codename '${CODENAME}' is not supported by this script." |
| 177 | + else |
| 178 | + echo "Distribution '${DISTRO}' in version '${VERSION}' is not supported by this script." |
| 179 | + fi |
| 180 | + exit 2 |
| 181 | + fi |
| 182 | +fi |
| 183 | + |
| 184 | + |
| 185 | +# install everything |
| 186 | + |
| 187 | +if [[ ! -f /etc/apt/trusted.gpg.d/apt.llvm.org.asc ]]; then |
| 188 | + # download GPG key once |
| 189 | + if [[ -z "$using_curl" ]]; then |
| 190 | + wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc |
| 191 | + else |
| 192 | + curl -sSL https://apt.llvm.org/llvm-snapshot.gpg.key | tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc |
| 193 | + fi |
| 194 | +fi |
| 195 | + |
| 196 | +if [[ -z "`apt-key list 2> /dev/null | grep -i llvm`" ]]; then |
| 197 | + # Delete the key in the old format |
| 198 | + apt-key del AF4F7421 || true |
| 199 | +fi |
| 200 | + |
| 201 | + |
| 202 | +# Add repository based on distribution |
| 203 | +if [[ "${VERSION_CODENAME}" == "bookworm" ]]; then |
| 204 | + # add it twice to workaround: |
| 205 | + # https://github.com/llvm/llvm-project/issues/62475 |
| 206 | + add-apt-repository -y "${REPO_NAME}" |
| 207 | + add-apt-repository -y "${REPO_NAME}" |
| 208 | +elif [[ $is_new_debian -eq 1 ]]; then |
| 209 | + # workaround missing add-apt-repository in newer Debian and use new source.list format |
| 210 | + SOURCES_FILE="/etc/apt/sources.list.d/http_apt_llvm_org_${CODENAME}_-${VERSION_CODENAME}.sources" |
| 211 | + TEXT_TO_ADD="Types: deb |
| 212 | +Architectures: amd64 arm64 |
| 213 | +Signed-By: /etc/apt/trusted.gpg.d/apt.llvm.org.asc |
| 214 | +URIs: ${BASE_URL}/${CODENAME}/ |
| 215 | +Suites: llvm-toolchain${LINKNAME}${LLVM_VERSION_STRING} |
| 216 | +Components: main" |
| 217 | + echo "$TEXT_TO_ADD" | tee -a "$SOURCES_FILE" > /dev/null |
| 218 | +else |
| 219 | + add-apt-repository -y "${REPO_NAME}" |
| 220 | +fi |
| 221 | + |
| 222 | +apt-get update |
| 223 | +PKG="clang-$LLVM_VERSION lldb-$LLVM_VERSION lld-$LLVM_VERSION clangd-$LLVM_VERSION" |
| 224 | +if [[ $ALL -eq 1 ]]; then |
| 225 | + # same as in test-install.sh |
| 226 | + # No worries if we have dups |
| 227 | + PKG="$PKG clang-tidy-$LLVM_VERSION clang-format-$LLVM_VERSION clang-tools-$LLVM_VERSION llvm-$LLVM_VERSION-dev lld-$LLVM_VERSION lldb-$LLVM_VERSION llvm-$LLVM_VERSION-tools libomp-$LLVM_VERSION-dev libc++-$LLVM_VERSION-dev libc++abi-$LLVM_VERSION-dev libclang-common-$LLVM_VERSION-dev libclang-$LLVM_VERSION-dev libclang-cpp$LLVM_VERSION-dev liblldb-$LLVM_VERSION-dev libunwind-$LLVM_VERSION-dev" |
| 228 | + if test $LLVM_VERSION -gt 14; then |
| 229 | + PKG="$PKG libclang-rt-$LLVM_VERSION-dev libpolly-$LLVM_VERSION-dev" |
| 230 | + fi |
| 231 | +fi |
| 232 | +apt-get install -y $PKG |
0 commit comments