From 72fc8dcca7c69b73fb92dd4b03ef2a5583675a48 Mon Sep 17 00:00:00 2001 From: Vincent Taverna Date: Mon, 16 Nov 2026 15:34:20 -0500 Subject: [PATCH] build: cross-compile a static riscv64 musl binary in ci scripts/riscv64.sh builds riscv64gc-unknown-linux-musl with zig and cargo-zigbuild, refuses a dynamically linked result and smoke-tests it under qemu-user. GitHub keeps the binary as an artifact; Tangled runs the same script. --- .github/workflows/actions.lock | 6 ++++ .github/workflows/ci.yml | 50 +++++++++++++++++++++++++++++++++ .gitignore | 1 + .tangled/workflows/riscv64.yaml | 27 ++++++++++++++++++ scripts/riscv64.sh | 34 ++++++++++++++++++++++ 5 files changed, 118 insertions(+) create mode 100644 .tangled/workflows/riscv64.yaml create mode 100755 scripts/riscv64.sh diff --git a/.github/workflows/actions.lock b/.github/workflows/actions.lock index a061ef8..611efb7 100644 --- a/.github/workflows/actions.lock +++ b/.github/workflows/actions.lock @@ -9,6 +9,7 @@ workflows: - 'swatinem/rust-cache@v2.9.3' '.github/workflows/ci.yml': - 'actions/checkout@v7.0.1' + - 'actions/upload-artifact@v7.0.2' - 'swatinem/rust-cache@v2.9.3' '.github/workflows/zizmor.yml': - 'actions/checkout@v7.0.1' @@ -19,6 +20,11 @@ dependencies: commit: 'sha1-3d3c42e5aac5ba805825da76410c181273ba90b1' owner_id: 44036562 repo_id: 197814629 + 'actions/upload-artifact@v7.0.2': + ref: 'v7.0.2' + commit: 'sha1-cf430e030ddbb5b0abf93d22962f4752f3646cd9' + owner_id: 44036562 + repo_id: 192625955 'autofix-ci/action@v1.3.4': ref: 'v1.3.4' commit: 'sha1-c5b2d67aa2274e7b5a18224e8171550871fc7e4a' diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e235b8b..b8f52a8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -112,3 +112,53 @@ jobs: env: # A snapshot that differs is a failure here, never a new file. INSTA_UPDATE: "no" + + riscv64: + name: 🧬 RISC-V (static riscv64 musl) + runs-on: ubuntu-24.04 + timeout-minutes: 40 + + steps: + - uses: actions/checkout@v7.0.1 + with: + persist-credentials: false + + - name: 🦀 Install the toolchain + run: | + rustup toolchain install + rustup target add riscv64gc-unknown-linux-musl + + - uses: Swatinem/rust-cache@v2.9.3 + with: + key: riscv64 + save-if: ${{ github.ref == 'refs/heads/main' }} + + # Zig is the C cross compiler for ring and aws-lc; checked by hash. + - name: ⚡ Install Zig and cargo-zigbuild + env: + ZIG_VERSION: 0.17.1 + ZIG_SHA256: 3581863e3cbcfd0fce0efc682ce7e9e93da96668a977791d356a26d2a7e9b17a + run: | + curl -fsSLo zig.tar.xz "https://ziglang.org/download/${ZIG_VERSION}/zig-x86_64-linux-${ZIG_VERSION}.tar.xz" + echo "${ZIG_SHA256} zig.tar.xz" | sha256sum -c - + mkdir -p "$HOME/zig" + tar -xJf zig.tar.xz -C "$HOME/zig" --strip-components=1 + rm zig.tar.xz + echo "$HOME/zig" >> "$GITHUB_PATH" + cargo install --locked cargo-zigbuild@0.24.1 + + - name: 🧪 Install qemu-user + run: | + sudo apt-get update + sudo apt-get install -y --no-install-recommends qemu-user-static + + - name: 🧬 Build and smoke-test + run: scripts/riscv64.sh + env: + QEMU: qemu-riscv64-static + + - uses: actions/upload-artifact@v7.0.2 + with: + name: libbui-riscv64gc-unknown-linux-musl + path: target/riscv64gc-unknown-linux-musl/release/libbui + if-no-files-found: error diff --git a/.gitignore b/.gitignore index 891e6bd..059c29c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.log .DS_Store *.snap.new +/target-* diff --git a/.tangled/workflows/riscv64.yaml b/.tangled/workflows/riscv64.yaml new file mode 100644 index 0000000..a42a04a --- /dev/null +++ b/.tangled/workflows/riscv64.yaml @@ -0,0 +1,27 @@ +# Builds the static riscv64 libbui (riscv64gc-unknown-linux-musl) on every +# push/PR to main and runs it under qemu-user. The same binary powers the +# in-browser machine on libbui.vinnymac.dev. +when: + - event: ["push", "pull_request"] + branch: ["main"] + +engine: microvm + +dependencies: + - rustup + - zig + - cargo-zigbuild + - qemu + - binutils + - gcc + +environment: + CARGO_TERM_COLOR: always + +steps: + - name: toolchain + command: | + rustup toolchain install + rustup target add riscv64gc-unknown-linux-musl + - name: build and smoke-test + command: QEMU=qemu-riscv64 scripts/riscv64.sh diff --git a/scripts/riscv64.sh b/scripts/riscv64.sh new file mode 100755 index 0000000..0a2a8a4 --- /dev/null +++ b/scripts/riscv64.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env sh +# Builds the static riscv64 libbui (riscv64gc-unknown-linux-musl) and, when +# qemu-riscv64 is available, runs it once to prove it starts. CI runs this; +# so can you. Needs rustup, zig and cargo-zigbuild. +# +# scripts/riscv64.sh # build and smoke-test +# QEMU=qemu-riscv64-static scripts/riscv64.sh +set -eu + +target=riscv64gc-unknown-linux-musl +cd "$(dirname "$0")/.." + +rustup target add "$target" >/dev/null +cargo zigbuild --locked --release -p libbui --target "$target" +binary="${CARGO_TARGET_DIR:-target}/$target/release/libbui" + +# A static binary has no interpreter: it must run on any riscv64 Linux, +# Alpine and the browser's machine included. +if readelf -l "$binary" | grep -q 'Requesting program interpreter'; then + echo "error: $binary is dynamically linked" >&2 + exit 1 +fi + +qemu="${QEMU:-qemu-riscv64}" +if command -v "$qemu" >/dev/null 2>&1; then + "$qemu" "$binary" --version + "$qemu" "$binary" --help >/dev/null + HOME="$(mktemp -d)" "$qemu" "$binary" paths --json >/dev/null + echo "smoke test passed under $qemu" +else + echo "warning: $qemu not found; built but not run" >&2 +fi + +sha256sum "$binary" -- knotmirror 0.0.1