Created
September 22, 2024 16:13
-
-
Save mlabbe/f97338b7c6032aed2a5d92cd9b2c5c9b to your computer and use it in GitHub Desktop.
add llvm repo to ubuntu / linux mint and symlink llvm binaries, making it the default compiler
This file contains 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
#!/usr/bin/perl -w | |
use strict; | |
# update this whe n new versions of llvm come out | |
my $llvm_version = 19; | |
# mint has a file that lets you know the ubuntu upstream release version | |
sub get_dist_version_from_mint { | |
open my $fh, '<', '/etc/upstream-release/lsb-release' or die; | |
my $codename = ''; | |
while (my $line = <$fh>) { | |
if ($line =~ /^DISTRIB_CODENAME=(\S+)/) { | |
$codename = $1; | |
} | |
} | |
close $fh; | |
return $codename; | |
} | |
sub read_os_release { | |
my $os_release_file = '/etc/os-release'; | |
my %os_info; | |
if (-e $os_release_file) { | |
open my $fh, '<', $os_release_file or die "Could not open '$os_release_file': $!"; | |
while (my $line = <$fh>) { | |
chomp $line; # Remove newline character | |
if ($line =~ /^(.*)=(.*)$/) { | |
my $key = $1; | |
my $value = $2; | |
# Remove surrounding quotes from the value | |
$value =~ s/^"(.*)"$/$1/; | |
$value =~ s/^'(.*)'$/$1/; | |
$os_info{$key} = $value; | |
} | |
} | |
close $fh; | |
} | |
return %os_info; | |
} | |
sub run_cmd { | |
my $cmd = shift; | |
print("> $cmd\n"); | |
return system($cmd) == 0; | |
} | |
sub apt_source { | |
my $llvm_version = shift; | |
my $dist_version = shift; | |
return "deb http://apt.llvm.org/$dist_version/ llvm-toolchain-$dist_version-$llvm_version main"; | |
} | |
# debian alternative priority | |
my $priority = 100; | |
my %os_release = read_os_release(); | |
my $dist_version = $os_release{UBUNTU_CODENAME}; | |
die $dist_version; | |
run_cmd("wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add -"); | |
# attempt removal of old apt source, may silently fail | |
my $old_apt_source = apt_source($llvm_version - 1, $dist_version); | |
run_cmd("add-apt-repository --remove \"$old_apt_source\""); | |
my $new_apt_source = apt_source($llvm_version, $dist_version); | |
run_cmd("add-apt-repository \"$new_apt_source\""); | |
run_cmd("apt update"); | |
my @packages = qw(clang lldb lld llvm clang-format clang-tidy clang-tools); | |
my $cmd = "apt -y install "; | |
foreach (@packages) { | |
my $pkg_name = $_ . "-" .$llvm_version; | |
$cmd .= "$pkg_name "; | |
} | |
run_cmd($cmd); | |
my @programs = qw(clang clang++ clang-tidy clang-format scan-build lldb lld llvm-config); | |
# remove existing alternatives | |
foreach (@programs) { | |
run_cmd("update-alternatives --remove-all $_"); | |
} | |
# now install alternatives | |
foreach (@programs) { | |
my $pkg_name = $_ . "-" .$llvm_version; | |
run_cmd("update-alternatives --install /usr/bin/$_ $_ /usr/bin/$pkg_name $priority"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment