Skip to content

Commit a9e484f

Browse files
authored
Merge pull request #107 from aminya/kcov [skip ci]
2 parents 8fb789d + de92c65 commit a9e484f

File tree

11 files changed

+122
-56
lines changed

11 files changed

+122
-56
lines changed

cspell.config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ words:
2222
- CPPFLAGS
2323
- cpprc
2424
- Cpython
25+
- DCMAKE
2526
- deps
2627
- devel
2728
- dyld

dist/setup_cpp.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/setup_cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/setup_cpp.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/setup_cpp.mjs.map

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/default_versions.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ const DefaultUbuntuVersion: Record<string, Record<number, string>> = {
6363
16: "legacy",
6464
14: "legacy",
6565
},
66+
kcov: {
67+
22: "40",
68+
20: "40-binary", // https://github.com/SimonKagstrom/kcov/releases
69+
18: "40",
70+
16: "40",
71+
14: "40",
72+
},
6673
}
6774

6875
/** Get the default version if passed true or undefined, otherwise return the version itself */

src/kcov/__tests__/kcov.test.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,35 @@ import { setupKcov } from "../kcov"
22
import { setupTmpDir, cleanupTmpDir, testBin } from "../../utils/tests/test-helpers"
33
import { InstallationInfo } from "../../utils/setup/setupBin"
44
import which from "which"
5+
import { info } from "@actions/core"
56

67
jest.setTimeout(300000)
78
describe("setup-Kcov", () => {
89
if (process.platform !== "linux") {
9-
it.todo("should setup kcov on non-linux")
10+
it.todo("should setup kcov on non-Windows")
1011
return
1112
}
1213

13-
it("should setup Kcov v40", async () => {
14+
it("should setup Kcov v40 via downloading the binaries", async () => {
15+
const directory = await setupTmpDir("kcov-v40")
16+
const { binDir } = (await setupKcov("40-binary", directory, "")) as InstallationInfo
17+
// the prebuild binary only works on ubuntu 20.04
18+
try {
19+
await testBin("kcov", ["--version"], binDir)
20+
} catch (err) {
21+
info((err as Error).message)
22+
}
23+
await cleanupTmpDir("kcov-v40")
24+
})
25+
26+
it("should build and setup Kcov v40", async () => {
1427
const directory = await setupTmpDir("kcov-v40")
1528
const { binDir } = (await setupKcov("40", directory, "")) as InstallationInfo
1629
await testBin("kcov", ["--version"], binDir)
1730
await cleanupTmpDir("kcov-v40")
1831
})
1932

20-
it("should setup Kcov v39", async () => {
33+
it("should build and setup Kcov v39", async () => {
2134
const directory = await setupTmpDir("kcov-v39")
2235
const { binDir } = (await setupKcov("39", directory, "")) as InstallationInfo
2336
await testBin("kcov", ["--version"], binDir)
@@ -32,7 +45,7 @@ describe("setup-Kcov", () => {
3245
// await cleanupTmpDir("kcov-v39")
3346
// })
3447

35-
it("should setup Kcov v38", async () => {
48+
it("should build and setup Kcov v38", async () => {
3649
try {
3750
const directory2 = await setupTmpDir("kcov-v38")
3851

src/kcov/kcov.ts

Lines changed: 74 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,48 @@
11
import execa from "execa"
22
import { join } from "path"
3-
import untildify from "untildify"
43
import which from "which"
54
import { setupCmake } from "../cmake/cmake"
65
import { getVersion } from "../default_versions"
7-
import { execSudo } from "../utils/exec/sudo"
86
import { addBinExtension } from "../utils/extension/extension"
97
import { extractTarByExe } from "../utils/setup/extract"
108
import { setupAptPack } from "../utils/setup/setupAptPack"
119
import { setupPacmanPack } from "../utils/setup/setupPacmanPack"
12-
import { PackageInfo, setupBin } from "../utils/setup/setupBin"
10+
import { InstallationInfo, PackageInfo, setupBin } from "../utils/setup/setupBin"
1311
import { isArch } from "../utils/env/isArch"
1412
import { hasDnf } from "../utils/env/hasDnf"
1513
import { setupDnfPack } from "../utils/setup/setupDnfPack"
1614
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"
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

4340
async 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
}

src/utils/setup/setupBin.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ export async function setupBin(
8282
}
8383
}
8484

85-
const installDir = join(setupDir, extractedFolderName)
86-
const binDir = join(installDir, binRelativeDir)
85+
let installDir = join(setupDir, extractedFolderName)
86+
let binDir = join(installDir, binRelativeDir)
8787
const binFile = join(binDir, binFileName)
8888

8989
// download ane extract the package into the installation directory.
@@ -114,8 +114,12 @@ export async function setupBin(
114114
try {
115115
const downloaded = await downloadTool(url)
116116
await extractFunction?.(downloaded, setupDir)
117+
// if (typeof extractedBinDir === "string") {
118+
// binDir = extractedBinDir
119+
// installDir = extractedBinDir
120+
// }
117121
} catch (err) {
118-
throw new Error(`Failed to download ${name} ${version} ${arch}: ${err}`)
122+
throw new Error(`Failed to download ${name} ${version} ${arch} from ${url}: ${err}`)
119123
}
120124
}
121125

src/utils/setup/version.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,14 @@ export function semverCoerceIfInvalid(version: string) {
115115
}
116116
return version
117117
}
118+
119+
export function removeVPrefix(version: string) {
120+
return parseInt(version.replace(/^v/, ""), 10)
121+
}
122+
123+
export function addVPrefix(version: string) {
124+
if (!version.match(/^v/)) {
125+
return `v${version}`
126+
}
127+
return version
128+
}

0 commit comments

Comments
 (0)