Skip to content

Commit 322f081

Browse files
authored
Merge pull request #112 from aminya/bazel [skip ci]
2 parents c8cec57 + 41af5a7 commit 322f081

File tree

11 files changed

+113
-33
lines changed

11 files changed

+113
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Setting up a **cross-platform** environment for building and testing C++/C proje
1717
| category | tools |
1818
| --------------------- | ------------------------------------------------------------ |
1919
| compiler and analyzer | llvm, gcc, msvc, vcvarsall, cppcheck, clangtidy, clangformat |
20-
| build system | cmake, ninja, meson, make, task |
20+
| build system | cmake, ninja, meson, make, task, bazel |
2121
| package manager | vcpkg, conan, choco, brew, nala |
2222
| cache | cppcache |
2323
| documentation | doxygen, graphviz |

cspell.config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ words:
1212
- aarch
1313
- aminya
1414
- applellvm
15+
- bazel
16+
- bazelisk
17+
- copr
18+
- vbatts
1519
- buildtools
1620
- caxa
1721
- ccache

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/bazel/__tests__/bazel.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { setupBazel } from "../bazel"
2+
import { testBin } from "../../utils/tests/test-helpers"
3+
import { InstallationInfo } from "../../utils/setup/setupBin"
4+
5+
jest.setTimeout(300000)
6+
describe("setup-bazel", () => {
7+
it("should setup bazel", async () => {
8+
const installInfo = await setupBazel("", "", process.arch)
9+
10+
await testBin("bazel", ["--version"], (installInfo as InstallationInfo | undefined)?.binDir)
11+
})
12+
})

src/bazel/bazel.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { addAptKeyViaDownload, setupAptPack } from "../utils/setup/setupAptPack"
2+
import { setupBrewPack } from "../utils/setup/setupBrewPack"
3+
import { setupChocoPack } from "../utils/setup/setupChocoPack"
4+
import { isArch } from "../utils/env/isArch"
5+
import { hasDnf } from "../utils/env/hasDnf"
6+
import { setupDnfPack } from "../utils/setup/setupDnfPack"
7+
import { isUbuntu } from "../utils/env/isUbuntu"
8+
import { execSudo } from "../utils/exec/sudo"
9+
10+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
11+
export async function setupBazel(version: string, _setupDir: string, _arch: string) {
12+
switch (process.platform) {
13+
case "win32": {
14+
// install bazelisk because it contains both
15+
return setupChocoPack("bazelisk", version)
16+
}
17+
case "darwin": {
18+
// install bazelisk because it contains both
19+
return setupBrewPack("bazelisk", version)
20+
}
21+
case "linux": {
22+
if (isArch()) {
23+
throw new Error("installing bazel on Arch linux is not supported yet")
24+
} else if (hasDnf()) {
25+
// https://bazel.build/install/redhat
26+
setupDnfPack("dnf-plugins-core", undefined)
27+
execSudo("dnf", ["copr", "enable", "vbatts/bazel"])
28+
return setupDnfPack("bazel4", undefined)
29+
} else if (isUbuntu()) {
30+
// https://bazel.build/install/ubuntu
31+
const keyFileName = await addAptKeyViaDownload(
32+
"bazel-archive-keyring.gpg",
33+
"https://bazel.build/bazel-release.pub.gpg"
34+
)
35+
execSudo("bash", [
36+
"-c",
37+
`echo "deb [arch=amd64 signed-by=${keyFileName}] https://storage.googleapis.com/bazel-apt stable jdk1.8" | tee /etc/apt/sources.list.d/bazel.list`,
38+
])
39+
return setupAptPack("bazel", version, [], true)
40+
}
41+
throw new Error(`Unsupported linux distribution`)
42+
}
43+
default: {
44+
throw new Error(`Unsupported platform`)
45+
}
46+
}
47+
}

src/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import { addEnv } from "./utils/env/addEnv"
4343
import { setupSevenZip } from "./sevenzip/sevenzip"
4444
import { setupGraphviz } from "./graphviz/graphviz"
4545
import { setupNala } from "./nala/nala"
46+
import { setupBazel } from "./bazel/bazel"
4647

4748
/** The setup functions */
4849
const setups = {
@@ -51,6 +52,7 @@ const setups = {
5152
ninja: setupNinja,
5253
python: setupPython,
5354
vcpkg: setupVcpkg,
55+
bazel: setupBazel,
5456
conan: setupConan,
5557
meson: setupMeson,
5658
gcovr: setupGcovr,
@@ -80,6 +82,7 @@ const tools: Array<keyof typeof setups> = [
8082
"brew",
8183
"python",
8284
"vcpkg",
85+
"bazel",
8386
"cmake",
8487
"ninja",
8588
"conan",
@@ -359,6 +362,7 @@ All the available tools:
359362
--cmake
360363
--ninja
361364
--vcpkg
365+
--bazel
362366
--meson
363367
--conan
364368
--make

src/nala/nala.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { dirname } from "path"
22
import which from "which"
33
import { isUbuntu } from "../utils/env/isUbuntu"
44
import { execSudo } from "../utils/exec/sudo"
5-
import { setupAptPack } from "../utils/setup/setupAptPack"
5+
import { addAptKeyViaDownload, setupAptPack } from "../utils/setup/setupAptPack"
66

77
let binDir: string | undefined
88

@@ -22,14 +22,13 @@ export async function setupNala(version: string, _setupDir: string, _arch: strin
2222
}
2323

2424
// https://github.com/volitank/nala#-installation
25-
await setupAptPack("wget")
25+
const keyFileName = await addAptKeyViaDownload(
26+
"volian-archive-scar-unstable.gpg",
27+
"https://deb.volian.org/volian/scar.key"
28+
)
2629
execSudo("/bin/bash", [
2730
"-c",
28-
`wget -qO - https://deb.volian.org/volian/scar.key | tee /etc/apt/trusted.gpg.d/volian-archive-scar-unstable.gpg > /dev/null`,
29-
])
30-
execSudo("/bin/bash", [
31-
"-c",
32-
`echo "deb http://deb.volian.org/volian/ scar main" | tee /etc/apt/sources.list.d/volian-archive-scar-unstable.list`,
31+
`echo "deb [signed-by=${keyFileName}] http://deb.volian.org/volian/ scar main" | tee /etc/apt/sources.list.d/volian-archive-scar-unstable.list`,
3332
])
3433

3534
try {

src/utils/setup/setupAptPack.ts

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import { InstallationInfo } from "./setupBin"
33
import { execSudo } from "../exec/sudo"
44
import { info } from "@actions/core"
5-
import { warning } from "../io/io"
65
import { isGitHubCI } from "../env/isCI"
76
import { addEnv, cpprc_path, setupCppInProfile } from "../env/addEnv"
87
import { appendFileSync, existsSync } from "fs"
@@ -18,10 +17,10 @@ export async function setupAptPack(
1817
repositories: string[] = [],
1918
update = false
2019
): Promise<InstallationInfo> {
21-
info(`Installing ${name} ${version ?? ""} via apt`)
22-
2320
const apt: string = getApt()
2421

22+
info(`Installing ${name} ${version ?? ""} via ${apt}`)
23+
2524
process.env.DEBIAN_FRONTEND = "noninteractive"
2625

2726
if (!didUpdate || update) {
@@ -80,33 +79,48 @@ async function initApt(apt: string) {
8079
"ca-certificates",
8180
"gnupg",
8281
])
83-
addAptKey(["3B4FE6ACC0B21F32", "40976EAF437D05B5"], "setup-cpp-ubuntu-archive.gpg")
84-
addAptKey(["1E9377A2BA9EF27F"], "setup-cpp-launchpad-toolchain.gpg")
82+
addAptKeyViaServer(["3B4FE6ACC0B21F32", "40976EAF437D05B5"], "setup-cpp-ubuntu-archive.gpg")
83+
addAptKeyViaServer(["1E9377A2BA9EF27F"], "launchpad-toolchain.gpg")
8584
if (apt === "nala") {
8685
// enable utf8 otherwise it fails because of the usage of ASCII encoding
8786
await addEnv("LANG", "C.UTF-8")
8887
await addEnv("LC_ALL", "C.UTF-8")
8988
}
9089
}
9190

92-
function addAptKey(keys: string[], name: string) {
93-
try {
94-
if (!existsSync(`/root/.gnupg/${name}`)) {
95-
for (const key of keys) {
96-
execSudo("gpg", [
97-
"--no-default-keyring",
98-
"--keyring",
99-
name,
100-
"--keyserver",
101-
"keyserver.ubuntu.com",
102-
"--recv-keys",
103-
key,
104-
])
105-
}
91+
function initGpg() {
92+
execSudo("gpg", ["-k"])
93+
}
94+
95+
export function addAptKeyViaServer(keys: string[], name: string, server = "keyserver.ubuntu.com") {
96+
const fileName = `/etc/apt/trusted.gpg.d/${name}`
97+
if (!existsSync(fileName)) {
98+
initGpg()
99+
for (const key of keys) {
100+
execSudo("gpg", [
101+
"--no-default-keyring",
102+
"--keyring",
103+
`gnupg-ring:${fileName}`,
104+
"--keyserver",
105+
server,
106+
"--recv-keys",
107+
key,
108+
])
109+
execSudo("chmod", ["644", fileName])
106110
}
107-
} catch (err) {
108-
warning(`Failed to add keys: ${err}`)
109111
}
112+
return fileName
113+
}
114+
115+
export async function addAptKeyViaDownload(name: string, url: string) {
116+
const fileName = `/etc/apt/trusted.gpg.d/${name}`
117+
if (!existsSync(fileName)) {
118+
initGpg()
119+
await setupAptPack("curl", undefined)
120+
execSudo("bash", ["-c", `curl -s ${url} | gpg --no-default-keyring --keyring gnupg-ring:${fileName} --import`])
121+
execSudo("chmod", ["644", fileName])
122+
}
123+
return fileName
110124
}
111125

112126
export function updateAptAlternatives(name: string, path: string) {

0 commit comments

Comments
 (0)