-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
set_up_gcc_alternatives.sh
executable file
·120 lines (104 loc) · 4.1 KB
/
set_up_gcc_alternatives.sh
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env bash
# This file is part of eRCaGuy_dotfiles: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles
# Added to eRCaGuy_dotfiles by Gabriel Staples in 2023. Originally written by Liam Staskawicz
# (see below), and added to this repo with his permission. The purpose of this script is to make it
# easy to switch between various versions of gcc on your build machine.
#
# INSTALLATION:
# 1. Create a symlink in ~/bin to this script so you can run it from anywhere.
# cd /path/to/here
# mkdir -p ~/bin
# ln -si "${PWD}/set_up_gcc_alternatives.sh" ~/bin/set_up_gcc_alternatives # required
# ln -si "${PWD}/set_up_gcc_alternatives.sh" ~/bin/gs_set_up_gcc_alternatives # optional; replace "gs" with your initials
# . ~/.profile
# 2. Run it
# set_up_gcc_alternatives
# # or
# gs_set_up_gcc_alternatives
#
# TODO:
# 1. [ ] Don't do the full PPA installation and `update-alternatives --install` commands each time.
# Rather, do them *once*. Then, on subsequent calls, simply set the new version with `sudo
# update-alternatives --set` or whatever, and be done. You can either use a temporary flag file
# in /tmp to test for previous runs, or store a more-permanent flag file in the user's home dir or
# something. Also, if the user is calling to set the new gcc version to the currently-set gcc
# version, then of course there's nothing more to do, so just print a statement saying that, and
# exit.
# Originally By Liam Staskawicz
# 2022
# - https://github.com/liamstask
# - https://www.linkedin.com/in/liamstaskawcz/
#
# DESCRIPTION:
# Install multiple GCC versions, with the ability to switch which one is active system-wide via
# `update-alternatives` executable.
#
# USAGE:
# set_up_gcc_alternatives.sh <GCC_VERSION_TO_ACTIVATE>
#
# Currently-supported versions are 8, 9, 10, 11.
#
# EXAMPLES:
# set_up_gcc_alternatives.sh # set to gcc/g++ version 8
# set_up_gcc_alternatives.sh 8 # set to gcc/g++ version 8
# set_up_gcc_alternatives.sh 9 # set to gcc/g++ version 9
# set_up_gcc_alternatives.sh 10 # set to gcc/g++ version 10
# set_up_gcc_alternatives.sh 11 # set to gcc/g++ version 11
#
# REFERENCES:
# 1. Google search for "set up gcc alternatives" -
# https://www.google.com/search?q=set+up+gcc+alternatives&oq=set+up+gcc+alternatives&aqs=chrome..69i57.2997j0j9&sourceid=chrome&ie=UTF-8
# 1. https://linuxconfig.org/how-to-switch-between-multiple-gcc-and-g-compiler-versions-on-ubuntu-20-04-lts-focal-fossa
set -eu
# accept version from first arg, >>defaults to 8<<
ACTIVE_GCC_VERSION=${1:-8}
echo "**********************"
echo "* Current GCC version"
echo "**********************"
echo "gcc: $(gcc --version)"
echo "g++: $(g++ --version)"
echo "gcov: $(gcov --version)"
echo ""
echo "**********************"
echo "INSTALLING PPAS:"
echo "**********************"
echo ""
sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
sudo apt update -y
# Install gcc versions of interest
# 1. For earlier versions of Ubuntu
# apt install -y \
# build-essential \
# software-properties-common \
# manpages-dev \
# gcc-8 g++-8 \
# gcc-9 g++-9 \
# gcc-10 g++-10 \
# gcc-11 g++-11
# 2. For Ubuntu 22.04
# NB: gcc-8/g++-8 can only be installed on Ubuntu 22.04 if done manually. See:
# https://askubuntu.com/a/1446899/327339
# So, let's remove that one. YOU MUST GO MANUALLY INSTALL gcc-8/g++-8 per the answer above!
sudo apt install -y \
build-essential \
software-properties-common \
manpages-dev \
gcc-9 g++-9 \
gcc-10 g++-10 \
gcc-11 g++-11
for i in {8..11}
do
sudo update-alternatives \
--install /usr/bin/gcc gcc /usr/bin/gcc-"$i" "$i" \
--slave /usr/bin/g++ g++ /usr/bin/g++-"$i" \
--slave /usr/bin/gcov gcov /usr/bin/gcov-"$i"
done
# set active version - select a different version as needed
sudo update-alternatives --set gcc "$(update-alternatives --list gcc | grep gcc-$ACTIVE_GCC_VERSION)"
echo ""
echo "**********************"
echo "* Selected GCC version"
echo "**********************"
echo "gcc: $(gcc --version)"
echo "g++: $(g++ --version)"
echo "gcov: $(gcov --version)"