Skip to content

Commit

Permalink
Merge pull request #341 from aminya/compiler-version [skip ci]
Browse files Browse the repository at this point in the history
fix: more robust parsing of compiler name/versions
  • Loading branch information
aminya authored Feb 16, 2025
2 parents 49c8316 + 3290875 commit 68aea2b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 28 deletions.
2 changes: 1 addition & 1 deletion dist/legacy/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/legacy/setup-cpp.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/modern/setup-cpp.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/modern/setup-cpp.mjs.map

Large diffs are not rendered by default.

50 changes: 36 additions & 14 deletions src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,47 @@ describe("getCompilerInfo", () => {
expect(compiler).toBe("llvm")
expect(version).toBe("12")
})
})

describe("getCompilerInfo", () => {
it("getCompilerInfo with semver", () => {
const { compiler, version } = getCompilerInfo("llvm-12.0.0")
expect(compiler).toBe("llvm")
expect(version).toBe("12.0.0")
it("extracts gcc version", () => {
const { compiler, version } = getCompilerInfo("gcc-11.2.0")
expect(compiler).toBe("gcc")
expect(version).toBe("11.2.0")
})

it("getCompilerInfo with major version", () => {
const { compiler, version } = getCompilerInfo("llvm-12")
expect(compiler).toBe("llvm")
expect(version).toBe("12")
it("extracts apple-clang version", () => {
const { compiler, version } = getCompilerInfo("apple-clang-14")
expect(compiler).toBe("apple-clang")
expect(version).toBe("14")
})

it("getCompilerInfo without version", () => {
const { compiler, version } = getCompilerInfo("llvm")
expect(compiler).toBe("llvm")
expect(version).toBeUndefined()
it("extracts msvc version", () => {
const { compiler, version } = getCompilerInfo("msvc-14.16.27023")
expect(compiler).toBe("msvc")
expect(version).toBe("14.16.27023")
})

it("extracts msvc version with year", () => {
const { compiler, version } = getCompilerInfo("msvc-2017")
expect(compiler).toBe("msvc")
expect(version).toBe("2017")
})

it("extracts msvc version with year and version", () => {
const { compiler, version } = getCompilerInfo("msvc-2017.1")
expect(compiler).toBe("msvc")
expect(version).toBe("2017.1")
})

it("extracts gcc version with pre-release and build metadata", () => {
const { compiler, version } = getCompilerInfo("gcc-11.2.0-beta.1+build.123")
expect(compiler).toBe("gcc")
expect(version).toBe("11.2.0-beta.1+build.123")
})

it("extracts clang version with pre-release", () => {
const { compiler, version } = getCompilerInfo("clang-15.0.0-rc1")
expect(compiler).toBe("clang")
expect(version).toBe("15.0.0-rc1")
})
})

Expand Down
35 changes: 25 additions & 10 deletions src/compilers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,18 @@ export type CompilerInfo = {
}

/**
* Detecting the compiler version. Divide the given string by `-` and use the second element as the version
* Match version patterns like:
* - Standard versions: digit.digit.digit
* - Semver with pre-release: digit.digit.digit-alpha.1
* - Semver with build metadata: digit.digit.digit+build.123
* - MSVC style: digit.digit.digit.digit
* - Year versions: 2015, 2017, etc.
*/
const versionPattern = /[.-]((?:\d{4}|\d+(?:\.\d+)*(?:-[\w.-]+)?(?:\+[\w.-]+)?)$)/

/**
* Detecting the compiler version by looking for a version-like pattern.
* Supports compiler names that contain hyphens and various version formats.
*
* @param compilerAndVersion - The compiler and version string
* @returns The compiler and version
Expand All @@ -28,16 +39,20 @@ export type CompilerInfo = {
*/
export function getCompilerInfo(compilerAndVersion: string): CompilerInfo {
try {
const compilerAndMaybeVersion = compilerAndVersion.split("-")
const compiler = compilerAndMaybeVersion[0]
if (1 in compilerAndMaybeVersion) {
const maybeVersion = compilerAndMaybeVersion[1]
if (semverValid(maybeVersion) === null) {
info(`Invalid semver version ${maybeVersion} used for the compiler.`)
}
return { compiler, version: maybeVersion }
const match = compilerAndVersion.match(versionPattern)

if (match === null) {
return { compiler: compilerAndVersion, version: undefined }
}

const version = match[1]
const compiler = compilerAndVersion.slice(0, match.index).replace(/[.-]$/, "")

// Only check semver for non-year versions
if (!version.match(/^\d{4}$/) && semverValid(version) === null) {
info(`Non-semver version format: ${version}`)
}
return { compiler, version: undefined }
return { compiler, version }
} catch (err) {
error(`Failed to parse the compiler info ${compilerAndVersion}: ${err}`)
return { compiler: compilerAndVersion, version: undefined }
Expand Down

0 comments on commit 68aea2b

Please sign in to comment.