Skip to content

Commit 3aa347d

Browse files
authored
Merge pull request #69 from aminya/sync [skip ci]
2 parents 6251144 + aa15853 commit 3aa347d

File tree

5 files changed

+72
-13
lines changed

5 files changed

+72
-13
lines changed

dist/setup_cpp.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/setup_cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/main.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { getCompilerInfo } from "../main"
1+
import { syncVersions } from "../default_versions"
2+
import { getCompilerInfo, Inputs, parseArgs } from "../main"
23

34
jest.setTimeout(300000)
45
describe("getCompilerInfo", () => {
@@ -20,3 +21,16 @@ describe("getCompilerInfo", () => {
2021
expect(version).toBe("12")
2122
})
2223
})
24+
25+
describe("syncVersion", () => {
26+
it("Syncs llvm tools versions", async () => {
27+
const llvmTools = ["llvm", "clangtidy", "clangformat"] as Inputs[]
28+
expect(syncVersions(parseArgs(["--llvm", "14.0.0", "--clangtidy", "true"]), llvmTools)).toBe(true)
29+
expect(syncVersions(parseArgs(["--llvm", "13.0.0", "--clangtidy", "true"]), llvmTools)).toBe(true)
30+
expect(syncVersions(parseArgs(["--llvm", "13.0.0", "--clangtidy", "12.0.0"]), llvmTools)).toBe(false)
31+
32+
const opts = parseArgs(["--llvm", "14.0.0", "--clangtidy", "true"])
33+
expect(syncVersions(opts, llvmTools)).toBe(true)
34+
expect(opts.llvm).toBe(opts.clangtidy)
35+
})
36+
})

src/default_versions.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Inputs, Opts } from "./main"
2+
13
const DefaultVersions: Record<string, string> = {
24
llvm: "13.0.0", // https://github.com/llvm/llvm-project/releases
35
clangtidy: "13.0.0",
@@ -16,7 +18,7 @@ const DefaultVersions: Record<string, string> = {
1618

1719
/** Get the default version if passed true or undefined, otherwise return the version itself */
1820
export function getVersion(name: string, version: string | undefined, osVersion: number[] | null = null) {
19-
if (version === "true" || (version === undefined && name in DefaultVersions)) {
21+
if (useDefault(version, name)) {
2022
// llvm on linux
2123
if (process.platform === "linux" && ["llvm", "clangtidy", "clangformat"].includes(name)) {
2224
// choose the default version for llvm based on ubuntu
@@ -32,3 +34,31 @@ export function getVersion(name: string, version: string | undefined, osVersion:
3234
return version ?? ""
3335
}
3436
}
37+
38+
function useDefault(version: string | undefined, name: string) {
39+
return version === "true" || (version === undefined && name in DefaultVersions)
40+
}
41+
42+
export function syncVersions(opts: Opts, tools: Inputs[]): boolean {
43+
for (let i = 0; i < tools.length; i++) {
44+
// tools excluding i_tool
45+
const otherTools = tools.slice(0, i).concat(tools.slice(i + 1))
46+
47+
const tool = tools[i]
48+
49+
if (!useDefault(opts[tool], tool)) {
50+
for (let i_other = 0; i_other < otherTools.length; i_other++) {
51+
const otherTool = otherTools[i_other]
52+
const useDefaultOtherTool = useDefault(opts[otherTool], otherTools[i_other])
53+
if (useDefaultOtherTool) {
54+
// use the same version if the other tool was requested with the default
55+
opts[otherTool] = opts[tool]
56+
} else if (opts[tool] !== opts[otherTools[i_other]]) {
57+
// error if different from the other given versions
58+
return false
59+
}
60+
}
61+
}
62+
}
63+
return true
64+
}

src/main.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import numerousLocale from "numerous/locales/en.js"
3131
import { ubuntuVersion } from "./utils/env/ubuntu_version"
3232

3333
import semverValid from "semver/functions/valid"
34-
import { getVersion } from "./default_versions"
34+
import { getVersion, syncVersions } from "./default_versions"
3535
import { setupGcc } from "./gcc/gcc"
3636
import { InstallationInfo } from "./utils/setup/setupBin"
3737
import { error, info, success, warning } from "./utils/io/io"
@@ -100,7 +100,7 @@ const tools: Array<keyof typeof setups> = [
100100
]
101101

102102
/** The possible inputs to the program */
103-
type Inputs = keyof typeof setups | "compiler" | "architecture"
103+
export type Inputs = keyof typeof setups | "compiler" | "architecture"
104104

105105
// an array of possible inputs
106106
const inputs: Array<Inputs> = ["compiler", "architecture", ...tools]
@@ -112,12 +112,7 @@ export async function main(args: string[]): Promise<number> {
112112
}
113113

114114
// parse options using mri or github actions
115-
const opts = mri<Record<Inputs, string | undefined> & { help: boolean }>(args, {
116-
string: inputs,
117-
default: Object.fromEntries(inputs.map((inp) => [inp, maybeGetInput(inp)])),
118-
alias: { h: "help" },
119-
boolean: "help",
120-
})
115+
const opts = parseArgs(args)
121116

122117
// print help
123118
if (opts.help) {
@@ -150,6 +145,12 @@ export async function main(args: string[]): Promise<number> {
150145
warning((err as Error).toString())
151146
}
152147

148+
// sync the version for the llvm tools
149+
if (!syncVersions(opts, ["llvm", "clangtidy", "clangformat"])) {
150+
error("The same version must be used for llvm, clangformat and clangtidy")
151+
return 1
152+
}
153+
153154
// loop over the tools and run their setup function
154155
for (const tool of tools) {
155156
// 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> {
163164
try {
164165
let installationInfo: InstallationInfo | undefined | void
165166
if (tool === "vcvarsall") {
166-
// eslint-disable-next-line no-await-in-loop
167167
setupVCVarsall(getVersion(tool, version, osVersion), undefined, arch, undefined, undefined, false, false)
168168
} else {
169169
// get the setup function
@@ -293,6 +293,21 @@ main(process.argv)
293293
process.exitCode = 1
294294
})
295295

296+
export type Opts = mri.Argv<
297+
Record<Inputs, string | undefined> & {
298+
help: boolean
299+
}
300+
>
301+
302+
export function parseArgs(args: string[]): Opts {
303+
return mri<Record<Inputs, string | undefined> & { help: boolean }>(args, {
304+
string: inputs,
305+
default: Object.fromEntries(inputs.map((inp) => [inp, maybeGetInput(inp)])),
306+
alias: { h: "help" },
307+
boolean: "help",
308+
})
309+
}
310+
296311
/** Detecting the compiler version. Divide the given string by `-` and use the second element as the version */
297312
export function getCompilerInfo(maybeCompiler: string) {
298313
const compilerAndMaybeVersion = maybeCompiler.split("-")

0 commit comments

Comments
 (0)