mirror of https://github.com/aminya/setup-cpp
Merge pull request #69 from aminya/sync [skip ci]
This commit is contained in:
commit
3aa347d997
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -1,4 +1,5 @@
|
|||
import { getCompilerInfo } from "../main"
|
||||
import { syncVersions } from "../default_versions"
|
||||
import { getCompilerInfo, Inputs, parseArgs } from "../main"
|
||||
|
||||
jest.setTimeout(300000)
|
||||
describe("getCompilerInfo", () => {
|
||||
|
@ -20,3 +21,16 @@ describe("getCompilerInfo", () => {
|
|||
expect(version).toBe("12")
|
||||
})
|
||||
})
|
||||
|
||||
describe("syncVersion", () => {
|
||||
it("Syncs llvm tools versions", async () => {
|
||||
const llvmTools = ["llvm", "clangtidy", "clangformat"] as Inputs[]
|
||||
expect(syncVersions(parseArgs(["--llvm", "14.0.0", "--clangtidy", "true"]), llvmTools)).toBe(true)
|
||||
expect(syncVersions(parseArgs(["--llvm", "13.0.0", "--clangtidy", "true"]), llvmTools)).toBe(true)
|
||||
expect(syncVersions(parseArgs(["--llvm", "13.0.0", "--clangtidy", "12.0.0"]), llvmTools)).toBe(false)
|
||||
|
||||
const opts = parseArgs(["--llvm", "14.0.0", "--clangtidy", "true"])
|
||||
expect(syncVersions(opts, llvmTools)).toBe(true)
|
||||
expect(opts.llvm).toBe(opts.clangtidy)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import { Inputs, Opts } from "./main"
|
||||
|
||||
const DefaultVersions: Record<string, string> = {
|
||||
llvm: "13.0.0", // https://github.com/llvm/llvm-project/releases
|
||||
clangtidy: "13.0.0",
|
||||
|
@ -16,7 +18,7 @@ const DefaultVersions: Record<string, string> = {
|
|||
|
||||
/** Get the default version if passed true or undefined, otherwise return the version itself */
|
||||
export function getVersion(name: string, version: string | undefined, osVersion: number[] | null = null) {
|
||||
if (version === "true" || (version === undefined && name in DefaultVersions)) {
|
||||
if (useDefault(version, name)) {
|
||||
// llvm on linux
|
||||
if (process.platform === "linux" && ["llvm", "clangtidy", "clangformat"].includes(name)) {
|
||||
// choose the default version for llvm based on ubuntu
|
||||
|
@ -32,3 +34,31 @@ export function getVersion(name: string, version: string | undefined, osVersion:
|
|||
return version ?? ""
|
||||
}
|
||||
}
|
||||
|
||||
function useDefault(version: string | undefined, name: string) {
|
||||
return version === "true" || (version === undefined && name in DefaultVersions)
|
||||
}
|
||||
|
||||
export function syncVersions(opts: Opts, tools: Inputs[]): boolean {
|
||||
for (let i = 0; i < tools.length; i++) {
|
||||
// tools excluding i_tool
|
||||
const otherTools = tools.slice(0, i).concat(tools.slice(i + 1))
|
||||
|
||||
const tool = tools[i]
|
||||
|
||||
if (!useDefault(opts[tool], tool)) {
|
||||
for (let i_other = 0; i_other < otherTools.length; i_other++) {
|
||||
const otherTool = otherTools[i_other]
|
||||
const useDefaultOtherTool = useDefault(opts[otherTool], otherTools[i_other])
|
||||
if (useDefaultOtherTool) {
|
||||
// use the same version if the other tool was requested with the default
|
||||
opts[otherTool] = opts[tool]
|
||||
} else if (opts[tool] !== opts[otherTools[i_other]]) {
|
||||
// error if different from the other given versions
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
|
33
src/main.ts
33
src/main.ts
|
@ -31,7 +31,7 @@ import numerousLocale from "numerous/locales/en.js"
|
|||
import { ubuntuVersion } from "./utils/env/ubuntu_version"
|
||||
|
||||
import semverValid from "semver/functions/valid"
|
||||
import { getVersion } from "./default_versions"
|
||||
import { getVersion, syncVersions } from "./default_versions"
|
||||
import { setupGcc } from "./gcc/gcc"
|
||||
import { InstallationInfo } from "./utils/setup/setupBin"
|
||||
import { error, info, success, warning } from "./utils/io/io"
|
||||
|
@ -100,7 +100,7 @@ const tools: Array<keyof typeof setups> = [
|
|||
]
|
||||
|
||||
/** The possible inputs to the program */
|
||||
type Inputs = keyof typeof setups | "compiler" | "architecture"
|
||||
export type Inputs = keyof typeof setups | "compiler" | "architecture"
|
||||
|
||||
// an array of possible inputs
|
||||
const inputs: Array<Inputs> = ["compiler", "architecture", ...tools]
|
||||
|
@ -112,12 +112,7 @@ export async function main(args: string[]): Promise<number> {
|
|||
}
|
||||
|
||||
// parse options using mri or github actions
|
||||
const opts = mri<Record<Inputs, string | undefined> & { help: boolean }>(args, {
|
||||
string: inputs,
|
||||
default: Object.fromEntries(inputs.map((inp) => [inp, maybeGetInput(inp)])),
|
||||
alias: { h: "help" },
|
||||
boolean: "help",
|
||||
})
|
||||
const opts = parseArgs(args)
|
||||
|
||||
// print help
|
||||
if (opts.help) {
|
||||
|
@ -150,6 +145,12 @@ export async function main(args: string[]): Promise<number> {
|
|||
warning((err as Error).toString())
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// loop over the tools and run their setup function
|
||||
for (const tool of tools) {
|
||||
// get the version or "true" or undefined for this tool from the options
|
||||
|
@ -163,7 +164,6 @@ export async function main(args: string[]): Promise<number> {
|
|||
try {
|
||||
let installationInfo: InstallationInfo | undefined | void
|
||||
if (tool === "vcvarsall") {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
setupVCVarsall(getVersion(tool, version, osVersion), undefined, arch, undefined, undefined, false, false)
|
||||
} else {
|
||||
// get the setup function
|
||||
|
@ -293,6 +293,21 @@ main(process.argv)
|
|||
process.exitCode = 1
|
||||
})
|
||||
|
||||
export type Opts = mri.Argv<
|
||||
Record<Inputs, string | undefined> & {
|
||||
help: boolean
|
||||
}
|
||||
>
|
||||
|
||||
export function parseArgs(args: string[]): Opts {
|
||||
return mri<Record<Inputs, string | undefined> & { help: boolean }>(args, {
|
||||
string: inputs,
|
||||
default: Object.fromEntries(inputs.map((inp) => [inp, maybeGetInput(inp)])),
|
||||
alias: { h: "help" },
|
||||
boolean: "help",
|
||||
})
|
||||
}
|
||||
|
||||
/** Detecting the compiler version. Divide the given string by `-` and use the second element as the version */
|
||||
export function getCompilerInfo(maybeCompiler: string) {
|
||||
const compilerAndMaybeVersion = maybeCompiler.split("-")
|
||||
|
|
Loading…
Reference in New Issue