Supporting configuration settings for include and warnings

This commit is contained in:
Eric Ratliff
2026-02-15 08:42:32 -06:00
parent 991b9a8ee9
commit 7b8ee2d186
2 changed files with 135 additions and 5 deletions

View File

@@ -14,7 +14,7 @@ FQBN="${ARDUINO_FQBN:-arduino:avr:uno}"
BAUD="${ARDUINO_BAUD:-115200}"
BUILD_DIR="${ARDUINO_BUILD_DIR:-/tmp/arduino-build}"
ARDUINO_CLI="${ARDUINO_CLI_BIN:-arduino-cli}"
VERSION="1.1.0"
VERSION="1.2.0"
# -- Color output (disabled if stdout is not a terminal) ---------------------
if [[ -t 1 ]]; then
@@ -58,6 +58,7 @@ $(_help_section_commands)
$(_help_section_build_options)
$(_help_section_config_options)
$(_help_section_env_vars)
$(_help_section_project_config)
$(_help_section_examples)
$(_help_section_cheatsheet)
$(_help_section_sketch_layout)
@@ -193,6 +194,42 @@ ${BLD}ENVIRONMENT VARIABLES${RST}
EOF
}
_help_section_project_config() {
cat <<EOF
${BLD}PROJECT CONFIGURATION (.arduino-build.conf)${RST}
Per-project build settings. Place this file at the project root (the
directory containing .git or the top-level directory above your sketch).
The script walks up from the sketch directory to find it, similar to
how git finds .git/.
${BLD}Supported variables:${RST}
INCLUDE_DIRS="lib/hal lib/app"
Space-separated list of directories to add to the compiler include
path. Paths are relative to the .arduino-build.conf file location.
Each directory becomes a -I flag.
EXTRA_BUILD_FLAGS="-DDEBUG=1 -DVERSION=2"
Additional compiler flags appended after include dirs. Use for
preprocessor defines, optimization overrides, etc.
${BLD}Example .arduino-build.conf:${RST}
# Project-level build configuration
INCLUDE_DIRS="lib/hal lib/app lib/drivers"
EXTRA_BUILD_FLAGS="-DBOARD_REV=3"
${BLD}How it works:${RST}
The flags are passed via arduino-cli's --build-property mechanism:
--build-property "build.extra_flags=-Ilib/hal -Ilib/app ..."
This is per-project, not global. Different projects can have
different include paths and flags. The script itself stays generic.
EOF
}
_help_section_examples() {
cat <<EOF
@@ -664,6 +701,83 @@ auto_detect_port() {
echo "${candidates[0]}"
}
# ============================================================================
# PROJECT CONFIG DISCOVERY
# ============================================================================
# Walk up from a directory to find .arduino-build.conf at the project root.
# Echoes the project root path. Returns 1 if not found.
find_project_root() {
local dir="$1"
local max_depth=10
local depth=0
while [[ $depth -lt $max_depth ]] && [[ "$dir" != "/" ]]; do
if [[ -f "$dir/.arduino-build.conf" ]]; then
echo "$dir"
return 0
fi
dir="$(dirname "$dir")"
depth=$((depth + 1))
done
return 1
}
# Load project config and build extra compiler flags.
# Sets PROJECT_ROOT and EXTRA_COMPILE_FLAGS as side effects.
PROJECT_ROOT=""
EXTRA_COMPILE_FLAGS=""
load_project_config() {
local sketch_dir="$1"
local conf_file=""
PROJECT_ROOT="$(find_project_root "$sketch_dir")" || {
info "No .arduino-build.conf found (using default settings)."
return 0
}
conf_file="$PROJECT_ROOT/.arduino-build.conf"
info "Project config: ${DIM}${conf_file}${RST}"
# Parse the conf file (only accept known variables, ignore everything else)
local include_dirs="" extra_flags=""
while IFS='=' read -r key value; do
# Strip leading/trailing whitespace
key="$(echo "$key" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"
value="$(echo "$value" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//;s/^"//;s/"$//')"
# Skip blank lines and comments
[[ -z "$key" || "$key" == \#* ]] && continue
case "$key" in
INCLUDE_DIRS) include_dirs="$value" ;;
EXTRA_BUILD_FLAGS) extra_flags="$value" ;;
esac
done < "$conf_file"
# Build -I flags from INCLUDE_DIRS (paths relative to project root)
local flags=""
for dir in $include_dirs; do
local abs_dir="$PROJECT_ROOT/$dir"
if [[ -d "$abs_dir" ]]; then
flags="$flags -I$abs_dir"
info " Include: ${DIM}$dir${RST}"
else
warn " Include dir not found: $dir (skipped)"
fi
done
# Append any extra flags
if [[ -n "$extra_flags" ]]; then
flags="$flags $extra_flags"
info " Extra flags: ${DIM}$extra_flags${RST}"
fi
# Trim leading space
EXTRA_COMPILE_FLAGS="$(echo "$flags" | sed 's/^[[:space:]]*//')"
}
# ============================================================================
# PERSISTENT SERIAL MONITOR (--watch)
# ============================================================================
@@ -885,9 +999,6 @@ if [[ $DO_CLEAN -eq 1 ]] && [[ -d "$SKETCH_BUILD_DIR" ]]; then
ok "Cache cleared."
fi
# ============================================================================
# COMPILE
# ============================================================================
# ============================================================================
# COMPILE (skipped with --upload-only)
# ============================================================================
@@ -916,16 +1027,24 @@ if [[ $UPLOAD_ONLY -eq 1 ]]; then
echo ""
fi
else
# Load per-project configuration (include paths, extra flags)
load_project_config "$SKETCH_DIR"
info "Compiling..."
mkdir -p "$SKETCH_BUILD_DIR"
COMPILE_ARGS=(
--fqbn "$FQBN"
--build-path "$SKETCH_BUILD_DIR"
--warnings all
--warnings more
)
[[ $DO_VERBOSE -eq 1 ]] && COMPILE_ARGS+=(--verbose)
# Inject per-project build flags (include paths, defines, etc.)
if [[ -n "$EXTRA_COMPILE_FLAGS" ]]; then
COMPILE_ARGS+=(--build-property "build.extra_flags=$EXTRA_COMPILE_FLAGS")
fi
if ! $ARDUINO_CLI compile "${COMPILE_ARGS[@]}" "$SKETCH_DIR"; then
die "Compilation failed."
fi