2023-01-20 06:06:43 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
/* eslint-disable node/shebang */
|
|
|
|
|
2023-08-22 11:16:33 +08:00
|
|
|
import { GITHUB_ACTIONS, isCI } from "ci-info"
|
2023-01-23 06:21:39 +08:00
|
|
|
import { error, info, success, warning } from "ci-log"
|
|
|
|
import * as numerous from "numerous"
|
|
|
|
import numerousLocale from "numerous/locales/en.js"
|
|
|
|
import * as timeDelta from "time-delta"
|
|
|
|
import timeDeltaLocale from "time-delta/locales/en.js"
|
|
|
|
import { untildifyUser } from "untildify-user"
|
2023-04-22 12:14:22 +08:00
|
|
|
import { checkUpdates } from "./check-updates"
|
2023-05-25 14:48:26 +08:00
|
|
|
import { parseArgs, printHelp } from "./cli-options"
|
|
|
|
import { installCompiler } from "./compilers"
|
|
|
|
import { installTool, tools } from "./tool"
|
|
|
|
import { finalizeCpprc } from "./utils/env/addEnv"
|
2023-01-23 06:21:39 +08:00
|
|
|
import { isArch } from "./utils/env/isArch"
|
2022-05-06 08:11:42 +08:00
|
|
|
import { ubuntuVersion } from "./utils/env/ubuntu_version"
|
2023-01-23 06:21:39 +08:00
|
|
|
import { setupPacmanPack } from "./utils/setup/setupPacmanPack"
|
2023-05-25 14:48:26 +08:00
|
|
|
import { syncVersions } from "./versions/versions"
|
2021-09-18 01:51:59 +08:00
|
|
|
|
|
|
|
/** The main entry function */
|
2023-05-25 14:48:26 +08:00
|
|
|
async function main(args: string[]): Promise<number> {
|
2023-04-22 12:14:22 +08:00
|
|
|
let checkUpdatePromise = Promise.resolve()
|
2023-04-22 17:31:04 +08:00
|
|
|
if (!GITHUB_ACTIONS) {
|
2023-04-22 12:14:22 +08:00
|
|
|
checkUpdatePromise = checkUpdates()
|
2021-09-18 20:20:18 +08:00
|
|
|
process.env.ACTIONS_ALLOW_UNSECURE_COMMANDS = "true"
|
|
|
|
}
|
|
|
|
|
2021-09-18 01:51:59 +08:00
|
|
|
// parse options using mri or github actions
|
2022-05-06 09:36:47 +08:00
|
|
|
const opts = parseArgs(args)
|
2021-09-14 15:03:59 +08:00
|
|
|
|
2021-09-18 02:09:00 +08:00
|
|
|
// print help
|
|
|
|
if (opts.help) {
|
|
|
|
printHelp()
|
|
|
|
}
|
|
|
|
|
2021-09-18 01:51:59 +08:00
|
|
|
// cpu architecture
|
|
|
|
const arch = opts.architecture ?? process.arch
|
|
|
|
|
|
|
|
// the installation dir for the tools that are downloaded directly
|
2022-08-08 11:04:59 +08:00
|
|
|
const setupCppDir = process.env.SETUP_CPP_DIR ?? untildifyUser("")
|
2021-09-18 01:51:59 +08:00
|
|
|
|
|
|
|
// report messages
|
|
|
|
const successMessages: string[] = []
|
|
|
|
const errorMessages: string[] = []
|
|
|
|
|
2022-04-18 20:27:28 +08:00
|
|
|
const timeFormatter = timeDelta.create({ autoloadLocales: true })
|
2022-04-18 21:16:11 +08:00
|
|
|
timeDelta.addLocale(timeDeltaLocale as timeDelta.Locale)
|
2023-01-23 06:21:39 +08:00
|
|
|
numerous.addLocale(numerousLocale as numerous.Locale)
|
2022-04-18 20:07:02 +08:00
|
|
|
let time1: number
|
|
|
|
let time2: number
|
2022-04-18 18:23:58 +08:00
|
|
|
|
2021-09-18 20:44:12 +08:00
|
|
|
// installing the specified tools
|
|
|
|
|
2022-11-03 11:06:36 +08:00
|
|
|
const osVersion = await ubuntuVersion()
|
2022-05-06 08:11:42 +08:00
|
|
|
|
2022-05-06 09:36:47 +08:00
|
|
|
// sync the version for the llvm tools
|
|
|
|
if (!syncVersions(opts, ["llvm", "clangtidy", "clangformat"])) {
|
|
|
|
error("The same version must be used for llvm, clangformat and clangtidy")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2022-11-03 11:55:26 +08:00
|
|
|
if (isArch() && typeof opts.cppcheck === "string" && typeof opts.gcovr === "string") {
|
|
|
|
info("installing python-pygments to avoid conflicts with cppcheck and gcovr on Arch linux")
|
2023-05-25 05:57:08 +08:00
|
|
|
await setupPacmanPack("python-pygments")
|
2022-10-29 04:54:02 +08:00
|
|
|
}
|
|
|
|
|
2023-05-25 14:48:26 +08:00
|
|
|
/** Used to unset CPPFLAGS of LLVM when other compilers are used as the main compiler */
|
|
|
|
let hasLLVM = false
|
|
|
|
|
2021-09-18 20:44:12 +08:00
|
|
|
// loop over the tools and run their setup function
|
2023-08-22 11:16:33 +08:00
|
|
|
|
|
|
|
let failedFast = false
|
2021-09-18 20:44:12 +08:00
|
|
|
for (const tool of tools) {
|
2023-08-22 11:16:33 +08:00
|
|
|
// fail fast inside CI when any tool fails
|
|
|
|
if (isCI) {
|
|
|
|
if (errorMessages.length !== 0) {
|
|
|
|
failedFast = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-18 20:44:12 +08:00
|
|
|
// get the version or "true" or undefined for this tool from the options
|
2022-02-14 07:50:15 +08:00
|
|
|
const version = opts[tool]
|
2021-09-18 20:44:12 +08:00
|
|
|
|
|
|
|
// skip if undefined
|
2022-02-14 07:50:15 +08:00
|
|
|
if (version !== undefined) {
|
2021-11-22 04:20:51 +08:00
|
|
|
// running the setup function for this tool
|
2022-04-18 20:07:02 +08:00
|
|
|
time1 = Date.now()
|
2023-05-25 14:48:26 +08:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2023-08-22 11:11:21 +08:00
|
|
|
hasLLVM = await installTool(
|
|
|
|
tool,
|
|
|
|
version,
|
|
|
|
osVersion,
|
|
|
|
arch,
|
|
|
|
setupCppDir,
|
|
|
|
successMessages,
|
|
|
|
errorMessages,
|
2023-08-22 11:16:33 +08:00
|
|
|
parseFloat(opts.timeout ?? "10"),
|
2023-08-22 11:11:21 +08:00
|
|
|
)
|
2022-04-18 20:07:02 +08:00
|
|
|
time2 = Date.now()
|
2022-04-18 21:24:04 +08:00
|
|
|
info(`took ${timeFormatter.format(time1, time2) || "0 seconds"}`)
|
2021-09-18 20:44:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 11:16:33 +08:00
|
|
|
if (!failedFast) {
|
|
|
|
// installing the specified compiler
|
|
|
|
const maybeCompiler = opts.compiler
|
|
|
|
if (maybeCompiler !== undefined) {
|
|
|
|
const time1Compiler = Date.now()
|
|
|
|
await installCompiler(maybeCompiler, osVersion, setupCppDir, arch, successMessages, hasLLVM, errorMessages)
|
|
|
|
const time2Compiler = Date.now()
|
|
|
|
info(`took ${timeFormatter.format(time1Compiler, time2Compiler) || "0 seconds"}`)
|
|
|
|
}
|
2021-09-18 01:51:59 +08:00
|
|
|
}
|
|
|
|
|
2022-11-23 12:51:18 +08:00
|
|
|
await finalizeCpprc()
|
2022-11-21 15:14:33 +08:00
|
|
|
|
2022-01-30 07:13:52 +08:00
|
|
|
if (successMessages.length === 0 && errorMessages.length === 0) {
|
2023-01-18 13:40:21 +08:00
|
|
|
warning("setup-cpp was called without any arguments. Nothing to do.")
|
2022-01-30 07:13:52 +08:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-09-18 01:51:59 +08:00
|
|
|
// report the messages in the end
|
2021-09-18 21:21:22 +08:00
|
|
|
successMessages.forEach((tool) => success(tool))
|
|
|
|
errorMessages.forEach((tool) => error(tool))
|
2021-09-18 01:51:59 +08:00
|
|
|
|
2023-01-18 13:40:21 +08:00
|
|
|
info("setup-cpp finished")
|
2021-09-30 08:51:50 +08:00
|
|
|
|
2023-04-22 17:31:04 +08:00
|
|
|
if (!GITHUB_ACTIONS) {
|
2021-09-30 08:51:50 +08:00
|
|
|
switch (process.platform) {
|
|
|
|
case "win32": {
|
2022-03-01 19:04:33 +08:00
|
|
|
warning("Run `RefreshEnv.cmd` or restart your shell to update the environment.")
|
2021-09-30 08:51:50 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
case "linux":
|
|
|
|
case "darwin": {
|
2022-03-01 19:04:33 +08:00
|
|
|
warning("Run `source ~/.cpprc` or restart your shell to update the environment.")
|
2021-09-30 08:51:50 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
// nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-22 12:14:22 +08:00
|
|
|
await checkUpdatePromise
|
|
|
|
|
2021-09-18 01:51:59 +08:00
|
|
|
return errorMessages.length === 0 ? 0 : 1 // exit with non-zero if any error message
|
2021-09-14 15:03:59 +08:00
|
|
|
}
|
2023-04-22 12:14:22 +08:00
|
|
|
|
2021-09-18 01:51:59 +08:00
|
|
|
// Run main
|
|
|
|
main(process.argv)
|
2021-09-14 15:03:59 +08:00
|
|
|
.then((ret) => {
|
|
|
|
process.exitCode = ret
|
|
|
|
})
|
2021-09-18 21:21:22 +08:00
|
|
|
.catch((err) => {
|
|
|
|
error("main() panicked!")
|
|
|
|
error(err as string | Error)
|
2021-09-14 15:03:59 +08:00
|
|
|
process.exitCode = 1
|
|
|
|
})
|