1
1
import execa from "execa"
2
2
import { join } from "path"
3
- import untildify from "untildify"
4
3
import which from "which"
5
4
import { setupCmake } from "../cmake/cmake"
6
5
import { getVersion } from "../default_versions"
7
- import { execSudo } from "../utils/exec/sudo"
8
6
import { addBinExtension } from "../utils/extension/extension"
9
7
import { extractTarByExe } from "../utils/setup/extract"
10
8
import { setupAptPack } from "../utils/setup/setupAptPack"
11
9
import { setupPacmanPack } from "../utils/setup/setupPacmanPack"
12
- import { PackageInfo , setupBin } from "../utils/setup/setupBin"
10
+ import { InstallationInfo , PackageInfo , setupBin } from "../utils/setup/setupBin"
13
11
import { isArch } from "../utils/env/isArch"
14
12
import { hasDnf } from "../utils/env/hasDnf"
15
13
import { setupDnfPack } from "../utils/setup/setupDnfPack"
16
14
import { isUbuntu } from "../utils/env/isUbuntu"
15
+ import { addVPrefix , removeVPrefix } from "../utils/setup/version"
16
+ import { info } from "../utils/io/io"
17
+ import { untildify_user } from "../utils/path/untildify"
18
+ import { setupNinja } from "../ninja/ninja"
17
19
18
- function getKcovPackageInfo ( version : string ) : PackageInfo {
19
- const version_number = parseInt ( version . replace ( / ^ v / , "" ) , 10 )
20
- if ( version_number === 38 ) {
21
- // eslint-disable-next-line no-param-reassign
22
- version = "v38"
20
+ function getDownloadKcovPackageInfo ( version : string ) : PackageInfo {
21
+ return {
22
+ url : `https://github.com/SimonKagstrom/kcov/releases/download/${ version } /kcov-amd64.tar.gz` ,
23
+ extractedFolderName : "" ,
24
+ binRelativeDir : "usr/local/bin" ,
25
+ binFileName : addBinExtension ( "kcov" ) ,
26
+ extractFunction : extractTarByExe ,
23
27
}
24
- if ( version_number >= 39 ) {
25
- return {
26
- url : `https://github.com/SimonKagstrom/kcov/releases/download/v${ version_number } /kcov-amd64.tar.gz` ,
27
- extractedFolderName : "" ,
28
- binRelativeDir : "usr/local/bin" ,
29
- binFileName : addBinExtension ( "kcov" ) ,
30
- extractFunction : extractTarByExe ,
31
- }
32
- } else {
33
- return {
34
- url : `https://github.com/SimonKagstrom/kcov/archive/refs/tags/${ version } .tar.gz` ,
35
- extractedFolderName : `kcov-${ version_number } ` ,
36
- binRelativeDir : "build/" ,
37
- binFileName : addBinExtension ( "kcov" ) ,
38
- extractFunction : buildKcov ,
39
- }
28
+ }
29
+
30
+ function getBuildKcovPackageInfo ( version : string ) : PackageInfo {
31
+ return {
32
+ url : `https://github.com/SimonKagstrom/kcov/archive/refs/tags/${ version } .tar.gz` ,
33
+ extractedFolderName : "" ,
34
+ binRelativeDir : "build/src" ,
35
+ binFileName : addBinExtension ( "kcov" ) ,
36
+ extractFunction : buildKcov ,
40
37
}
41
38
}
42
39
43
40
async function buildKcov ( file : string , dest : string ) {
44
41
const out = await extractTarByExe ( file , dest , [ "--strip-components=1" ] )
42
+
45
43
// build after extraction using CMake
46
- if ( which . sync ( "cmake" , { nothrow : true } ) === null ) {
47
- await setupCmake ( getVersion ( "cmake" , undefined ) , join ( untildify ( "" ) , "cmake" ) , "" )
48
- }
44
+ let cmake = await getCmake ( )
45
+
49
46
if ( process . platform === "linux" ) {
50
47
if ( isArch ( ) ) {
51
48
setupPacmanPack ( "libdwarf" )
@@ -58,27 +55,59 @@ async function buildKcov(file: string, dest: string) {
58
55
setupAptPack ( "libcurl4-openssl-dev" )
59
56
}
60
57
}
61
- await execa ( "cmake" , [ "-S" , "./" , "-B" , "./build" ] , { cwd : out , stdio : "inherit" } )
62
- await execa ( "cmake" , [ "--build" , "./build" , "--config" , "Release" ] , { cwd : out , stdio : "inherit" } )
63
- execSudo ( "cmake" , [ "--install" , "./build" ] , out )
58
+ const buildDir = join ( out , "build" )
59
+ await execa ( cmake , [ "-S" , out , "-B" , buildDir , "-DCMAKE_BUILD_TYPE=Release" , "-G" , "Ninja" ] , {
60
+ cwd : out ,
61
+ stdio : "inherit" ,
62
+ } )
63
+ await execa ( cmake , [ "--build" , buildDir , "--config" , "Release" ] , { cwd : out , stdio : "inherit" } )
64
+ // execSudo(cmake, ["--install", buildDir], out)
65
+ // return "user/local/bin" // the cmake install prefix
64
66
return out
65
67
}
66
68
67
- export async function setupKcov ( version : string , setupDir : string , arch : string ) {
68
- switch ( process . platform ) {
69
- case "linux" : {
70
- const installationInfo = await setupBin ( "kcov" , version , getKcovPackageInfo , setupDir , arch )
71
- if ( isArch ( ) ) {
72
- setupPacmanPack ( "binutils" )
73
- } else if ( hasDnf ( ) ) {
74
- setupDnfPack ( "binutils" )
75
- } else if ( isUbuntu ( ) ) {
76
- setupAptPack ( "libbinutils" )
77
- }
78
- return installationInfo
79
- }
80
- default : {
81
- throw new Error ( `Unsupported platform for ${ arch } ` )
69
+ async function getCmake ( ) {
70
+ let cmake = which . sync ( "cmake" , { nothrow : true } )
71
+ if ( cmake === null ) {
72
+ const { binDir } = await setupCmake ( getVersion ( "cmake" , undefined ) , join ( untildify_user ( "" ) , "cmake" ) , "" )
73
+ cmake = join ( binDir , "cmake" )
74
+ }
75
+ let ninja = which . sync ( "ninja" , { nothrow : true } )
76
+ if ( ninja === null ) {
77
+ await setupNinja ( getVersion ( "ninja" , undefined ) , join ( untildify_user ( "" ) , "ninja" ) , "" )
78
+ }
79
+ return cmake
80
+ }
81
+
82
+ export async function setupKcov ( versionGiven : string , setupDir : string , arch : string ) {
83
+ if ( process . platform !== "linux" ) {
84
+ info ( "Kcov is not supported on non-linux" )
85
+ return
86
+ }
87
+
88
+ // parse version
89
+ const versionSplit = versionGiven . split ( "-" )
90
+ let version = addVPrefix ( versionSplit [ 0 ] )
91
+ const installMethod = versionSplit [ 1 ] as "binary" | undefined
92
+ const version_number = removeVPrefix ( version )
93
+ // fix inconsistency in tagging
94
+ if ( version_number === 38 ) {
95
+ version = "v38"
96
+ }
97
+
98
+ let installationInfo : InstallationInfo
99
+ if ( installMethod === "binary" && version_number >= 39 ) {
100
+ installationInfo = await setupBin ( "kcov" , version , getDownloadKcovPackageInfo , setupDir , arch )
101
+ if ( isArch ( ) ) {
102
+ setupPacmanPack ( "binutils" )
103
+ } else if ( hasDnf ( ) ) {
104
+ setupDnfPack ( "binutils" )
105
+ } else if ( isUbuntu ( ) ) {
106
+ setupAptPack ( "libbinutils" )
82
107
}
108
+ return installationInfo
109
+ } else {
110
+ installationInfo = await setupBin ( "kcov" , version , getBuildKcovPackageInfo , setupDir , arch )
83
111
}
112
+ return installationInfo
84
113
}
0 commit comments