Skip to content

Commit 6dafc98

Browse files
committed
fix: use higher priority when installing llvm alternatives
before this change, gcc and g++ are installed using `updateAptAlternatives()` with the same priority of 40 when they are installed along with ld and libstdc++ as the dependencies of clang and llvm. but both gcc and clang are installed using the same priority of 40 on ubuntu, and the same applies to g++ and clang++. this renders it impossible to use the default compilers of cc and cxx when clang/llvm is installed using setup-cpp on an ubuntu host, as gcc is always prefered over clang by the update-alternatives, as their priorities are identical. in this change, the "priority" parameter is added to the setupGcc(), so that we can specify a different priority when installing llvm. strictly speaking, this is not necessary. as we can just use a higher priority when calling updateAptAlternatives() in llvm/llvm.ts. but by making it more explicit, we can ensure that we always prefer llvm over gcc when installing llvm. Signed-off-by: Kefu Chai <[email protected]>
1 parent dd5d0e8 commit 6dafc98

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

src/gcc/gcc.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function getGccPackageInfo(version: string, platform: NodeJS.Platform, arch: str
7878
}
7979

8080
// eslint-disable-next-line @typescript-eslint/no-unused-vars
81-
export async function setupGcc(version: string, setupDir: string, arch: string) {
81+
export async function setupGcc(version: string, setupDir: string, arch: string, priority: number = 40) {
8282
let installationInfo: InstallationInfo | undefined
8383
switch (process.platform) {
8484
case "win32": {
@@ -139,7 +139,7 @@ export async function setupGcc(version: string, setupDir: string, arch: string)
139139
}
140140
}
141141
if (installationInfo !== undefined) {
142-
await activateGcc(version, installationInfo.binDir)
142+
await activateGcc(version, installationInfo.binDir, priority)
143143
return installationInfo
144144
}
145145
return undefined
@@ -199,7 +199,7 @@ async function setupChocoMingw(version: string, arch: string): Promise<Installat
199199
return undefined
200200
}
201201

202-
async function activateGcc(version: string, binDir: string) {
202+
async function activateGcc(version: string, binDir: string, priority: number = 40) {
203203
const promises: Promise<void | ExecaReturnValue<string>>[] = []
204204
// Setup gcc as the compiler
205205

@@ -223,21 +223,21 @@ async function activateGcc(version: string, binDir: string) {
223223

224224
if (isUbuntu()) {
225225
promises.push(
226-
updateAptAlternatives("cc", `${binDir}/gcc-${majorVersion}`),
227-
updateAptAlternatives("cxx", `${binDir}/g++-${majorVersion}`),
228-
updateAptAlternatives("gcc", `${binDir}/gcc-${majorVersion}`),
229-
updateAptAlternatives("g++", `${binDir}/g++-${majorVersion}`),
226+
updateAptAlternatives("cc", `${binDir}/gcc-${majorVersion}`, priority),
227+
updateAptAlternatives("cxx", `${binDir}/g++-${majorVersion}`, priority),
228+
updateAptAlternatives("gcc", `${binDir}/gcc-${majorVersion}`, priority),
229+
updateAptAlternatives("g++", `${binDir}/g++-${majorVersion}`, priority),
230230
)
231231
}
232232
} else {
233233
promises.push(addEnv("CC", `${binDir}/gcc-${version}`), addEnv("CXX", `${binDir}/g++-${version}`))
234234

235235
if (isUbuntu()) {
236236
promises.push(
237-
updateAptAlternatives("cc", `${binDir}/gcc-${version}`),
238-
updateAptAlternatives("cxx", `${binDir}/g++-${version}`),
239-
updateAptAlternatives("gcc", `${binDir}/gcc-${version}`),
240-
updateAptAlternatives("g++", `${binDir}/g++-${version}`),
237+
updateAptAlternatives("cc", `${binDir}/gcc-${version}`, priority),
238+
updateAptAlternatives("cxx", `${binDir}/g++-${version}`, priority),
239+
updateAptAlternatives("gcc", `${binDir}/gcc-${version}`, priority),
240+
updateAptAlternatives("g++", `${binDir}/g++-${version}`, priority),
241241
)
242242
}
243243
}

src/llvm/llvm.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ const llvmBinaryDeps = memoize(llvmBinaryDeps_raw, { isPromise: true })
8282

8383
async function setupLLVMDeps_raw(arch: string) {
8484
if (process.platform === "linux") {
85-
// using llvm requires ld, an up to date libstdc++, etc. So, install gcc first
86-
await setupGcc(getVersion("gcc", undefined, await ubuntuVersion()), "", arch)
85+
// using llvm requires ld, an up to date libstdc++, etc. So, install gcc first,
86+
// but with a lower priority than the one used by activateLLVM()
87+
await setupGcc(getVersion("gcc", undefined, await ubuntuVersion()), "", arch, 40)
8788
}
8889
}
8990
const setupLLVMDeps = memoize(setupLLVMDeps_raw, { isPromise: true })
@@ -125,9 +126,10 @@ export async function activateLLVM(directory: string) {
125126
// }
126127

127128
if (isUbuntu()) {
129+
const priority = 60
128130
actPromises.push(
129-
updateAptAlternatives("cc", `${directory}/bin/clang`),
130-
updateAptAlternatives("cxx", `${directory}/bin/clang++`),
131+
updateAptAlternatives("cc", `${directory}/bin/clang`, priority),
132+
updateAptAlternatives("cxx", `${directory}/bin/clang++`, priority),
131133
updateAptAlternatives("clang", `${directory}/bin/clang`),
132134
updateAptAlternatives("clang++", `${directory}/bin/clang++`),
133135
updateAptAlternatives("lld", `${directory}/bin/lld`),

src/utils/setup/setupAptPack.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,14 @@ export async function addAptKeyViaDownload(name: string, url: string) {
219219
return fileName
220220
}
221221

222-
export async function updateAptAlternatives(name: string, path: string) {
222+
export async function updateAptAlternatives(name: string, path: string, priority: number = 40) {
223223
if (GITHUB_ACTIONS) {
224-
return execRoot("update-alternatives", ["--install", `/usr/bin/${name}`, name, path, "40"])
224+
return execRoot("update-alternatives", ["--install", `/usr/bin/${name}`, name, path, number.toString()])
225225
} else {
226226
await setupCppInProfile()
227227
return appendFile(
228228
cpprc_path,
229-
`\nif [ $UID -eq 0 ]; then update-alternatives --install /usr/bin/${name} ${name} ${path} 40; fi\n`,
229+
`\nif [ $UID -eq 0 ]; then update-alternatives --install /usr/bin/${name} ${name} ${path} ${priority}; fi\n`,
230230
)
231231
}
232232
}

0 commit comments

Comments
 (0)