Skip to content

Commit 3a86553

Browse files
committed
fix: sync LLVM compiler version and clang tools version
1 parent 5d12f41 commit 3a86553

File tree

8 files changed

+101
-33
lines changed

8 files changed

+101
-33
lines changed

dist/legacy/setup-cpp.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.mjs

+1-1
Large diffs are not rendered by default.

dist/modern/setup-cpp.mjs.map

+1-1
Large diffs are not rendered by default.

src/__tests__/main.test.ts

+32-2
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,39 @@ describe("getCompilerInfo", () => {
2525
})
2626
})
2727

28+
describe("getCompilerInfo", () => {
29+
it("getCompilerInfo with semver", () => {
30+
const { compiler, version } = getCompilerInfo("llvm-12.0.0")
31+
expect(compiler).toBe("llvm")
32+
expect(version).toBe("12.0.0")
33+
})
34+
35+
it("getCompilerInfo with major version", () => {
36+
const { compiler, version } = getCompilerInfo("llvm-12")
37+
expect(compiler).toBe("llvm")
38+
expect(version).toBe("12")
39+
})
40+
41+
it("getCompilerInfo without version", () => {
42+
const { compiler, version } = getCompilerInfo("llvm")
43+
expect(compiler).toBe("llvm")
44+
expect(version).toBeUndefined()
45+
})
46+
})
47+
2848
describe("syncVersion", () => {
2949
it("Syncs llvm tools versions", () => {
3050
expect(syncVersions(parseArgs(["--llvm", "14.0.0", "--clangtidy", "true"]), llvmTools as Inputs[])).toBe(true)
3151
expect(syncVersions(parseArgs(["--llvm", "13.0.0", "--clangtidy", "true"]), llvmTools as Inputs[])).toBe(true)
52+
expect(syncVersions(parseArgs(["--compiler", "llvm-13.0.0", "--clangtidy", "true"]), llvmTools as Inputs[])).toBe(
53+
true,
54+
)
3255
expect(syncVersions(parseArgs(["--llvm", "13.0.0", "--clangtidy", "12.0.0"]), llvmTools as Inputs[])).toBe(false)
3356

3457
const opts1 = parseArgs(["--llvm", "14.0.0", "--clangtidy", "true"])
3558
expect(syncVersions(opts1, llvmTools as Inputs[])).toBe(true)
36-
expect(opts1.llvm).toBe(opts1.clangtidy)
59+
expect(opts1.llvm).toBe("14.0.0")
60+
expect(opts1.clangtidy).toBe("14.0.0")
3761
expect(opts1.clangformat).toBe(undefined)
3862

3963
const opts2 = parseArgs(["--clangtidy", "15.0.0", "--clangformat", "true"])
@@ -45,8 +69,14 @@ describe("syncVersion", () => {
4569
const opts3 = parseArgs(["--llvm", "true", "--clangformat", "true"])
4670
expect(syncVersions(opts3, llvmTools as Inputs[])).toBe(true)
4771
expect(opts3.llvm).toBe("true")
48-
expect(opts3.clangtidy).toBe(undefined)
4972
expect(opts3.clangformat).toBe("true")
73+
expect(opts3.clangtidy).toBe(undefined)
74+
75+
const opts4 = parseArgs(["--compiler", "llvm-13.0.0", "--clangtidy", "true"])
76+
expect(syncVersions(opts4, [...llvmTools, "compiler"] as Inputs[], getCompilerInfo("llvm-13.0.0"))).toBe(true)
77+
expect(opts4.compiler).toBe("llvm-13.0.0")
78+
expect(opts4.clangtidy).toBe("13.0.0")
79+
expect(opts4.clangformat).toBe(undefined)
5080
})
5181
})
5282

src/compilers.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import { appleClangSetups, gccSetups, llvmSetups, mingwSetups, msvcSetups } from
1212
import type { InstallationInfo } from "./utils/setup/setupBin.js"
1313
import { getVersion } from "./versions/versions.js"
1414

15+
export type CompilerInfo = {
16+
compiler: string
17+
version: string | undefined
18+
}
19+
1520
/**
1621
* Detecting the compiler version. Divide the given string by `-` and use the second element as the version
1722
*
@@ -20,7 +25,7 @@ import { getVersion } from "./versions/versions.js"
2025
*
2126
* @nothrow It doesn't throw any error, but it logs the error if it fails to parse the compiler info
2227
*/
23-
export function getCompilerInfo(compilerAndVersion: string) {
28+
export function getCompilerInfo(compilerAndVersion: string): CompilerInfo {
2429
try {
2530
const compilerAndMaybeVersion = compilerAndVersion.split("-")
2631
const compiler = compilerAndMaybeVersion[0]
@@ -40,15 +45,14 @@ export function getCompilerInfo(compilerAndVersion: string) {
4045

4146
/** Installing the specified compiler */
4247
export async function installCompiler(
43-
compilerAndVersion: string,
48+
compiler: string,
49+
version: string | undefined,
4450
osVersion: number[] | null,
4551
setupCppDir: string,
4652
arch: string,
4753
successMessages: string[],
4854
errorMessages: string[],
4955
) {
50-
const { compiler, version } = getCompilerInfo(compilerAndVersion)
51-
5256
let installationInfo: InstallationInfo | undefined | void | null // null means the compiler is not supported
5357
try {
5458
// install the compiler. We allow some aliases for the compiler name
@@ -82,7 +86,7 @@ export async function installCompiler(
8286
}
8387
} catch (err) {
8488
error(err as string | Error)
85-
errorMessages.push(`Failed to install the ${compilerAndVersion}`)
89+
errorMessages.push(`Failed to install the ${compiler} ${version}`)
8690
}
8791

8892
if (installationInfo !== null) {

src/setup-cpp.ts

+21-16
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import timeDeltaLocale from "time-delta/locales/en.js"
1111
import { untildifyUser } from "untildify-user"
1212
import { checkUpdates } from "./check-updates.js"
1313
import { parseArgs, printHelp, rcOptions } from "./cli-options.js"
14-
import { installCompiler } from "./compilers.js"
14+
import { getCompilerInfo, installCompiler } from "./compilers.js"
1515
import { installTool } from "./installTool.js"
1616
import { type Inputs, llvmTools, tools } from "./tool.js"
1717
import { isArch } from "./utils/env/isArch.js"
@@ -55,8 +55,10 @@ async function main(args: string[]): Promise<number> {
5555

5656
const osVersion = await ubuntuVersion()
5757

58+
const compilerInfo = opts.compiler !== undefined ? getCompilerInfo(opts.compiler) : undefined
59+
5860
// sync the version for the llvm tools
59-
if (!syncVersions(opts, llvmTools as Inputs[])) {
61+
if (!syncVersions(opts, [...llvmTools, "compiler"] as Inputs[], compilerInfo)) {
6062
error("The same version must be used for llvm, clang-format and clang-tidy")
6163
return 1
6264
}
@@ -71,11 +73,9 @@ async function main(args: string[]): Promise<number> {
7173
let failedFast = false
7274
for (const tool of tools) {
7375
// fail fast inside CI when any tool fails
74-
if (isCI) {
75-
if (errorMessages.length !== 0) {
76-
failedFast = true
77-
break
78-
}
76+
if (isCI && errorMessages.length !== 0) {
77+
failedFast = true
78+
break
7979
}
8080

8181
// get the version or "true" or undefined for this tool from the options
@@ -101,15 +101,20 @@ async function main(args: string[]): Promise<number> {
101101
}
102102
}
103103

104-
if (!failedFast) {
105-
// installing the specified compiler
106-
const maybeCompiler = opts.compiler
107-
if (maybeCompiler !== undefined) {
108-
const time1Compiler = Date.now()
109-
await installCompiler(maybeCompiler, osVersion, setupCppDir, arch, successMessages, errorMessages)
110-
const time2Compiler = Date.now()
111-
info(`took ${timeFormatter.format(time1Compiler, time2Compiler) || "0 seconds"}`)
112-
}
104+
if (!failedFast && compilerInfo !== undefined) {
105+
// install the specified compiler
106+
const time1Compiler = Date.now()
107+
await installCompiler(
108+
compilerInfo.compiler,
109+
compilerInfo.version,
110+
osVersion,
111+
setupCppDir,
112+
arch,
113+
successMessages,
114+
errorMessages,
115+
)
116+
const time2Compiler = Date.now()
117+
info(`took ${timeFormatter.format(time1Compiler, time2Compiler) || "0 seconds"}`)
113118
}
114119

115120
await finalizeRC(rcOptions)

src/versions/versions.ts

+35-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Opts } from "../cli-options.js"
2+
import type { CompilerInfo } from "../compilers.js"
23
import type { Inputs } from "../tool.js"
34
import { DefaultUbuntuVersion, DefaultVersions } from "./default_versions.js"
45

@@ -34,21 +35,49 @@ function getDefaultLinuxVersion(osVersion: number[], toolLinuxVersions: Record<n
3435
/**
3536
* Sync the versions for the given inputs
3637
*
38+
* It modifies the opts object to have the same version for all the tools
3739
* If the return is false, it means that versions don't match the target version
40+
* @param opts - The options object (modified in place)
41+
* @param tools - The tools to sync the versions for (it can include `compiler`)
42+
* @param compilerInfo - The compiler info to sync the versions for (if any)
3843
*/
39-
export function syncVersions(opts: Opts, tools: Inputs[]): boolean {
44+
export function syncVersions(opts: Opts, tools: Inputs[], compilerInfo: CompilerInfo | undefined = undefined): boolean {
45+
// filter out the tools that are in use in the options
4046
const toolsInUse = tools.filter((tool) => opts[tool] !== undefined)
41-
const toolsNonDefaultVersion = toolsInUse.filter((tool) => !isVersionDefault(opts[tool]))
4247

43-
const targetVersion = toolsNonDefaultVersion.length >= 1 ? opts[toolsNonDefaultVersion[0]] : "true"
48+
// filter out the tools that are not default
49+
const toolsNonDefaultVersion = toolsInUse.filter((tool) => {
50+
const version = (tool === "compiler" && compilerInfo !== undefined)
51+
? compilerInfo.version
52+
: opts[tool]
53+
return !isVersionDefault(version)
54+
})
4455

45-
if (toolsNonDefaultVersion.some((tool) => opts[tool] !== targetVersion)) {
46-
// error if any explicit versions don't match the target version
56+
// find the target version to sync to
57+
const targetVersion: string = (toolsNonDefaultVersion.length !== 0)
58+
? (toolsNonDefaultVersion[0] === "compiler" && compilerInfo !== undefined)
59+
? compilerInfo.version ?? "true"
60+
: opts[toolsNonDefaultVersion[0]] ?? "true"
61+
: "true"
62+
63+
// error if any explicit versions don't match the target version
64+
if (
65+
toolsNonDefaultVersion.some((tool) => {
66+
if (tool === "compiler" && compilerInfo !== undefined) {
67+
return opts.compiler !== `${compilerInfo.compiler}-${targetVersion}`
68+
}
69+
70+
return opts[tool] !== targetVersion
71+
})
72+
) {
4773
return false
4874
}
4975

76+
// update the version of all the tools to the target version
5077
for (const tool of toolsInUse) {
51-
opts[tool] = targetVersion
78+
opts[tool] = (tool === "compiler" && compilerInfo !== undefined)
79+
? `${compilerInfo.compiler}-${targetVersion}`
80+
: targetVersion
5281
}
5382

5483
return true

0 commit comments

Comments
 (0)