|
| 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=15 |
| 24 | +BASE_URL="http://apt.llvm.org" |
| 25 | + |
| 26 | +# Check for required tools |
| 27 | +needed_binaries=(lsb_release wget add-apt-repository gpg) |
| 28 | +missing_binaries=() |
| 29 | +for binary in "${needed_binaries[@]}"; do |
| 30 | + if ! which $binary &>/dev/null ; then |
| 31 | + missing_binaries+=($binary) |
| 32 | + fi |
| 33 | +done |
| 34 | +if [[ ${#missing_binaries[@]} -gt 0 ]] ; then |
| 35 | + echo "You are missing some tools this script requires: ${missing_binaries[@]}" |
| 36 | + echo "(hint: apt install lsb-release wget software-properties-common gnupg)" |
| 37 | + exit 4 |
| 38 | +fi |
| 39 | + |
| 40 | +# Set default values for commandline arguments |
| 41 | +# We default to the current stable branch of LLVM |
| 42 | +LLVM_VERSION=$CURRENT_LLVM_STABLE |
| 43 | +ALL=0 |
| 44 | +DISTRO=$(lsb_release -is) |
| 45 | +VERSION=$(lsb_release -sr) |
| 46 | +UBUNTU_CODENAME="" |
| 47 | +CODENAME_FROM_ARGUMENTS="" |
| 48 | +# Obtain VERSION_CODENAME and UBUNTU_CODENAME (for Ubuntu and its derivatives) |
| 49 | +source /etc/os-release |
| 50 | +DISTRO=${DISTRO,,} |
| 51 | +case ${DISTRO} in |
| 52 | + debian) |
| 53 | + if [[ "${VERSION}" == "unstable" ]] || [[ "${VERSION}" == "testing" ]] || [[ "${VERSION_CODENAME}" == "bookworm" ]]; then |
| 54 | + # For now, bookworm == sid. |
| 55 | + # TODO change when bookworm is released |
| 56 | + CODENAME=unstable |
| 57 | + LINKNAME= |
| 58 | + else |
| 59 | + # "stable" Debian release |
| 60 | + CODENAME=${VERSION_CODENAME} |
| 61 | + LINKNAME=-${CODENAME} |
| 62 | + fi |
| 63 | + ;; |
| 64 | + *) |
| 65 | + # ubuntu and its derivatives |
| 66 | + if [[ -n "${UBUNTU_CODENAME}" ]]; then |
| 67 | + CODENAME=${UBUNTU_CODENAME} |
| 68 | + if [[ -n "${CODENAME}" ]]; then |
| 69 | + LINKNAME=-${CODENAME} |
| 70 | + fi |
| 71 | + fi |
| 72 | + ;; |
| 73 | +esac |
| 74 | + |
| 75 | +# read optional command line arguments |
| 76 | +if [ "$#" -ge 1 ] && [ "${1::1}" != "-" ]; then |
| 77 | + if [ "$1" != "all" ]; then |
| 78 | + LLVM_VERSION=$1 |
| 79 | + else |
| 80 | + # special case for ./llvm.sh all |
| 81 | + ALL=1 |
| 82 | + fi |
| 83 | + OPTIND=2 |
| 84 | + if [ "$#" -ge 2 ]; then |
| 85 | + if [ "$2" == "all" ]; then |
| 86 | + # Install all packages |
| 87 | + ALL=1 |
| 88 | + OPTIND=3 |
| 89 | + fi |
| 90 | + fi |
| 91 | +fi |
| 92 | + |
| 93 | +while getopts ":hm:n:" arg; do |
| 94 | + case $arg in |
| 95 | + h) |
| 96 | + usage |
| 97 | + ;; |
| 98 | + m) |
| 99 | + BASE_URL=${OPTARG} |
| 100 | + ;; |
| 101 | + n) |
| 102 | + CODENAME=${OPTARG} |
| 103 | + if [[ "${CODENAME}" == "unstable" ]]; then |
| 104 | + # link name does not apply to unstable repository |
| 105 | + LINKNAME= |
| 106 | + else |
| 107 | + LINKNAME=-${CODENAME} |
| 108 | + fi |
| 109 | + CODENAME_FROM_ARGUMENTS="true" |
| 110 | + ;; |
| 111 | + esac |
| 112 | +done |
| 113 | + |
| 114 | +if [[ $EUID -ne 0 ]]; then |
| 115 | + echo "This script must be run as root!" |
| 116 | + exit 1 |
| 117 | +fi |
| 118 | + |
| 119 | +declare -A LLVM_VERSION_PATTERNS |
| 120 | +LLVM_VERSION_PATTERNS[9]="-9" |
| 121 | +LLVM_VERSION_PATTERNS[10]="-10" |
| 122 | +LLVM_VERSION_PATTERNS[11]="-11" |
| 123 | +LLVM_VERSION_PATTERNS[12]="-12" |
| 124 | +LLVM_VERSION_PATTERNS[13]="-13" |
| 125 | +LLVM_VERSION_PATTERNS[14]="-14" |
| 126 | +LLVM_VERSION_PATTERNS[15]="-15" |
| 127 | +LLVM_VERSION_PATTERNS[16]="-16" |
| 128 | +LLVM_VERSION_PATTERNS[17]="" |
| 129 | + |
| 130 | +if [ ! ${LLVM_VERSION_PATTERNS[$LLVM_VERSION]+_} ]; then |
| 131 | + echo "This script does not support LLVM version $LLVM_VERSION" |
| 132 | + exit 3 |
| 133 | +fi |
| 134 | + |
| 135 | +LLVM_VERSION_STRING=${LLVM_VERSION_PATTERNS[$LLVM_VERSION]} |
| 136 | + |
| 137 | +# join the repository name |
| 138 | +if [[ -n "${CODENAME}" ]]; then |
| 139 | + REPO_NAME="deb ${BASE_URL}/${CODENAME}/ llvm-toolchain${LINKNAME}${LLVM_VERSION_STRING} main" |
| 140 | + |
| 141 | + # check if the repository exists for the distro and version |
| 142 | + if ! wget -q --method=HEAD ${BASE_URL}/${CODENAME} &> /dev/null; then |
| 143 | + if [[ -n "${CODENAME_FROM_ARGUMENTS}" ]]; then |
| 144 | + echo "Specified codename '${CODENAME}' is not supported by this script." |
| 145 | + else |
| 146 | + echo "Distribution '${DISTRO}' in version '${VERSION}' is not supported by this script." |
| 147 | + fi |
| 148 | + exit 2 |
| 149 | + fi |
| 150 | +fi |
| 151 | + |
| 152 | + |
| 153 | +# install everything |
| 154 | + |
| 155 | +if [[ ! -f /etc/apt/trusted.gpg.d/apt.llvm.org.asc ]]; then |
| 156 | + # download GPG key once |
| 157 | + wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc |
| 158 | +fi |
| 159 | + |
| 160 | +if [[ -z "`apt-key list 2> /dev/null | grep -i llvm`" ]]; then |
| 161 | + # Delete the key in the old format |
| 162 | + apt-key del AF4F7421 |
| 163 | +fi |
| 164 | + |
| 165 | +# first install the ubuntu base so we can establish the paths to clang |
| 166 | +apt-get install -y |
| 167 | + |
| 168 | +add-apt-repository "${REPO_NAME}" |
| 169 | +apt-get update |
| 170 | +PKG="clang-$LLVM_VERSION lldb-$LLVM_VERSION lld-$LLVM_VERSION clangd-$LLVM_VERSION" |
| 171 | +if [[ $ALL -eq 1 ]]; then |
| 172 | + # same as in test-install.sh |
| 173 | + # No worries if we have dups |
| 174 | + PKG="$PKG clang-tidy-$LLVM_VERSION" |
| 175 | + PKG="$PKG clang-format-$LLVM_VERSION" |
| 176 | + PKG="$PKG clang-tools-$LLVM_VERSION" |
| 177 | + PKG="$PKG llvm-$LLVM_VERSION-dev" |
| 178 | + PKG="$PKG lld-$LLVM_VERSION" |
| 179 | + PKG="$PKG lldb-$LLVM_VERSION" |
| 180 | + PKG="$PKG llvm-$LLVM_VERSION-tools" |
| 181 | + PKG="$PKG libomp-$LLVM_VERSION-dev" |
| 182 | + PKG="$PKG libc++-$LLVM_VERSION-dev" |
| 183 | + PKG="$PKG libc++abi-$LLVM_VERSION-dev" |
| 184 | + PKG="$PKG libclang-common-$LLVM_VERSION-dev" |
| 185 | + PKG="$PKG libclang-$LLVM_VERSION-dev" |
| 186 | + PKG="$PKG libclang-cpp$LLVM_VERSION-dev" |
| 187 | + PKG="$PKG libunwind-$LLVM_VERSION-dev" |
| 188 | + if test $LLVM_VERSION -gt 14; then |
| 189 | + PKG="$PKG libclang-rt-$LLVM_VERSION-dev libpolly-$LLVM_VERSION-dev" |
| 190 | + fi |
| 191 | +fi |
| 192 | +apt-get install -y $PKG |
0 commit comments