11import { join } from "path"
2- import { endGroup , notice , startGroup } from "@actions/core"
2+ import { endGroup , startGroup } from "@actions/core"
33import { error , info } from "ci-log"
4- import { addEnv } from "envosman"
54import semverValid from "semver/functions/valid"
6- import { getSuccessMessage , rcOptions } from "./cli-options.js"
5+ import { getSuccessMessage } from "./cli-options.js"
76import { setupGcc , setupMingw } from "./gcc/gcc.js"
87import { activateGcovGCC , activateGcovLLVM } from "./gcovr/gcovr.js"
8+ import { setupAppleClang } from "./llvm/apple-clang.js"
99import { setupLLVM } from "./llvm/llvm.js"
1010import { setupMSVC } from "./msvc/msvc.js"
11+ import { appleClangSetups , gccSetups , llvmSetups , mingwSetups , msvcSetups } from "./tool.js"
12+ import type { InstallationInfo } from "./utils/setup/setupBin.js"
1113import { getVersion } from "./versions/versions.js"
1214
13- /** Detecting the compiler version. Divide the given string by `-` and use the second element as the version */
15+ /**
16+ * Detecting the compiler version. Divide the given string by `-` and use the second element as the version
17+ *
18+ * @param compilerAndVersion - The compiler and version string
19+ * @returns The compiler and version
20+ *
21+ * @nothrow It doesn't throw any error, but it logs the error if it fails to parse the compiler info
22+ */
1423export function getCompilerInfo ( compilerAndVersion : string ) {
15- const compilerAndMaybeVersion = compilerAndVersion . split ( "-" )
16- const compiler = compilerAndMaybeVersion [ 0 ]
17- if ( 1 in compilerAndMaybeVersion ) {
18- const maybeVersion = compilerAndMaybeVersion [ 1 ]
19- if ( semverValid ( maybeVersion ) !== null ) {
20- return { compiler , version : maybeVersion }
21- } else {
22- info ( `Invalid semver version ${ maybeVersion } used for the compiler.` )
24+ try {
25+ const compilerAndMaybeVersion = compilerAndVersion . split ( "-" )
26+ const compiler = compilerAndMaybeVersion [ 0 ]
27+ if ( 1 in compilerAndMaybeVersion ) {
28+ const maybeVersion = compilerAndMaybeVersion [ 1 ]
29+ if ( semverValid ( maybeVersion ) === null ) {
30+ info ( `Invalid semver version ${ maybeVersion } used for the compiler.` )
31+ }
2332 return { compiler, version : maybeVersion }
2433 }
34+ return { compiler, version : undefined }
35+ } catch ( err ) {
36+ error ( `Failed to parse the compiler info ${ compilerAndVersion } : ${ err } ` )
37+ return { compiler : compilerAndVersion , version : undefined }
2538 }
26- return { compiler, version : undefined }
2739}
2840
2941/** Installing the specified compiler */
@@ -35,73 +47,47 @@ export async function installCompiler(
3547 successMessages : string [ ] ,
3648 errorMessages : string [ ] ,
3749) {
38- try {
39- const { compiler, version } = getCompilerInfo ( compilerAndVersion )
50+ const { compiler, version } = getCompilerInfo ( compilerAndVersion )
4051
52+ let installationInfo : InstallationInfo | undefined | void | null // null means the compiler is not supported
53+ try {
4154 // install the compiler. We allow some aliases for the compiler name
4255 startGroup ( `Installing ${ compiler } ${ version ?? "" } ` )
43- switch ( compiler ) {
44- case "llvm" :
45- case "clang" :
46- case "clang++" : {
47- const installationInfo = await setupLLVM (
48- getVersion ( "llvm" , version , osVersion ) ,
49- join ( setupCppDir , "llvm" ) ,
50- arch ,
51- )
52-
53- await activateGcovLLVM ( )
54-
55- successMessages . push ( getSuccessMessage ( "llvm" , installationInfo ) )
56- break
57- }
58- case "gcc" :
59- case "mingw" :
60- case "cygwin" :
61- case "msys" : {
62- const gccVersion = compiler === "mingw"
63- ? getVersion ( "mingw" , version , osVersion )
64- : getVersion ( "gcc" , version , osVersion )
65- const installationInfo = compiler === "mingw"
66- ? await setupMingw ( gccVersion , join ( setupCppDir , "gcc" ) , arch )
67- : await setupGcc ( gccVersion , join ( setupCppDir , "gcc" ) , arch )
68-
69- await activateGcovGCC ( gccVersion )
70-
71- successMessages . push ( getSuccessMessage ( "gcc" , installationInfo ) )
72- break
73- }
74- case "cl" :
75- case "msvc" :
76- case "msbuild" :
77- case "vs" :
78- case "visualstudio" :
79- case "visualcpp" :
80- case "visualc++" : {
81- const installationInfo = await setupMSVC (
82- getVersion ( "msvc" , version , osVersion ) ,
83- join ( setupCppDir , "msvc" ) ,
84- arch ,
85- )
86-
87- successMessages . push ( getSuccessMessage ( "msvc" , installationInfo ) )
88- break
89- }
90- case "appleclang" :
91- case "applellvm" : {
92- notice ( "Assuming apple-clang is already installed" )
93- await Promise . all ( [ addEnv ( "CC" , "clang" , rcOptions ) , addEnv ( "CXX" , "clang++" , rcOptions ) ] )
94- successMessages . push ( getSuccessMessage ( "apple-clang" , undefined ) )
95- break
96- }
97- default : {
98- errorMessages . push ( `Unsupported compiler ${ compiler } ` )
99- }
56+ if ( compiler in llvmSetups ) {
57+ installationInfo = await setupLLVM (
58+ getVersion ( "llvm" , version , osVersion ) ,
59+ join ( setupCppDir , "llvm" ) ,
60+ arch ,
61+ )
62+ await activateGcovLLVM ( )
63+ } else if ( compiler in gccSetups ) {
64+ const gccVersion = getVersion ( "gcc" , version , osVersion )
65+ installationInfo = await setupGcc ( gccVersion , join ( setupCppDir , "gcc" ) , arch )
66+ await activateGcovGCC ( gccVersion )
67+ } else if ( compiler in mingwSetups ) {
68+ const gccVersion = getVersion ( "mingw" , version , osVersion )
69+ installationInfo = await setupMingw ( gccVersion , join ( setupCppDir , "gcc" ) , arch )
70+ await activateGcovGCC ( gccVersion )
71+ } else if ( compiler in msvcSetups ) {
72+ installationInfo = await setupMSVC (
73+ getVersion ( "msvc" , version , osVersion ) ,
74+ join ( setupCppDir , "msvc" ) ,
75+ arch ,
76+ )
77+ } else if ( compiler in appleClangSetups ) {
78+ await setupAppleClang ( )
79+ } else {
80+ installationInfo = null
81+ errorMessages . push ( `Unsupported compiler ${ compiler } ` )
10082 }
10183 } catch ( err ) {
10284 error ( err as string | Error )
10385 errorMessages . push ( `Failed to install the ${ compilerAndVersion } ` )
10486 }
10587
88+ if ( installationInfo !== null ) {
89+ successMessages . push ( getSuccessMessage ( compiler , installationInfo ) )
90+ }
91+
10692 endGroup ( )
10793}
0 commit comments