Created
October 11, 2021 09:59
-
-
Save CJKay/f4789cb8c2030b85b467e0ca0be39a6e to your computer and use it in GitHub Desktop.
Update LLVM Alternatives
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
packages=("clang" "lldb" "lld" "clangd") | |
for package in ${packages[@]}; do | |
versions=$(apt-cache search --names-only "^${package}-[0-9]+$" | cut -d' ' -f1 | rev | cut -d'-' -f1 | rev) | |
for version in ${versions}; do | |
if ! dpkg -l "${package}-${version}" > /dev/null 2>&1; then | |
continue | |
fi | |
master= | |
slaves=() | |
files=$(dpkg -L "${package}-${version}") | |
for file in ${files}; do | |
if ! [ -f "${file}" ]; then | |
continue | |
fi | |
dirname=$(dirname ${file}) | |
basename=$(basename ${file}) | |
realname=${basename//-${version}/} | |
if [[ ":$PATH:" != *":${dirname}:"* ]] && [[ "${dirname}" != "/usr/share/man/"* ]]; then | |
continue | |
fi | |
if [ "${realname}" = "${package}" ]; then | |
master=${file} | |
else | |
slaves=(${slaves[@]} "${file}") | |
fi | |
done | |
if [ -z "${master}" ]; then | |
>&2 echo "No master identified for package: ${package}-${version}" | |
>&2 echo "Please update these alternatives manually: ${slaves}" | |
continue | |
fi | |
priority=$((100 * ${version})) | |
master_link=${master//-${version}/} | |
master_alternative=${master} | |
master_name=$(basename "${master_link}") | |
command=("sudo" "update-alternatives" "--install" "${master_link}" "${master_name}" "${master_alternative}" "${priority}") | |
for slave in ${slaves[@]}; do | |
slave_link=${slave//-${version}/} | |
slave_alternative=${slave} | |
slave_name=$(basename "${slave_link}") | |
command=(${command[@]} "--slave" "${slave_link}" "${slave_name}" "${slave_alternative}") | |
done | |
${command[@]} | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script is intended for consumers of LLVM's Debian/Ubuntu automatic installer script, which can be found here. It identifies the list of installed versioned LLVM packages (i.e.
clang-<version>
,lldb-<version>
,lld-<version>
andclangd-<version>
) and usesupdate-alternatives
to create switchable symbolic links to the packages' binaries and manpages.The master/slave relationship between the main package binary and its dependants is maintained when creating alternatives. For example, where
clang
is the package,/usr/bin/clang
is the master binary, and/usr/bin/clang++
is a slave binary. Doing this ensures that the binaries from each package are switched together rather than independently: