2024-08-07 14:44:32 +08:00
|
|
|
import { join } from "path"
|
2023-05-25 14:48:26 +08:00
|
|
|
import { endGroup, notice, startGroup } from "@actions/core"
|
|
|
|
import { error, info } from "ci-log"
|
2024-08-15 09:22:33 +08:00
|
|
|
import { addEnv } from "os-env"
|
2023-05-25 14:48:26 +08:00
|
|
|
import semverValid from "semver/functions/valid"
|
2024-08-16 06:22:07 +08:00
|
|
|
import { getSuccessMessage, rcOptions } from "./cli-options.js"
|
|
|
|
import { setupGcc, setupMingw } from "./gcc/gcc.js"
|
|
|
|
import { activateGcovGCC, activateGcovLLVM } from "./gcovr/gcovr.js"
|
|
|
|
import { setupLLVM } from "./llvm/llvm.js"
|
|
|
|
import { setupMSVC } from "./msvc/msvc.js"
|
|
|
|
import { getVersion } from "./versions/versions.js"
|
2023-05-25 14:48:26 +08:00
|
|
|
|
|
|
|
/** Detecting the compiler version. Divide the given string by `-` and use the second element as the version */
|
|
|
|
export function getCompilerInfo(compilerAndVersion: string) {
|
|
|
|
const compilerAndMaybeVersion = compilerAndVersion.split("-")
|
|
|
|
const compiler = compilerAndMaybeVersion[0]
|
|
|
|
if (1 in compilerAndMaybeVersion) {
|
|
|
|
const maybeVersion = compilerAndMaybeVersion[1]
|
|
|
|
if (semverValid(maybeVersion) !== null) {
|
|
|
|
return { compiler, version: maybeVersion }
|
|
|
|
} else {
|
|
|
|
info(`Invalid semver version ${maybeVersion} used for the compiler.`)
|
|
|
|
return { compiler, version: maybeVersion }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { compiler, version: undefined }
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Installing the specified compiler */
|
|
|
|
export async function installCompiler(
|
|
|
|
compilerAndVersion: string,
|
|
|
|
osVersion: number[] | null,
|
|
|
|
setupCppDir: string,
|
|
|
|
arch: string,
|
|
|
|
successMessages: string[],
|
|
|
|
hasLLVM: boolean,
|
2023-07-25 04:20:04 +08:00
|
|
|
errorMessages: string[],
|
2023-05-25 14:48:26 +08:00
|
|
|
) {
|
|
|
|
try {
|
|
|
|
const { compiler, version } = getCompilerInfo(compilerAndVersion)
|
|
|
|
|
|
|
|
// install the compiler. We allow some aliases for the compiler name
|
|
|
|
startGroup(`Installing ${compiler} ${version ?? ""}`)
|
|
|
|
switch (compiler) {
|
|
|
|
case "llvm":
|
|
|
|
case "clang":
|
|
|
|
case "clang++": {
|
|
|
|
const installationInfo = await setupLLVM(
|
|
|
|
getVersion("llvm", version, osVersion),
|
|
|
|
join(setupCppDir, "llvm"),
|
2023-07-25 04:20:04 +08:00
|
|
|
arch,
|
2023-05-25 14:48:26 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
await activateGcovLLVM()
|
|
|
|
|
|
|
|
successMessages.push(getSuccessMessage("llvm", installationInfo))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case "gcc":
|
|
|
|
case "mingw":
|
|
|
|
case "cygwin":
|
|
|
|
case "msys": {
|
2024-04-03 15:15:43 +08:00
|
|
|
const gccVersion = compiler === "mingw"
|
|
|
|
? getVersion("mingw", version, osVersion)
|
|
|
|
: getVersion("gcc", version, osVersion)
|
|
|
|
const installationInfo = compiler === "mingw"
|
|
|
|
? await setupMingw(gccVersion, join(setupCppDir, "gcc"), arch)
|
|
|
|
: await setupGcc(gccVersion, join(setupCppDir, "gcc"), arch)
|
2023-05-25 14:48:26 +08:00
|
|
|
|
|
|
|
if (hasLLVM) {
|
|
|
|
// remove back the added CPPFLAGS of LLVM that include the LLVM headers
|
2024-08-15 09:43:53 +08:00
|
|
|
await addEnv("CPPFLAGS", "", rcOptions)
|
2023-05-25 14:48:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
await activateGcovGCC(gccVersion)
|
|
|
|
|
|
|
|
successMessages.push(getSuccessMessage("gcc", installationInfo))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case "cl":
|
|
|
|
case "msvc":
|
|
|
|
case "msbuild":
|
|
|
|
case "vs":
|
|
|
|
case "visualstudio":
|
|
|
|
case "visualcpp":
|
|
|
|
case "visualc++": {
|
|
|
|
const installationInfo = await setupMSVC(
|
|
|
|
getVersion("msvc", version, osVersion),
|
|
|
|
join(setupCppDir, "msvc"),
|
2023-07-25 04:20:04 +08:00
|
|
|
arch,
|
2023-05-25 14:48:26 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if (hasLLVM) {
|
|
|
|
// remove the CPPFLAGS of LLVM that include the LLVM headers
|
2024-08-15 09:43:53 +08:00
|
|
|
await addEnv("CPPFLAGS", "", rcOptions)
|
2023-05-25 14:48:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
successMessages.push(getSuccessMessage("msvc", installationInfo))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case "appleclang":
|
|
|
|
case "applellvm": {
|
|
|
|
notice("Assuming apple-clang is already installed")
|
2024-08-15 09:43:53 +08:00
|
|
|
await Promise.all([addEnv("CC", "clang", rcOptions), addEnv("CXX", "clang++", rcOptions)])
|
2023-05-25 14:48:26 +08:00
|
|
|
successMessages.push(getSuccessMessage("apple-clang", undefined))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
errorMessages.push(`Unsupported compiler ${compiler}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
error(err as string | Error)
|
|
|
|
errorMessages.push(`Failed to install the ${compilerAndVersion}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
endGroup()
|
|
|
|
}
|