Skip to content

Commit

Permalink
Merge pull request #112 from aminya/bazel [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya authored Jul 28, 2022
2 parents c8cec57 + 41af5a7 commit 322f081
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Setting up a **cross-platform** environment for building and testing C++/C proje
| category | tools |
| --------------------- | ------------------------------------------------------------ |
| compiler and analyzer | llvm, gcc, msvc, vcvarsall, cppcheck, clangtidy, clangformat |
| build system | cmake, ninja, meson, make, task |
| build system | cmake, ninja, meson, make, task, bazel |
| package manager | vcpkg, conan, choco, brew, nala |
| cache | cppcache |
| documentation | doxygen, graphviz |
Expand Down
4 changes: 4 additions & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ words:
- aarch
- aminya
- applellvm
- bazel
- bazelisk
- copr
- vbatts
- buildtools
- caxa
- ccache
Expand Down
2 changes: 1 addition & 1 deletion dist/setup_cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/setup_cpp.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/setup_cpp.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/setup_cpp.mjs.map

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/bazel/__tests__/bazel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { setupBazel } from "../bazel"
import { testBin } from "../../utils/tests/test-helpers"
import { InstallationInfo } from "../../utils/setup/setupBin"

jest.setTimeout(300000)
describe("setup-bazel", () => {
it("should setup bazel", async () => {
const installInfo = await setupBazel("", "", process.arch)

await testBin("bazel", ["--version"], (installInfo as InstallationInfo | undefined)?.binDir)
})
})
47 changes: 47 additions & 0 deletions src/bazel/bazel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { addAptKeyViaDownload, setupAptPack } from "../utils/setup/setupAptPack"
import { setupBrewPack } from "../utils/setup/setupBrewPack"
import { setupChocoPack } from "../utils/setup/setupChocoPack"
import { isArch } from "../utils/env/isArch"
import { hasDnf } from "../utils/env/hasDnf"
import { setupDnfPack } from "../utils/setup/setupDnfPack"
import { isUbuntu } from "../utils/env/isUbuntu"
import { execSudo } from "../utils/exec/sudo"

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function setupBazel(version: string, _setupDir: string, _arch: string) {
switch (process.platform) {
case "win32": {
// install bazelisk because it contains both
return setupChocoPack("bazelisk", version)
}
case "darwin": {
// install bazelisk because it contains both
return setupBrewPack("bazelisk", version)
}
case "linux": {
if (isArch()) {
throw new Error("installing bazel on Arch linux is not supported yet")
} else if (hasDnf()) {
// https://bazel.build/install/redhat
setupDnfPack("dnf-plugins-core", undefined)
execSudo("dnf", ["copr", "enable", "vbatts/bazel"])
return setupDnfPack("bazel4", undefined)
} else if (isUbuntu()) {
// https://bazel.build/install/ubuntu
const keyFileName = await addAptKeyViaDownload(
"bazel-archive-keyring.gpg",
"https://bazel.build/bazel-release.pub.gpg"
)
execSudo("bash", [
"-c",
`echo "deb [arch=amd64 signed-by=${keyFileName}] https://storage.googleapis.com/bazel-apt stable jdk1.8" | tee /etc/apt/sources.list.d/bazel.list`,
])
return setupAptPack("bazel", version, [], true)
}
throw new Error(`Unsupported linux distribution`)
}
default: {
throw new Error(`Unsupported platform`)
}
}
}
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { addEnv } from "./utils/env/addEnv"
import { setupSevenZip } from "./sevenzip/sevenzip"
import { setupGraphviz } from "./graphviz/graphviz"
import { setupNala } from "./nala/nala"
import { setupBazel } from "./bazel/bazel"

/** The setup functions */
const setups = {
Expand All @@ -51,6 +52,7 @@ const setups = {
ninja: setupNinja,
python: setupPython,
vcpkg: setupVcpkg,
bazel: setupBazel,
conan: setupConan,
meson: setupMeson,
gcovr: setupGcovr,
Expand Down Expand Up @@ -80,6 +82,7 @@ const tools: Array<keyof typeof setups> = [
"brew",
"python",
"vcpkg",
"bazel",
"cmake",
"ninja",
"conan",
Expand Down Expand Up @@ -359,6 +362,7 @@ All the available tools:
--cmake
--ninja
--vcpkg
--bazel
--meson
--conan
--make
Expand Down
13 changes: 6 additions & 7 deletions src/nala/nala.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { dirname } from "path"
import which from "which"
import { isUbuntu } from "../utils/env/isUbuntu"
import { execSudo } from "../utils/exec/sudo"
import { setupAptPack } from "../utils/setup/setupAptPack"
import { addAptKeyViaDownload, setupAptPack } from "../utils/setup/setupAptPack"

let binDir: string | undefined

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

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

try {
Expand Down
56 changes: 35 additions & 21 deletions src/utils/setup/setupAptPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { InstallationInfo } from "./setupBin"
import { execSudo } from "../exec/sudo"
import { info } from "@actions/core"
import { warning } from "../io/io"
import { isGitHubCI } from "../env/isCI"
import { addEnv, cpprc_path, setupCppInProfile } from "../env/addEnv"
import { appendFileSync, existsSync } from "fs"
Expand All @@ -18,10 +17,10 @@ export async function setupAptPack(
repositories: string[] = [],
update = false
): Promise<InstallationInfo> {
info(`Installing ${name} ${version ?? ""} via apt`)

const apt: string = getApt()

info(`Installing ${name} ${version ?? ""} via ${apt}`)

process.env.DEBIAN_FRONTEND = "noninteractive"

if (!didUpdate || update) {
Expand Down Expand Up @@ -80,33 +79,48 @@ async function initApt(apt: string) {
"ca-certificates",
"gnupg",
])
addAptKey(["3B4FE6ACC0B21F32", "40976EAF437D05B5"], "setup-cpp-ubuntu-archive.gpg")
addAptKey(["1E9377A2BA9EF27F"], "setup-cpp-launchpad-toolchain.gpg")
addAptKeyViaServer(["3B4FE6ACC0B21F32", "40976EAF437D05B5"], "setup-cpp-ubuntu-archive.gpg")
addAptKeyViaServer(["1E9377A2BA9EF27F"], "launchpad-toolchain.gpg")
if (apt === "nala") {
// enable utf8 otherwise it fails because of the usage of ASCII encoding
await addEnv("LANG", "C.UTF-8")
await addEnv("LC_ALL", "C.UTF-8")
}
}

function addAptKey(keys: string[], name: string) {
try {
if (!existsSync(`/root/.gnupg/${name}`)) {
for (const key of keys) {
execSudo("gpg", [
"--no-default-keyring",
"--keyring",
name,
"--keyserver",
"keyserver.ubuntu.com",
"--recv-keys",
key,
])
}
function initGpg() {
execSudo("gpg", ["-k"])
}

export function addAptKeyViaServer(keys: string[], name: string, server = "keyserver.ubuntu.com") {
const fileName = `/etc/apt/trusted.gpg.d/${name}`
if (!existsSync(fileName)) {
initGpg()
for (const key of keys) {
execSudo("gpg", [
"--no-default-keyring",
"--keyring",
`gnupg-ring:${fileName}`,
"--keyserver",
server,
"--recv-keys",
key,
])
execSudo("chmod", ["644", fileName])
}
} catch (err) {
warning(`Failed to add keys: ${err}`)
}
return fileName
}

export async function addAptKeyViaDownload(name: string, url: string) {
const fileName = `/etc/apt/trusted.gpg.d/${name}`
if (!existsSync(fileName)) {
initGpg()
await setupAptPack("curl", undefined)
execSudo("bash", ["-c", `curl -s ${url} | gpg --no-default-keyring --keyring gnupg-ring:${fileName} --import`])
execSudo("chmod", ["644", fileName])
}
return fileName
}

export function updateAptAlternatives(name: string, path: string) {
Expand Down

0 comments on commit 322f081

Please sign in to comment.