|
| 1 | +# shellcheck shell=bash |
| 2 | +# |
| 3 | +# Shared bootstrap for RMK CI scripts. Source this from other scripts in |
| 4 | +# .github/ci/ to pick up common env, cache, helpers, and example discovery. |
| 5 | +# |
| 6 | +# source "$(dirname "${BASH_SOURCE[0]}")/_lib.sh" |
| 7 | +# |
| 8 | +# Expected preamble in the caller: |
| 9 | +# |
| 10 | +# #!/bin/bash |
| 11 | +# set -euo pipefail |
| 12 | +# |
| 13 | + |
| 14 | +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" |
| 15 | +cd "$repo_root" |
| 16 | + |
| 17 | +export CARGO_TERM_COLOR=always |
| 18 | +export CARGO_TERM_PROGRESS_WHEN=never |
| 19 | +export CARGO_NET_GIT_FETCH_WITH_CLI=true |
| 20 | +export TERM="${TERM:-dumb}" |
| 21 | + |
| 22 | +# Cache root: if RMK_CI_CACHE_ROOT is set (self-hosted runner or local reuse), |
| 23 | +# redirect rustup/cargo homes and target dir there. Otherwise fall back to |
| 24 | +# an in-tree target/ci dir so local runs don't clobber the default target. |
| 25 | +if [[ -n "${RMK_CI_CACHE_ROOT:-}" ]]; then |
| 26 | + export RUSTUP_HOME="${RUSTUP_HOME:-$RMK_CI_CACHE_ROOT/rustup}" |
| 27 | + export CARGO_HOME="${CARGO_HOME:-$RMK_CI_CACHE_ROOT/cargo}" |
| 28 | + export PATH="$CARGO_HOME/bin:$PATH" |
| 29 | + mkdir -p "$RMK_CI_CACHE_ROOT" |
| 30 | + if [[ -f "$HOME/.cargo/config.toml" && ! -f "$CARGO_HOME/config.toml" ]]; then |
| 31 | + mkdir -p "$CARGO_HOME" |
| 32 | + cp "$HOME/.cargo/config.toml" "$CARGO_HOME/config.toml" |
| 33 | + fi |
| 34 | + target_root="$RMK_CI_CACHE_ROOT/target" |
| 35 | +else |
| 36 | + target_root="$repo_root/target/ci" |
| 37 | +fi |
| 38 | +mkdir -p "$target_root" |
| 39 | + |
| 40 | +# Embedded Rust targets every stable toolchain build needs. |
| 41 | +TARGETS=( |
| 42 | + thumbv6m-none-eabi |
| 43 | + thumbv7m-none-eabi |
| 44 | + thumbv7em-none-eabi |
| 45 | + thumbv7em-none-eabihf |
| 46 | + thumbv8m.main-none-eabihf |
| 47 | + riscv32imc-unknown-none-elf |
| 48 | + riscv32imac-unknown-none-elf |
| 49 | +) |
| 50 | + |
| 51 | +log_section() { |
| 52 | + printf "\n==> %s\n" "$1" |
| 53 | +} |
| 54 | + |
| 55 | +ensure_toolchain() { |
| 56 | + rustup toolchain install "$1" --profile minimal --no-self-update |
| 57 | +} |
| 58 | + |
| 59 | +ensure_stable_toolchain() { |
| 60 | + ensure_toolchain stable |
| 61 | + rustup component add clippy rustfmt llvm-tools --toolchain stable |
| 62 | + rustup target add --toolchain stable "${TARGETS[@]}" |
| 63 | +} |
| 64 | + |
| 65 | +# $1=bin $2=crate-name |
| 66 | +ensure_cargo_tool() { |
| 67 | + if command -v "$1" >/dev/null 2>&1; then |
| 68 | + return |
| 69 | + fi |
| 70 | + cargo +stable install --locked "$2" |
| 71 | +} |
| 72 | + |
| 73 | +require_cargo_batch() { |
| 74 | + if command -v cargo-batch >/dev/null 2>&1; then |
| 75 | + return |
| 76 | + fi |
| 77 | + cargo install --git https://github.com/embassy-rs/cargo-batch cargo --bin cargo-batch --locked |
| 78 | +} |
| 79 | + |
| 80 | +ensure_esp_toolchain() { |
| 81 | + ensure_cargo_tool espup espup |
| 82 | + espup install |
| 83 | + if [[ ! -f "$HOME/export-esp.sh" ]]; then |
| 84 | + echo "espup did not create $HOME/export-esp.sh" >&2 |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | +} |
| 88 | + |
| 89 | +# Examples auto-discovery currently skips over: |
| 90 | +# - nrf54lm20_ble: Cargo.toml references local path deps |
| 91 | +# (/Users/.../nrf-sdc, /Users/.../nrf-mpsl) that only exist on the author's |
| 92 | +# workstation. The git-rev alternatives sit commented-out next to them; |
| 93 | +# once those are swapped in, this entry can be removed. |
| 94 | +# - esp32_ble_split: dual-target split example that only builds through the |
| 95 | +# `build-central` / `build-peripheral` cargo aliases (different chip per |
| 96 | +# bin, no [build].target set). Not buildable via a plain `cargo build`. |
| 97 | +EXAMPLE_SKIPLIST=( |
| 98 | + "examples/use_rust/nrf54lm20_ble" |
| 99 | + "examples/use_config/esp32_ble_split" |
| 100 | +) |
| 101 | + |
| 102 | +# Echoes Cargo.toml paths for every buildable example, one per line. |
| 103 | +# A buildable example is a direct child of examples/use_{rust,config}/ that |
| 104 | +# has both a src/ dir and a Cargo.toml (filters out placeholders like fix/), |
| 105 | +# and is not listed in EXAMPLE_SKIPLIST. |
| 106 | +list_example_manifests() { |
| 107 | + local dir stripped skip entry |
| 108 | + for dir in examples/use_rust/*/ examples/use_config/*/; do |
| 109 | + [[ -d "$dir/src" && -f "$dir/Cargo.toml" ]] || continue |
| 110 | + stripped="${dir%/}" |
| 111 | + skip=0 |
| 112 | + for entry in "${EXAMPLE_SKIPLIST[@]}"; do |
| 113 | + if [[ "$stripped" == "$entry" ]]; then |
| 114 | + skip=1 |
| 115 | + break |
| 116 | + fi |
| 117 | + done |
| 118 | + (( skip == 0 )) && printf '%s\n' "${dir}Cargo.toml" |
| 119 | + done |
| 120 | +} |
| 121 | + |
| 122 | +# Echoes the default build target triple declared in the manifest's sibling |
| 123 | +# .cargo/config.toml ([build].target). Only the first uncommented occurrence |
| 124 | +# is emitted; returns empty if the file or the key is absent. Trailing |
| 125 | +# TOML comments on the value are stripped. |
| 126 | +get_example_target() { |
| 127 | + local manifest="$1" |
| 128 | + local dir config |
| 129 | + dir="$(dirname "$manifest")" |
| 130 | + config="$dir/.cargo/config.toml" |
| 131 | + [[ -f "$config" ]] || return 0 |
| 132 | + awk ' |
| 133 | + /^\[/ { section = $0; next } |
| 134 | + section == "[build]" && /^[[:space:]]*target[[:space:]]*=/ { |
| 135 | + sub(/^[[:space:]]*target[[:space:]]*=[[:space:]]*/, "") |
| 136 | + sub(/[[:space:]]*#.*$/, "") |
| 137 | + sub(/^"/, "") |
| 138 | + sub(/"[[:space:]]*$/, "") |
| 139 | + print |
| 140 | + exit |
| 141 | + } |
| 142 | + ' "$config" |
| 143 | +} |
0 commit comments