Supporting multiple boards

This commit is contained in:
Eric Ratliff
2026-02-19 10:12:33 -06:00
parent 2739d83b99
commit b909da298e
16 changed files with 1554 additions and 94 deletions

View File

@@ -6,6 +6,7 @@
#
# Usage:
# ./build.sh Compile (verify only)
# ./build.sh --board mega Use a named board
# ./build.sh --clean Delete build cache first
# ./build.sh --verbose Show full compiler output
#
@@ -32,27 +33,34 @@ die() { echo "${RED}FAIL${RST} $*" >&2; exit 1; }
# -- Parse .anvil.toml -----------------------------------------------------
[[ -f "$CONFIG" ]] || die "No .anvil.toml found in $SCRIPT_DIR"
# Extract a simple string value: toml_get "key"
# Searches the whole file; for sectioned keys, grep is specific enough
# given our small, flat schema.
toml_get() {
(grep "^$1 " "$CONFIG" 2>/dev/null || true) | head -1 | sed 's/.*= *"\{0,1\}\([^"]*\)"\{0,1\}/\1/' | tr -d ' '
}
# Extract a TOML array as space-separated values: toml_array "key"
toml_array() {
(grep "^$1 " "$CONFIG" 2>/dev/null || true) | head -1 \
| sed 's/.*\[//; s/\].*//; s/"//g; s/,/ /g' | tr -s ' '
}
toml_section_get() {
local section="$1" key="$2"
awk -v section="[$section]" -v key="$key" '
$0 == section { found=1; next }
/^\[/ { found=0 }
found && $1 == key && /=/ {
sub(/^[^=]*= *"?/, ""); sub(/"? *$/, ""); print; exit
}
' "$CONFIG"
}
SKETCH_NAME="$(toml_get 'name')"
FQBN="$(toml_get 'fqbn')"
DEFAULT_BOARD="$(toml_get 'default')"
WARNINGS="$(toml_get 'warnings')"
INCLUDE_DIRS="$(toml_array 'include_dirs')"
EXTRA_FLAGS="$(toml_array 'extra_flags')"
[[ -n "$SKETCH_NAME" ]] || die "Could not read project name from .anvil.toml"
[[ -n "$FQBN" ]] || die "Could not read fqbn from .anvil.toml"
[[ -n "$SKETCH_NAME" ]] || die "Could not read project name from .anvil.toml"
[[ -n "$DEFAULT_BOARD" ]] || die "Could not read default board from .anvil.toml"
SKETCH_DIR="$SCRIPT_DIR/$SKETCH_NAME"
BUILD_DIR="$SCRIPT_DIR/.build"
@@ -60,20 +68,35 @@ BUILD_DIR="$SCRIPT_DIR/.build"
# -- Parse arguments -------------------------------------------------------
DO_CLEAN=0
VERBOSE=""
BOARD_NAME=""
for arg in "$@"; do
case "$arg" in
--clean) DO_CLEAN=1 ;;
--verbose) VERBOSE="--verbose" ;;
while [[ $# -gt 0 ]]; do
case "$1" in
--board) BOARD_NAME="$2"; shift 2 ;;
--clean) DO_CLEAN=1; shift ;;
--verbose) VERBOSE="--verbose"; shift ;;
-h|--help)
echo "Usage: ./build.sh [--clean] [--verbose]"
echo "Usage: ./build.sh [--board NAME] [--clean] [--verbose]"
echo " Compiles the sketch. Settings from .anvil.toml."
echo " --board NAME selects a board from [boards.NAME]."
exit 0
;;
*) die "Unknown option: $arg" ;;
*) die "Unknown option: $1" ;;
esac
done
# -- Resolve board ---------------------------------------------------------
ACTIVE_BOARD="${BOARD_NAME:-$DEFAULT_BOARD}"
FQBN="$(toml_section_get "boards.$ACTIVE_BOARD" "fqbn")"
if [[ -z "$FQBN" ]]; then
die "No board '$ACTIVE_BOARD' in .anvil.toml.\n Add it: anvil board --add $ACTIVE_BOARD"
fi
if [[ -n "$BOARD_NAME" ]]; then
ok "Using board: $BOARD_NAME ($FQBN)"
fi
# -- Preflight -------------------------------------------------------------
command -v arduino-cli &>/dev/null \
|| die "arduino-cli not found in PATH. Install it first."
@@ -121,7 +144,8 @@ COMPILE_ARGS=(
)
if [[ -n "$BUILD_FLAGS" ]]; then
COMPILE_ARGS+=(--build-property "build.extra_flags=$BUILD_FLAGS")
COMPILE_ARGS+=(--build-property "compiler.cpp.extra_flags=$BUILD_FLAGS")
COMPILE_ARGS+=(--build-property "compiler.c.extra_flags=$BUILD_FLAGS")
fi
if [[ -n "$VERBOSE" ]]; then
@@ -142,4 +166,4 @@ if [[ -f "$ELF" ]] && command -v avr-size &>/dev/null; then
avr-size --mcu=atmega328p -C "$ELF"
fi
echo ""
echo ""