11import execa from "execa"
22import { join } from "path"
3- import untildify from "untildify"
43import which from "which"
54import { setupCmake } from "../cmake/cmake"
65import { getVersion } from "../default_versions"
7- import { execSudo } from "../utils/exec/sudo"
86import { addBinExtension } from "../utils/extension/extension"
97import { extractTarByExe } from "../utils/setup/extract"
108import { setupAptPack } from "../utils/setup/setupAptPack"
119import { setupPacmanPack } from "../utils/setup/setupPacmanPack"
12- import { PackageInfo , setupBin } from "../utils/setup/setupBin"
10+ import { InstallationInfo , PackageInfo , setupBin } from "../utils/setup/setupBin"
1311import { isArch } from "../utils/env/isArch"
1412import { hasDnf } from "../utils/env/hasDnf"
1513import { setupDnfPack } from "../utils/setup/setupDnfPack"
1614import { 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"
1719
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 ,
2327 }
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 ,
4037 }
4138}
4239
4340async function buildKcov ( file : string , dest : string ) {
4441 const out = await extractTarByExe ( file , dest , [ "--strip-components=1" ] )
42+
4543 // 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+
4946 if ( process . platform === "linux" ) {
5047 if ( isArch ( ) ) {
5148 setupPacmanPack ( "libdwarf" )
@@ -58,27 +55,59 @@ async function buildKcov(file: string, dest: string) {
5855 setupAptPack ( "libcurl4-openssl-dev" )
5956 }
6057 }
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
6466 return out
6567}
6668
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" )
82107 }
108+ return installationInfo
109+ } else {
110+ installationInfo = await setupBin ( "kcov" , version , getBuildKcovPackageInfo , setupDir , arch )
83111 }
112+ return installationInfo
84113}
0 commit comments