@@ -4,6 +4,7 @@ import ciInfo from "ci-info"
44const { GITHUB_ACTIONS } = ciInfo
55import { info , warning } from "ci-log"
66import { addEnv } from "envosman"
7+ import { execa } from "execa"
78import memoize from "memoizee"
89import { pathExists } from "path-exists"
910import { addExeExt } from "patha"
@@ -23,6 +24,7 @@ import { trySetupLLVMBrew } from "./llvm_brew_installer.js"
2324import { majorLLVMVersion } from "./utils.js"
2425
2526const dirname = typeof __dirname === "string" ? __dirname : path . dirname ( fileURLToPath ( import . meta. url ) )
27+ const APT_DEFAULT_GCC_DEPENDENCY = / ^ [ \t ] + D e p e n d s : [ \t ] + g c c - ( \d + ) [ \t ] * $ / mu
2628
2729export async function setupLLVM ( { version, setupDir, arch } : SetupOptions ) : Promise < InstallationInfo > {
2830 const installationInfo = await setupLLVMOnly ( version , setupDir , arch )
@@ -68,11 +70,24 @@ async function setupGccForLLVM_(arch: string) {
6870 if ( process . platform === "linux" ) {
6971 // using llvm requires ld, an up to date libstdc++, etc. So, install gcc first,
7072 // but with a lower priority than the one used by activateLLVM()
71- await setupGcc ( { version : getVersion ( "gcc" , undefined , await ubuntuVersion ( ) ) , setupDir : "" , arch, priority : 40 } )
73+ const distroVersion = await ubuntuVersion ( )
74+ const defaultGccVersion = getVersion ( "gcc" , undefined , distroVersion )
75+ const gccVersion = hasAptGet ( ) ? await getAptDefaultGccVersion ( defaultGccVersion ) : defaultGccVersion
76+ await setupGcc ( { version : gccVersion , setupDir : "" , arch, priority : 40 } )
7277 }
7378}
7479const setupGccForLLVM = memoize ( setupGccForLLVM_ , { promise : true } )
7580
81+ async function getAptDefaultGccVersion ( fallback : string ) {
82+ try {
83+ const { stdout } = await execa ( "apt-cache" , [ "depends" , "gcc" ] , { stdio : "pipe" } )
84+ return stdout . match ( APT_DEFAULT_GCC_DEPENDENCY ) ?. [ 1 ] ?? fallback
85+ } catch {
86+ // Preserve the existing default when APT metadata cannot be queried.
87+ return fallback
88+ }
89+ }
90+
7691export async function activateLLVM ( directory : string , version : string ) {
7792 const ld = process . env . LD_LIBRARY_PATH ?? ""
7893 const dyld = process . env . DYLD_LIBRARY_PATH ?? ""
0 commit comments