Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,18 @@ public async Task CompilerInstallationRunsTheExpectedWorkloadCommandInLinuxForGc
ProcessStartInfo expectedInfo = new ProcessStartInfo();
List<string> expectedCommands = new List<string>()
{
"sudo update-alternatives --remove-all gcc",
"sudo update-alternatives --remove-all gfortran",
"sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y",
"sudo apt update",
"sudo apt install build-essential gcc-123 g++-123 gfortran-123 -y --quiet",
"sudo update-alternatives --remove-all gcc",
"sudo update-alternatives --remove-all cc",
"sudo update-alternatives --remove-all g++",
"sudo update-alternatives --remove-all gcov",
"sudo update-alternatives --remove-all gcc-ar",
"sudo update-alternatives --remove-all gcc-ranlib",
"sudo update-alternatives --remove-all gfortran",
"sudo update-alternatives --remove-all cpp",
"sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-123 1230 " +
$"--slave /usr/bin/g++ g++ /usr/bin/g++-123 " +
$"--slave /usr/bin/gcov gcov /usr/bin/gcov-123 " +
$"--slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-123 " +
$"--slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-123 " +
$"--slave /usr/bin/gfortran gfortran /usr/bin/gfortran-123 " +
$"--slave /usr/bin/cpp cpp /usr/bin/cpp-123",
$"--slave /usr/bin/gfortran gfortran /usr/bin/gfortran-123",
"sudo update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-123 1230",
};

int commandExecuted = 0;
Expand Down Expand Up @@ -115,7 +109,7 @@ public async Task CompilerInstallationRunsTheExpectedWorkloadCommandInLinuxForGc
await compilerInstallation.ExecuteAsync(CancellationToken.None).ConfigureAwait(false);
}

Assert.AreEqual(12, commandExecuted);
Assert.AreEqual(7, commandExecuted);
}

[Test]
Expand Down Expand Up @@ -247,24 +241,18 @@ public async Task CompilerInstallationInLinuxDefaultsToGcc10()
ProcessStartInfo expectedInfo = new ProcessStartInfo();
List<string> expectedCommands = new List<string>()
{
"sudo update-alternatives --remove-all gcc",
"sudo update-alternatives --remove-all gfortran",
"sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y",
"sudo apt update",
"sudo apt install build-essential gcc-10 g++-10 gfortran-10 -y --quiet",
"sudo update-alternatives --remove-all gcc",
"sudo update-alternatives --remove-all cc",
"sudo update-alternatives --remove-all g++",
"sudo update-alternatives --remove-all gcov",
"sudo update-alternatives --remove-all gcc-ar",
"sudo update-alternatives --remove-all gcc-ranlib",
"sudo update-alternatives --remove-all gfortran",
"sudo update-alternatives --remove-all cpp",
"sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 " +
$"--slave /usr/bin/g++ g++ /usr/bin/g++-10 " +
$"--slave /usr/bin/gcov gcov /usr/bin/gcov-10 " +
$"--slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-10 " +
$"--slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-10 " +
$"--slave /usr/bin/gfortran gfortran /usr/bin/gfortran-10 " +
$"--slave /usr/bin/cpp cpp /usr/bin/cpp-10",
$"--slave /usr/bin/gfortran gfortran /usr/bin/gfortran-10",
"sudo update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-10 100",
};

int commandExecuted = 0;
Expand Down Expand Up @@ -297,7 +285,7 @@ public async Task CompilerInstallationInLinuxDefaultsToGcc10()
await compilerInstallation.ExecuteAsync(CancellationToken.None).ConfigureAwait(false);
}

Assert.AreEqual(12, commandExecuted);
Assert.AreEqual(7, commandExecuted);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ private async Task InstallGccAsync(string gccVersion, EventContext telemetryCont
case LinuxDistribution.Ubuntu:
case LinuxDistribution.Debian:
// default to 10
await this.RemoveAlternativesAsync(telemetryContext, cancellationToken);
gccVersion = (string.IsNullOrEmpty(gccVersion)) ? "10" : gccVersion;
await this.ExecuteCommandAsync("add-apt-repository", $"ppa:ubuntu-toolchain-r/test -y", Environment.CurrentDirectory, telemetryContext, cancellationToken);
await this.ExecuteCommandAsync("apt", $"update", Environment.CurrentDirectory, telemetryContext, cancellationToken);
Expand All @@ -224,6 +225,7 @@ private async Task InstallGccAsync(string gccVersion, EventContext telemetryCont
case LinuxDistribution.CentOS8:
case LinuxDistribution.RHEL8:
case LinuxDistribution.Mariner:
await this.RemoveAlternativesAsync(telemetryContext, cancellationToken);
await this.ExecuteCommandAsync("dnf", @$"install make gcc-toolset-{gccVersion} gcc-toolset-{gccVersion}-gcc-gfortran -y --quiet", Environment.CurrentDirectory, telemetryContext, cancellationToken);
await this.SetGccPriorityAsync(gccVersion, telemetryContext, cancellationToken);

Expand All @@ -234,18 +236,12 @@ private async Task InstallGccAsync(string gccVersion, EventContext telemetryCont
}
}

private async Task SetGccPriorityAsync(string gccVersion, EventContext telemetryContext, CancellationToken cancellationToken)
private async Task RemoveAlternativesAsync(EventContext telemetryContext, CancellationToken cancellationToken)
{
string[] compilers =
{
"gcc",
"cc",
"g++",
"gcov",
"gcc-ar",
"gcc-ranlib",
"gfortran",
"cpp"
"gfortran"
};

// due to the following error:
Expand All @@ -258,23 +254,30 @@ private async Task SetGccPriorityAsync(string gccVersion, EventContext telemetry
{
await this.ExecuteCommandAsync("update-alternatives", $"--remove-all {compiler}", Environment.CurrentDirectory, telemetryContext, cancellationToken);
}
catch
catch
{
// the message is:
// "error: no alternatives for g++"
// so we can continue as normal; non-breaking
}
}
}

private async Task SetGccPriorityAsync(string gccVersion, EventContext telemetryContext, CancellationToken cancellationToken)
{
string updateAlternativeArgument = $"--install /usr/bin/gcc gcc /usr/bin/gcc-{gccVersion} {gccVersion}0 " +
$"--slave /usr/bin/g++ g++ /usr/bin/g++-{gccVersion} " +
$"--slave /usr/bin/gcov gcov /usr/bin/gcov-{gccVersion} " +
$"--slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-{gccVersion} " +
$"--slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-{gccVersion} " +
$"--slave /usr/bin/gfortran gfortran /usr/bin/gfortran-{gccVersion} " +
$"--slave /usr/bin/cpp cpp /usr/bin/cpp-{gccVersion}";
$"--slave /usr/bin/gfortran gfortran /usr/bin/gfortran-{gccVersion}";

await this.ExecuteCommandAsync("update-alternatives", updateAlternativeArgument, Environment.CurrentDirectory, telemetryContext, cancellationToken);

// For some update path, the cpp can't be update-alternative by a gcc, so needs a separate call.
string updateAlternativeArgumentCpp = $"--install /usr/bin/cpp cpp /usr/bin/cpp-{gccVersion} {gccVersion}0";

await this.ExecuteCommandAsync("update-alternatives", updateAlternativeArgumentCpp, Environment.CurrentDirectory, telemetryContext, cancellationToken);
}

private async Task InstallAoccAsync(string aoccVersion, EventContext telemetryContext, CancellationToken cancellationToken)
Expand Down