1.8 KiB · text History dee327e
#!/usr/bin/env bash
# Print the ESP-IDF CMake build directory matching the ELF cargo just linked.
#
# Two things make this more than a glob.
#
# **Cargo has two layouts.** Older cargo puts a build script's output at
# `build/<pkg>-<hash>/out`; newer cargo nests it as `build/<pkg>/<hash>/out`.
# A tree that has been built by both — this one has — contains both, and CI
# on a current nightly produces only the flat one. Match either.
#
# **Stale directories outlive the build that made them.** Cargo does not clean
# up when a hash changes, so there can be several and only one matches the ELF
# beside it. Taking the first alphabetically, which is what a glob does, is how
# you flash a bootloader built from a different sdkconfig — which fails as a
# boot loop rather than as an error. So: newest `bootloader/bootloader.bin`
# wins, and a directory without one is not a candidate at all, since the
# bootloader is the only reason anything asks for this path.
#
# Prints nothing and exits 1 if there is no usable directory, naming what it
# did find on stderr.
set -euo pipefail

profile=${1:-release}
root="target/riscv32imac-esp-espidf/${profile}/build"

newest=""
while IFS= read -r dir; do
    bootloader="$dir/bootloader/bootloader.bin"
    [ -f "$bootloader" ] || continue
    if [ -z "$newest" ] || [ "$bootloader" -nt "$newest/bootloader/bootloader.bin" ]; then
        newest=$dir
    fi
done < <(find "$root" -maxdepth 4 -type d -path "*esp-idf-sys*/out/build" 2>/dev/null)

if [ -z "$newest" ]; then
    echo "idf-build-dir: no ESP-IDF build directory with a bootloader under $root" >&2
    echo "idf-build-dir: candidates seen:" >&2
    find "$root" -maxdepth 4 -type d -name "out" -path "*esp-idf-sys*" 2>/dev/null >&2 ||
        echo "  (none — has this profile been built?)" >&2
    exit 1
fi

printf '%s\n' "$newest"