Switched to build.ps1
Some checks failed
CI / Test (Linux) (push) Has been cancelled
CI / Test (Windows MSVC) (push) Has been cancelled
CI / Clippy (push) Has been cancelled
CI / Format (push) Has been cancelled

- Batch script had issues reading from cargo toml file
- Issues were revealed in cargo test (awesome!)
- build.bat is now a wrapper for build.ps1
This commit is contained in:
Eric Ratliff
2026-02-22 22:08:33 -06:00
parent 1ede07df81
commit 7e8d7ecce5
5 changed files with 283 additions and 199 deletions

View File

@@ -1,9 +1,11 @@
@echo off
setlocal enabledelayedexpansion
:: build.bat -- Compile the sketch using arduino-cli
:: build.bat -- Thin wrapper that invokes build.ps1
::
:: Reads all settings from .anvil.toml. No Anvil binary required.
:: Students can type "build" at a command prompt or double-click this file.
:: All logic lives in build.ps1. Requires PowerShell 5.1+ (ships with
:: Windows 10/11).
::
:: Settings are read from .anvil.toml. No Anvil binary required.
::
:: Usage:
:: build.bat Compile (verify only)
@@ -11,173 +13,5 @@ setlocal enabledelayedexpansion
:: build.bat --clean Delete build cache first
:: build.bat --verbose Show full compiler output
set "SCRIPT_DIR=%~dp0"
set "SCRIPT_DIR=%SCRIPT_DIR:~0,-1%"
set "CONFIG=%SCRIPT_DIR%\.anvil.toml"
if not exist "%CONFIG%" (
echo FAIL: No .anvil.toml found in %SCRIPT_DIR%
exit /b 1
)
:: -- Parse .anvil.toml (flat keys) ----------------------------------------
for /f "usebackq tokens=1,* delims==" %%a in ("%CONFIG%") do (
set "_K=%%a"
if not "!_K:~0,1!"=="#" if not "!_K:~0,1!"=="[" (
set "_K=!_K: =!"
set "_V=%%b"
if defined _V (
set "_V=!_V: =!"
set "_V=!_V:"=!"
)
if "!_K!"=="name" set "SKETCH_NAME=!_V!"
if "!_K!"=="default" set "DEFAULT_BOARD=!_V!"
if "!_K!"=="warnings" set "WARNINGS=!_V!"
)
)
if "%SKETCH_NAME%"=="" (
echo FAIL: Could not read project name from .anvil.toml
exit /b 1
)
set "SKETCH_DIR=%SCRIPT_DIR%\%SKETCH_NAME%"
set "BUILD_DIR=%SCRIPT_DIR%\.build"
:: -- Parse arguments ------------------------------------------------------
set "DO_CLEAN=0"
set "VERBOSE="
set "BOARD_NAME="
:parse_args
if "%~1"=="" goto done_args
if "%~1"=="--board" set "BOARD_NAME=%~2" & shift & shift & goto parse_args
if "%~1"=="--clean" set "DO_CLEAN=1" & shift & goto parse_args
if "%~1"=="--verbose" set "VERBOSE=--verbose" & shift & goto parse_args
if "%~1"=="--help" goto show_help
if "%~1"=="-h" goto show_help
echo FAIL: Unknown option: %~1
exit /b 1
:show_help
echo Usage: build.bat [--board NAME] [--clean] [--verbose]
echo Compiles the sketch. Settings from .anvil.toml.
echo --board NAME selects a board from [boards.NAME].
exit /b 0
:done_args
:: -- Resolve board --------------------------------------------------------
if "%BOARD_NAME%"=="" set "BOARD_NAME=%DEFAULT_BOARD%"
if "%BOARD_NAME%"=="" (
echo FAIL: No default board set in .anvil.toml.
echo.
echo Add a default to the [build] section of .anvil.toml:
echo default = "uno"
echo.
echo And make sure a matching [boards.uno] section exists:
echo [boards.uno]
echo fqbn = "arduino:avr:uno"
echo.
echo Or with Anvil: anvil board --default uno
echo List boards: anvil board --listall
echo arduino-cli board listall
exit /b 1
)
set "BOARD_SECTION=[boards.%BOARD_NAME%]"
set "IN_SECTION=0"
set "FQBN="
for /f "usebackq tokens=*" %%L in ("%CONFIG%") do (
set "_LINE=%%L"
if "!_LINE!"=="!BOARD_SECTION!" (
set "IN_SECTION=1"
) else if "!IN_SECTION!"=="1" (
if "!_LINE:~0,1!"=="[" (
set "IN_SECTION=0"
) else if not "!_LINE:~0,1!"=="#" (
for /f "tokens=1,* delims==" %%a in ("!_LINE!") do (
set "_BK=%%a"
set "_BK=!_BK: =!"
set "_BV=%%b"
if defined _BV (
set "_BV=!_BV: =!"
set "_BV=!_BV:"=!"
)
if "!_BK!"=="fqbn" set "FQBN=!_BV!"
)
)
)
)
if "!FQBN!"=="" (
echo FAIL: No [boards.%BOARD_NAME%] section in .anvil.toml.
echo.
echo Add it to .anvil.toml:
echo [boards.%BOARD_NAME%]
echo fqbn = "arduino:avr:uno" ^(replace with your board^)
echo.
echo Or with Anvil: anvil board --add %BOARD_NAME%
echo List boards: anvil board --listall
echo arduino-cli board listall
exit /b 1
)
if not "%BOARD_NAME%"=="%DEFAULT_BOARD%" (
echo ok Using board: %BOARD_NAME% -- %FQBN%
)
:: -- Preflight ------------------------------------------------------------
where arduino-cli >nul 2>nul
if errorlevel 1 (
echo FAIL: arduino-cli not found in PATH.
exit /b 1
)
if not exist "%SKETCH_DIR%" (
echo FAIL: Sketch directory not found: %SKETCH_DIR%
exit /b 1
)
:: -- Clean ----------------------------------------------------------------
if "%DO_CLEAN%"=="1" (
if exist "%BUILD_DIR%" (
echo Cleaning build cache...
rmdir /s /q "%BUILD_DIR%"
)
)
:: -- Build include flags --------------------------------------------------
set "BUILD_FLAGS="
for %%d in (lib\hal lib\app) do (
if exist "%SCRIPT_DIR%\%%d" (
set "BUILD_FLAGS=!BUILD_FLAGS! -I%SCRIPT_DIR%\%%d"
)
)
:: Auto-discover driver libraries (added by: anvil add <driver>)
if exist "%SCRIPT_DIR%\lib\drivers" (
for /d %%d in ("%SCRIPT_DIR%\lib\drivers\*") do (
set "BUILD_FLAGS=!BUILD_FLAGS! -I%%d"
)
)
set "BUILD_FLAGS=!BUILD_FLAGS! -Werror"
:: -- Compile --------------------------------------------------------------
echo Compiling %SKETCH_NAME%...
echo Board: %FQBN%
echo Sketch: %SKETCH_DIR%
echo.
if not exist "%BUILD_DIR%" mkdir "%BUILD_DIR%"
arduino-cli compile --fqbn %FQBN% --build-path "%BUILD_DIR%" --warnings %WARNINGS% --build-property "compiler.cpp.extra_flags=%BUILD_FLAGS%" --build-property "compiler.c.extra_flags=%BUILD_FLAGS%" %VERBOSE% "%SKETCH_DIR%"
if errorlevel 1 (
echo.
echo FAIL: Compilation failed.
exit /b 1
)
echo.
echo ok Compile succeeded.
echo.
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0build.ps1" %*
exit /b %ERRORLEVEL%

212
templates/basic/build.ps1 Normal file
View File

@@ -0,0 +1,212 @@
# build.ps1 -- Compile the sketch using arduino-cli
#
# Reads all settings from .anvil.toml. No Anvil binary required.
# Called by build.bat (thin wrapper) or directly:
# powershell -File build.ps1 [--board NAME] [--clean] [--verbose]
#
# Exit codes: 0 = success, 1 = error
param(
[string]$board = "",
[switch]$clean,
[switch]$verbose,
[switch]$help
)
$ErrorActionPreference = "Stop"
# -- Helpers ---------------------------------------------------------------
function Fail($msg) {
Write-Host "FAIL: $msg" -ForegroundColor Red
exit 1
}
function Ok($msg) {
Write-Host "ok $msg" -ForegroundColor Green
}
# -- Locate config ---------------------------------------------------------
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
$Config = Join-Path $ScriptDir ".anvil.toml"
if (-not (Test-Path $Config)) {
Fail "No .anvil.toml found in $ScriptDir"
}
# -- Parse .anvil.toml -----------------------------------------------------
# Simple line-by-line parser. Tracks current section header to support
# [project], [build], [boards.NAME], etc.
$tomlData = @{}
$currentSection = ""
foreach ($rawLine in Get-Content $Config) {
$line = $rawLine.Trim()
# Skip blank lines and comments
if ($line -eq "" -or $line.StartsWith("#")) { continue }
# Section header: [project], [boards.uno], etc.
if ($line -match '^\[(.+)\]$') {
$currentSection = $Matches[1]
continue
}
# Key = value (only process lines with =)
if ($line -match '^([^=]+?)\s*=\s*(.+)$') {
$key = $Matches[1].Trim()
$val = $Matches[2].Trim()
# Strip surrounding quotes
if ($val.StartsWith('"') -and $val.EndsWith('"')) {
$val = $val.Substring(1, $val.Length - 2)
}
# Skip array values (multi-line or inline [...])
if ($val.StartsWith("[")) { continue }
$fullKey = if ($currentSection) { "$currentSection.$key" } else { $key }
$tomlData[$fullKey] = $val
}
}
# -- Extract settings ------------------------------------------------------
$SketchName = $tomlData["project.name"]
$DefaultBoard = $tomlData["build.default"]
$Warnings = $tomlData["build.warnings"]
if (-not $SketchName) {
Fail "Could not read project name from .anvil.toml"
}
$SketchDir = Join-Path $ScriptDir $SketchName
$BuildDir = Join-Path $ScriptDir ".build"
# -- Help ------------------------------------------------------------------
if ($help) {
Write-Host "Usage: build.bat [--board NAME] [--clean] [--verbose]"
Write-Host " Compiles the sketch. Settings from .anvil.toml."
Write-Host " --board NAME selects a board from [boards.NAME]."
exit 0
}
# -- Resolve board ---------------------------------------------------------
$BoardName = if ($board) { $board } else { $DefaultBoard }
if (-not $BoardName) {
Fail @"
No default board set in .anvil.toml.
Add a default to the [build] section of .anvil.toml:
default = "uno"
And make sure a matching [boards.uno] section exists:
[boards.uno]
fqbn = "arduino:avr:uno"
Or with Anvil: Anvil board --default uno
List boards: Anvil board --listall
arduino-cli board listall
"@
}
$Fqbn = $tomlData["boards.$BoardName.fqbn"]
if (-not $Fqbn) {
Fail @"
No [boards.$BoardName] section in .anvil.toml.
Add it to .anvil.toml:
[boards.$BoardName]
fqbn = "arduino:avr:uno" (replace with your board)
Or with Anvil: Anvil board --add $BoardName
List boards: Anvil board --listall
arduino-cli board listall
"@
}
if ($BoardName -ne $DefaultBoard) {
Ok "Using board: $BoardName -- $Fqbn"
}
# -- Preflight -------------------------------------------------------------
$arduinoCli = Get-Command "arduino-cli" -ErrorAction SilentlyContinue
if (-not $arduinoCli) {
Fail "arduino-cli not found in PATH."
}
if (-not (Test-Path $SketchDir)) {
Fail "Sketch directory not found: $SketchDir"
}
# -- Clean -----------------------------------------------------------------
if ($clean -and (Test-Path $BuildDir)) {
Write-Host "Cleaning build cache..."
Remove-Item -Recurse -Force $BuildDir
}
# -- Build include flags ---------------------------------------------------
$buildFlags = @()
foreach ($sub in @("lib\hal", "lib\app")) {
$dir = Join-Path $ScriptDir $sub
if (Test-Path $dir) {
$buildFlags += "-I$dir"
}
}
# Auto-discover driver libraries (added by: anvil add <driver>)
$driversDir = Join-Path $ScriptDir "lib\drivers"
if (Test-Path $driversDir) {
foreach ($d in Get-ChildItem -Path $driversDir -Directory) {
$buildFlags += "-I$($d.FullName)"
}
}
$buildFlags += "-Werror"
$flagsStr = $buildFlags -join " "
# -- Compile ---------------------------------------------------------------
Write-Host "Compiling $SketchName..."
Write-Host " Board: $Fqbn"
Write-Host " Sketch: $SketchDir"
Write-Host ""
if (-not (Test-Path $BuildDir)) {
New-Item -ItemType Directory -Path $BuildDir | Out-Null
}
$compileArgs = @(
"compile"
"--fqbn", $Fqbn
"--build-path", $BuildDir
"--warnings", $Warnings
"--build-property", "compiler.cpp.extra_flags=$flagsStr"
"--build-property", "compiler.c.extra_flags=$flagsStr"
)
if ($verbose) {
$compileArgs += "--verbose"
}
$compileArgs += $SketchDir
& arduino-cli @compileArgs
if ($LASTEXITCODE -ne 0) {
Write-Host ""
Fail "Compilation failed."
}
Write-Host ""
Ok "Compile succeeded."
Write-Host ""
exit 0