Skip to content

Instantly share code, notes, and snippets.

@stefanct
Forked from gmas/gist:b5fd3d038905d1265485
Last active November 7, 2019 17:26
Show Gist options
  • Save stefanct/f1240dfc0ec7a2c56201e8a789502c90 to your computer and use it in GitHub Desktop.
Save stefanct/f1240dfc0ec7a2c56201e8a789502c90 to your computer and use it in GitHub Desktop.
switch gcc versions in debian
#!/bin/sh
versions="6 8"
basetools="cpp gcc-ar gcc-nm gcc-ranlib gcov gcov-dump gcov-tool"
opttools= # "g++ gfortran"
platform="arm-linux-gnueabihf" # FIXME: make it work with "", and multiple platforms
# Test for root rights
if [ $(id -u) -ne 0 ]; then
echo "This script must be run as root.">&2
exit 1
fi
# Create list of all needed packages (e.g., gcc-6-arm-linux-gnueabi)
packages=
for v in $versions ; do
for t in gcc $opttools ; do
packages="$packages $t-$v-$platform"
done
done
# Ensure respective versions of the tools are installed
echo "Installing $packages"
apt install $packages
# Remove existing alternatives
for t in gcc $opttools ; do
update-alternatives --remove-all $t 2>/dev/null
done
# Add everything to the update-alternatives db
for v in $versions ; do
prio=$(expr $v \* 10)
slaves=
for t in $opttools $basetools ; do
slaves="$slaves --slave /usr/bin/$platform-$t $platform-$t /usr/bin/$platform-$t-$v"
done
update-alternatives --install /usr/bin/$platform-gcc $platform-gcc /usr/bin/$platform-gcc-$v $prio $slaves
done
# Choose the newest version by default
update-alternatives --auto $platform-gcc
echo "$platform-gcc has been set to $(realpath $(which $platform-gcc))"
echo "Use \"sudo update-alternatives --config $platform-gcc\" to change it."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment