Skip to content

Commit c6fcad2

Browse files
ericavellaErica Vellanoweth
andauthored
remove compilers fix (#220)
* removing compiler alternatives existing on the machine before providing new ones * adding remove-all gcc * adding cc * updating tests * fixing cc issue * removing all but gcc/gfortran --------- Co-authored-by: Erica Vellanoweth <[email protected]>
1 parent ff1da58 commit c6fcad2

File tree

2 files changed

+24
-33
lines changed

2 files changed

+24
-33
lines changed

src/VirtualClient/VirtualClient.Dependencies.UnitTests/CompilerInstallationTests.cs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,18 @@ public async Task CompilerInstallationRunsTheExpectedWorkloadCommandInLinuxForGc
6666
ProcessStartInfo expectedInfo = new ProcessStartInfo();
6767
List<string> expectedCommands = new List<string>()
6868
{
69+
"sudo update-alternatives --remove-all gcc",
70+
"sudo update-alternatives --remove-all gfortran",
6971
"sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y",
7072
"sudo apt update",
7173
"sudo apt install build-essential gcc-123 g++-123 gfortran-123 -y --quiet",
72-
"sudo update-alternatives --remove-all gcc",
73-
"sudo update-alternatives --remove-all cc",
74-
"sudo update-alternatives --remove-all g++",
75-
"sudo update-alternatives --remove-all gcov",
76-
"sudo update-alternatives --remove-all gcc-ar",
77-
"sudo update-alternatives --remove-all gcc-ranlib",
78-
"sudo update-alternatives --remove-all gfortran",
79-
"sudo update-alternatives --remove-all cpp",
8074
"sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-123 1230 " +
8175
$"--slave /usr/bin/g++ g++ /usr/bin/g++-123 " +
8276
$"--slave /usr/bin/gcov gcov /usr/bin/gcov-123 " +
8377
$"--slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-123 " +
8478
$"--slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-123 " +
85-
$"--slave /usr/bin/gfortran gfortran /usr/bin/gfortran-123 " +
86-
$"--slave /usr/bin/cpp cpp /usr/bin/cpp-123",
79+
$"--slave /usr/bin/gfortran gfortran /usr/bin/gfortran-123",
80+
"sudo update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-123 1230",
8781
};
8882

8983
int commandExecuted = 0;
@@ -115,7 +109,7 @@ public async Task CompilerInstallationRunsTheExpectedWorkloadCommandInLinuxForGc
115109
await compilerInstallation.ExecuteAsync(CancellationToken.None).ConfigureAwait(false);
116110
}
117111

118-
Assert.AreEqual(12, commandExecuted);
112+
Assert.AreEqual(7, commandExecuted);
119113
}
120114

121115
[Test]
@@ -247,24 +241,18 @@ public async Task CompilerInstallationInLinuxDefaultsToGcc10()
247241
ProcessStartInfo expectedInfo = new ProcessStartInfo();
248242
List<string> expectedCommands = new List<string>()
249243
{
244+
"sudo update-alternatives --remove-all gcc",
245+
"sudo update-alternatives --remove-all gfortran",
250246
"sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y",
251247
"sudo apt update",
252248
"sudo apt install build-essential gcc-10 g++-10 gfortran-10 -y --quiet",
253-
"sudo update-alternatives --remove-all gcc",
254-
"sudo update-alternatives --remove-all cc",
255-
"sudo update-alternatives --remove-all g++",
256-
"sudo update-alternatives --remove-all gcov",
257-
"sudo update-alternatives --remove-all gcc-ar",
258-
"sudo update-alternatives --remove-all gcc-ranlib",
259-
"sudo update-alternatives --remove-all gfortran",
260-
"sudo update-alternatives --remove-all cpp",
261249
"sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 " +
262250
$"--slave /usr/bin/g++ g++ /usr/bin/g++-10 " +
263251
$"--slave /usr/bin/gcov gcov /usr/bin/gcov-10 " +
264252
$"--slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-10 " +
265253
$"--slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-10 " +
266-
$"--slave /usr/bin/gfortran gfortran /usr/bin/gfortran-10 " +
267-
$"--slave /usr/bin/cpp cpp /usr/bin/cpp-10",
254+
$"--slave /usr/bin/gfortran gfortran /usr/bin/gfortran-10",
255+
"sudo update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-10 100",
268256
};
269257

270258
int commandExecuted = 0;
@@ -297,7 +285,7 @@ public async Task CompilerInstallationInLinuxDefaultsToGcc10()
297285
await compilerInstallation.ExecuteAsync(CancellationToken.None).ConfigureAwait(false);
298286
}
299287

300-
Assert.AreEqual(12, commandExecuted);
288+
Assert.AreEqual(7, commandExecuted);
301289
}
302290

303291
[Test]

src/VirtualClient/VirtualClient.Dependencies/CompilerInstallation.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ private async Task InstallGccAsync(string gccVersion, EventContext telemetryCont
213213
case LinuxDistribution.Ubuntu:
214214
case LinuxDistribution.Debian:
215215
// default to 10
216+
await this.RemoveAlternativesAsync(telemetryContext, cancellationToken);
216217
gccVersion = (string.IsNullOrEmpty(gccVersion)) ? "10" : gccVersion;
217218
await this.ExecuteCommandAsync("add-apt-repository", $"ppa:ubuntu-toolchain-r/test -y", Environment.CurrentDirectory, telemetryContext, cancellationToken);
218219
await this.ExecuteCommandAsync("apt", $"update", Environment.CurrentDirectory, telemetryContext, cancellationToken);
@@ -224,6 +225,7 @@ private async Task InstallGccAsync(string gccVersion, EventContext telemetryCont
224225
case LinuxDistribution.CentOS8:
225226
case LinuxDistribution.RHEL8:
226227
case LinuxDistribution.Mariner:
228+
await this.RemoveAlternativesAsync(telemetryContext, cancellationToken);
227229
await this.ExecuteCommandAsync("dnf", @$"install make gcc-toolset-{gccVersion} gcc-toolset-{gccVersion}-gcc-gfortran -y --quiet", Environment.CurrentDirectory, telemetryContext, cancellationToken);
228230
await this.SetGccPriorityAsync(gccVersion, telemetryContext, cancellationToken);
229231

@@ -234,18 +236,12 @@ private async Task InstallGccAsync(string gccVersion, EventContext telemetryCont
234236
}
235237
}
236238

237-
private async Task SetGccPriorityAsync(string gccVersion, EventContext telemetryContext, CancellationToken cancellationToken)
239+
private async Task RemoveAlternativesAsync(EventContext telemetryContext, CancellationToken cancellationToken)
238240
{
239241
string[] compilers =
240242
{
241243
"gcc",
242-
"cc",
243-
"g++",
244-
"gcov",
245-
"gcc-ar",
246-
"gcc-ranlib",
247-
"gfortran",
248-
"cpp"
244+
"gfortran"
249245
};
250246

251247
// due to the following error:
@@ -258,23 +254,30 @@ private async Task SetGccPriorityAsync(string gccVersion, EventContext telemetry
258254
{
259255
await this.ExecuteCommandAsync("update-alternatives", $"--remove-all {compiler}", Environment.CurrentDirectory, telemetryContext, cancellationToken);
260256
}
261-
catch
257+
catch
262258
{
263259
// the message is:
264260
// "error: no alternatives for g++"
265261
// so we can continue as normal; non-breaking
266262
}
267263
}
264+
}
268265

266+
private async Task SetGccPriorityAsync(string gccVersion, EventContext telemetryContext, CancellationToken cancellationToken)
267+
{
269268
string updateAlternativeArgument = $"--install /usr/bin/gcc gcc /usr/bin/gcc-{gccVersion} {gccVersion}0 " +
270269
$"--slave /usr/bin/g++ g++ /usr/bin/g++-{gccVersion} " +
271270
$"--slave /usr/bin/gcov gcov /usr/bin/gcov-{gccVersion} " +
272271
$"--slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-{gccVersion} " +
273272
$"--slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-{gccVersion} " +
274-
$"--slave /usr/bin/gfortran gfortran /usr/bin/gfortran-{gccVersion} " +
275-
$"--slave /usr/bin/cpp cpp /usr/bin/cpp-{gccVersion}";
273+
$"--slave /usr/bin/gfortran gfortran /usr/bin/gfortran-{gccVersion}";
276274

277275
await this.ExecuteCommandAsync("update-alternatives", updateAlternativeArgument, Environment.CurrentDirectory, telemetryContext, cancellationToken);
276+
277+
// For some update path, the cpp can't be update-alternative by a gcc, so needs a separate call.
278+
string updateAlternativeArgumentCpp = $"--install /usr/bin/cpp cpp /usr/bin/cpp-{gccVersion} {gccVersion}0";
279+
280+
await this.ExecuteCommandAsync("update-alternatives", updateAlternativeArgumentCpp, Environment.CurrentDirectory, telemetryContext, cancellationToken);
278281
}
279282

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

0 commit comments

Comments
 (0)