#!/usr/bin/env bash # # build.sh -- Compile the sketch using arduino-cli # # Reads all settings from .anvil.toml. No Anvil binary required. # # Usage: # ./build.sh Compile (verify only) # ./build.sh --clean Delete build cache first # ./build.sh --verbose Show full compiler output # # Prerequisites: arduino-cli in PATH, arduino:avr core installed # Install: anvil setup (or manually: arduino-cli core install arduino:avr) set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" CONFIG="$SCRIPT_DIR/.anvil.toml" # -- Colors ---------------------------------------------------------------- if [[ -t 1 ]]; then RED=$'\033[0;31m'; GRN=$'\033[0;32m'; YLW=$'\033[0;33m' CYN=$'\033[0;36m'; BLD=$'\033[1m'; RST=$'\033[0m' else RED=''; GRN=''; YLW=''; CYN=''; BLD=''; RST='' fi ok() { echo "${GRN}ok${RST} $*"; } warn() { echo "${YLW}warn${RST} $*"; } 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 ' ' } SKETCH_NAME="$(toml_get 'name')" FQBN="$(toml_get 'fqbn')" 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" SKETCH_DIR="$SCRIPT_DIR/$SKETCH_NAME" BUILD_DIR="$SCRIPT_DIR/.build" # -- Parse arguments ------------------------------------------------------- DO_CLEAN=0 VERBOSE="" for arg in "$@"; do case "$arg" in --clean) DO_CLEAN=1 ;; --verbose) VERBOSE="--verbose" ;; -h|--help) echo "Usage: ./build.sh [--clean] [--verbose]" echo " Compiles the sketch. Settings from .anvil.toml." exit 0 ;; *) die "Unknown option: $arg" ;; esac done # -- Preflight ------------------------------------------------------------- command -v arduino-cli &>/dev/null \ || die "arduino-cli not found in PATH. Install it first." [[ -d "$SKETCH_DIR" ]] \ || die "Sketch directory not found: $SKETCH_DIR" [[ -f "$SKETCH_DIR/$SKETCH_NAME.ino" ]] \ || die "Sketch file not found: $SKETCH_DIR/$SKETCH_NAME.ino" # -- Clean ----------------------------------------------------------------- if [[ $DO_CLEAN -eq 1 ]] && [[ -d "$BUILD_DIR" ]]; then echo "${YLW}Cleaning build cache...${RST}" rm -rf "$BUILD_DIR" ok "Cache cleared." fi # -- Build include flags --------------------------------------------------- BUILD_FLAGS="" for dir in $INCLUDE_DIRS; do abs="$SCRIPT_DIR/$dir" if [[ -d "$abs" ]]; then BUILD_FLAGS="$BUILD_FLAGS -I$abs" else warn "Include directory not found: $dir" fi done for flag in $EXTRA_FLAGS; do BUILD_FLAGS="$BUILD_FLAGS $flag" done # -- Compile --------------------------------------------------------------- echo "${CYN}${BLD}Compiling ${SKETCH_NAME}...${RST}" echo " Board: $FQBN" echo " Sketch: $SKETCH_DIR" echo "" mkdir -p "$BUILD_DIR" COMPILE_ARGS=( compile --fqbn "$FQBN" --build-path "$BUILD_DIR" --warnings "$WARNINGS" ) if [[ -n "$BUILD_FLAGS" ]]; then COMPILE_ARGS+=(--build-property "build.extra_flags=$BUILD_FLAGS") fi if [[ -n "$VERBOSE" ]]; then COMPILE_ARGS+=("$VERBOSE") fi COMPILE_ARGS+=("$SKETCH_DIR") arduino-cli "${COMPILE_ARGS[@]}" || die "Compilation failed." echo "" ok "Compile succeeded." # -- Binary size ----------------------------------------------------------- ELF="$BUILD_DIR/$SKETCH_NAME.ino.elf" if [[ -f "$ELF" ]] && command -v avr-size &>/dev/null; then echo "" avr-size --mcu=atmega328p -C "$ELF" fi echo ""