Add Bazel musl V8 build workflow

This commit is contained in:
pakrym-oai
2026-03-13 20:23:20 +00:00
committed by Channing Conger
parent 014e19510d
commit 7ec408fa32
7 changed files with 558 additions and 0 deletions
+125
View File
@@ -0,0 +1,125 @@
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import gzip
import re
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
MODULE_BAZEL = ROOT / "MODULE.bazel"
def parse_v8_crate_version() -> str:
text = MODULE_BAZEL.read_text()
match = re.search(
r"https://static\.crates\.io/crates/v8/v8-([0-9.]+)\.crate",
text,
)
if match is None:
raise SystemExit("could not determine v8 crate version from MODULE.bazel")
return match.group(1)
def bazel_execroot() -> Path:
result = subprocess.run(
["bazel", "info", "execution_root"],
cwd=ROOT,
check=True,
capture_output=True,
text=True,
)
return Path(result.stdout.strip())
def bazel_output_files(platform: str, labels: list[str]) -> list[Path]:
expression = "set(" + " ".join(labels) + ")"
result = subprocess.run(
[
"bazel",
"cquery",
f"--platforms=@llvm//platforms:{platform}",
"--output=files",
expression,
],
cwd=ROOT,
check=True,
capture_output=True,
text=True,
)
execroot = bazel_execroot()
return [execroot / line.strip() for line in result.stdout.splitlines() if line.strip()]
def stage_release_assets(platform: str, target: str, output_dir: Path) -> None:
target_suffix = target.replace("-", "_")
version_suffix = parse_v8_crate_version().replace(".", "_")
lib_label = f"//third_party/v8:v8_{version_suffix}_{target_suffix}"
binding_label = f"//third_party/v8:src_binding_release_{target_suffix}"
outputs = bazel_output_files(platform, [lib_label, binding_label])
try:
lib_path = next(path for path in outputs if path.suffix == ".a")
except StopIteration as exc:
raise SystemExit(f"missing static archive output for {target}") from exc
try:
binding_path = next(path for path in outputs if path.suffix == ".rs")
except StopIteration as exc:
raise SystemExit(f"missing binding output for {target}") from exc
output_dir.mkdir(parents=True, exist_ok=True)
archive_name = f"librusty_v8_release_{target}.a.gz"
binding_name = f"src_binding_release_{target}.rs"
with lib_path.open("rb") as src, (output_dir / archive_name).open("wb") as dst:
with gzip.GzipFile(
filename="",
mode="wb",
fileobj=dst,
compresslevel=9,
mtime=0,
) as gz:
shutil.copyfileobj(src, gz)
shutil.copyfile(binding_path, output_dir / binding_name)
print(output_dir / archive_name)
print(output_dir / binding_name)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="command", required=True)
subparsers.add_parser("print-version")
stage_parser = subparsers.add_parser("stage")
stage_parser.add_argument("--platform", required=True)
stage_parser.add_argument("--target", required=True)
stage_parser.add_argument("--output-dir", required=True)
return parser.parse_args()
def main() -> int:
args = parse_args()
if args.command == "print-version":
print(parse_v8_crate_version())
return 0
if args.command == "stage":
stage_release_assets(
platform=args.platform,
target=args.target,
output_dir=Path(args.output_dir),
)
return 0
raise SystemExit(f"unsupported command: {args.command}")
if __name__ == "__main__":
sys.exit(main())
@@ -0,0 +1,98 @@
name: rusty-v8-bazel-build
on:
pull_request: {}
push:
branches:
- main
workflow_dispatch:
concurrency:
group: ${{ github.workflow }}::${{ github.ref_name }}
cancel-in-progress: ${{ github.ref_name != 'main' }}
jobs:
build:
name: Build ${{ matrix.target }}
runs-on: ${{ matrix.runner }}
permissions:
contents: read
actions: read
strategy:
fail-fast: false
matrix:
include:
- runner: ubuntu-24.04
platform: linux_amd64_musl
target: x86_64-unknown-linux-musl
- runner: ubuntu-24.04-arm
platform: linux_aarch64_musl
target: aarch64-unknown-linux-musl
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Set up Bazel
uses: bazelbuild/setup-bazelisk@v3
- name: Build Bazel V8 musl artifacts
env:
BUILDBUDDY_API_KEY: ${{ secrets.BUILDBUDDY_API_KEY }}
PLATFORM: ${{ matrix.platform }}
TARGET: ${{ matrix.target }}
shell: bash
run: |
set -euo pipefail
version="$(python3 .github/scripts/rusty_v8_bazel.py print-version)"
version_suffix="${version//./_}"
target_suffix="${TARGET//-/_}"
lib_target="//third_party/v8:v8_${version_suffix}_${target_suffix}"
binding_target="//third_party/v8:src_binding_release_${target_suffix}"
bazel_args=(
build
"--platforms=@llvm//platforms:${PLATFORM}"
"${lib_target}"
"${binding_target}"
--build_metadata=REPO_URL=https://github.com/${GITHUB_REPOSITORY}.git
--build_metadata=COMMIT_SHA=$(git rev-parse HEAD)
--build_metadata=ROLE=CI
--build_metadata=VISIBILITY=PUBLIC
)
if [[ -n "${BUILDBUDDY_API_KEY:-}" ]]; then
bazel \
--noexperimental_remote_repo_contents_cache \
--bazelrc=.github/workflows/ci.bazelrc \
"${bazel_args[@]}" \
"--remote_header=x-buildbuddy-api-key=${BUILDBUDDY_API_KEY}"
else
bazel \
--noexperimental_remote_repo_contents_cache \
"${bazel_args[@]}" \
--remote_cache= \
--remote_executor=
fi
- name: Stage rusty_v8 release assets
env:
PLATFORM: ${{ matrix.platform }}
TARGET: ${{ matrix.target }}
shell: bash
run: |
set -euo pipefail
output_dir="dist/${TARGET}"
python3 .github/scripts/rusty_v8_bazel.py stage \
--platform "${PLATFORM}" \
--target "${TARGET}" \
--output-dir "${output_dir}"
- name: Upload staged artifacts
uses: actions/upload-artifact@v7
with:
name: rusty-v8-${{ matrix.target }}
path: dist/${{ matrix.target }}/*