This commit is contained in:
Oleg Kulachenko 2026-04-20 10:53:44 +02:00 committed by GitHub
commit d54bed7e2e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

116
build.sh
View file

@ -3,7 +3,7 @@
global_usage() { global_usage() {
cat - <<EOF cat - <<EOF
Usage: Usage:
sh build.sh <command> [target] bash build.sh <command> [target]
Commands: Commands:
help|usage|-h|--help: Shows this message. help|usage|-h|--help: Shows this message.
@ -17,19 +17,19 @@ Commands:
Examples: Examples:
Build firmware for the "RAK_4631_repeater" device target Build firmware for the "RAK_4631_repeater" device target
$ sh build.sh build-firmware RAK_4631_repeater $ bash build.sh build-firmware RAK_4631_repeater
Build all firmwares for device targets containing the string "RAK_4631" Build all firmwares for device targets containing the string "RAK_4631"
$ sh build.sh build-matching-firmwares <build-match-spec> $ bash build.sh build-matching-firmwares <build-match-spec>
Build all companion firmwares Build all companion firmwares
$ sh build.sh build-companion-firmwares $ bash build.sh build-companion-firmwares
Build all repeater firmwares Build all repeater firmwares
$ sh build.sh build-repeater-firmwares $ bash build.sh build-repeater-firmwares
Build all chat room server firmwares Build all chat room server firmwares
$ sh build.sh build-room-server-firmwares $ bash build.sh build-room-server-firmwares
Environment Variables: Environment Variables:
DISABLE_DEBUG=1: Disables all debug logging flags (MESH_DEBUG, MESH_PACKET_LOGGING, etc.) DISABLE_DEBUG=1: Disables all debug logging flags (MESH_DEBUG, MESH_PACKET_LOGGING, etc.)
@ -39,17 +39,19 @@ Examples:
Build without debug logging: Build without debug logging:
$ export FIRMWARE_VERSION=v1.0.0 $ export FIRMWARE_VERSION=v1.0.0
$ export DISABLE_DEBUG=1 $ export DISABLE_DEBUG=1
$ sh build.sh build-firmware RAK_4631_repeater $ bash build.sh build-firmware RAK_4631_repeater
Build with debug logging (default, uses flags from variant files): Build with debug logging (default, uses flags from variant files):
$ export FIRMWARE_VERSION=v1.0.0 $ export FIRMWARE_VERSION=v1.0.0
$ sh build.sh build-firmware RAK_4631_repeater $ bash build.sh build-firmware RAK_4631_repeater
EOF EOF
} }
# get a list of pio env names that start with "env:" # get a list of pio env names that start with "env:"
get_pio_envs() { get_pio_envs() {
pio project config | grep 'env:' | sed 's/env://' local pio_output
pio_output=$(pio project config) || return $?
sed -n 's/^env://p' <<< "${pio_output}"
} }
# Catch cries for help before doing anything else. # Catch cries for help before doing anything else.
@ -62,6 +64,9 @@ case $1 in
get_pio_envs get_pio_envs
exit 0 exit 0
;; ;;
*)
# Fall through to main command handling below
;;
esac esac
# cache project config json for use in get_platform_for_env() # cache project config json for use in get_platform_for_env()
@ -70,10 +75,15 @@ PIO_CONFIG_JSON=$(pio project config --json-output)
# $1 should be the string to find (case insensitive) # $1 should be the string to find (case insensitive)
get_pio_envs_containing_string() { get_pio_envs_containing_string() {
shopt -s nocasematch shopt -s nocasematch
envs=($(get_pio_envs)) local env_list envs
env_list=$(get_pio_envs) || return $?
mapfile -t envs <<< "${env_list}"
if [[ ${#envs[@]} -gt 0 && -z "${envs[-1]}" ]]; then
unset 'envs[-1]'
fi
for env in "${envs[@]}"; do for env in "${envs[@]}"; do
if [[ "$env" == *${1}* ]]; then if [[ "${env}" == *"${1}"* ]]; then
echo $env echo "${env}"
fi fi
done done
} }
@ -81,10 +91,15 @@ get_pio_envs_containing_string() {
# $1 should be the string to find (case insensitive) # $1 should be the string to find (case insensitive)
get_pio_envs_ending_with_string() { get_pio_envs_ending_with_string() {
shopt -s nocasematch shopt -s nocasematch
envs=($(get_pio_envs)) local env_list envs
env_list=$(get_pio_envs) || return $?
mapfile -t envs <<< "${env_list}"
if [[ ${#envs[@]} -gt 0 && -z "${envs[-1]}" ]]; then
unset 'envs[-1]'
fi
for env in "${envs[@]}"; do for env in "${envs[@]}"; do
if [[ "$env" == *${1} ]]; then if [[ "${env}" == *"${1}" ]]; then
echo $env echo "${env}"
fi fi
done done
} }
@ -93,11 +108,11 @@ get_pio_envs_ending_with_string() {
# $1 should be the environment name # $1 should be the environment name
get_platform_for_env() { get_platform_for_env() {
local env_name=$1 local env_name=$1
echo "$PIO_CONFIG_JSON" | python3 -c " echo "${PIO_CONFIG_JSON}" | python3 -c "
import sys, json, re import sys, json, re
data = json.load(sys.stdin) data = json.load(sys.stdin)
for section, options in data: for section, options in data:
if section == 'env:$env_name': if section == 'env:${env_name}':
for key, value in options: for key, value in options:
if key == 'build_flags': if key == 'build_flags':
for flag in value: for flag in value:
@ -110,7 +125,8 @@ for section, options in data:
# disable all debug logging flags if DISABLE_DEBUG=1 is set # disable all debug logging flags if DISABLE_DEBUG=1 is set
disable_debug_flags() { disable_debug_flags() {
if [ "$DISABLE_DEBUG" == "1" ]; then # shellcheck disable=SC2154
if [[ "${DISABLE_DEBUG}" == "1" ]]; then
export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -UMESH_DEBUG -UBLE_DEBUG_LOGGING -UWIFI_DEBUG_LOGGING -UBRIDGE_DEBUG -UGPS_NMEA_DEBUG -UCORE_DEBUG_LEVEL -UESPNOW_DEBUG_LOGGING -UDEBUG_RP2040_WIRE -UDEBUG_RP2040_SPI -UDEBUG_RP2040_CORE -UDEBUG_RP2040_PORT -URADIOLIB_DEBUG_SPI -UCFG_DEBUG -URADIOLIB_DEBUG_BASIC -URADIOLIB_DEBUG_PROTOCOL" export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -UMESH_DEBUG -UBLE_DEBUG_LOGGING -UWIFI_DEBUG_LOGGING -UBRIDGE_DEBUG -UGPS_NMEA_DEBUG -UCORE_DEBUG_LEVEL -UESPNOW_DEBUG_LOGGING -UDEBUG_RP2040_WIRE -UDEBUG_RP2040_SPI -UDEBUG_RP2040_CORE -UDEBUG_RP2040_PORT -URADIOLIB_DEBUG_SPI -UCFG_DEBUG -URADIOLIB_DEBUG_BASIC -URADIOLIB_DEBUG_PROTOCOL"
fi fi
} }
@ -118,7 +134,7 @@ disable_debug_flags() {
# build firmware for the provided pio env in $1 # build firmware for the provided pio env in $1
build_firmware() { build_firmware() {
# get env platform for post build actions # get env platform for post build actions
ENV_PLATFORM=($(get_platform_for_env $1)) ENV_PLATFORM=$(get_platform_for_env "$1")
# get git commit sha # get git commit sha
COMMIT_HASH=$(git rev-parse --short HEAD) COMMIT_HASH=$(git rev-parse --short HEAD)
@ -127,7 +143,7 @@ build_firmware() {
FIRMWARE_BUILD_DATE=$(date '+%d-%b-%Y') FIRMWARE_BUILD_DATE=$(date '+%d-%b-%Y')
# get FIRMWARE_VERSION, which should be provided by the environment # get FIRMWARE_VERSION, which should be provided by the environment
if [ -z "$FIRMWARE_VERSION" ]; then if [[ -z "${FIRMWARE_VERSION}" ]]; then
echo "FIRMWARE_VERSION must be set in environment" echo "FIRMWARE_VERSION must be set in environment"
exit 1 exit 1
fi fi
@ -147,49 +163,59 @@ build_firmware() {
disable_debug_flags disable_debug_flags
# build firmware target # build firmware target
pio run -e $1 pio run -e "$1"
# build merge-bin for esp32 fresh install, copy .bins to out folder (e.g: Heltec_v3_room_server-v1.0.0-SHA.bin) # build merge-bin for esp32 fresh install, copy .bins to out folder (e.g: Heltec_v3_room_server-v1.0.0-SHA.bin)
if [ "$ENV_PLATFORM" == "ESP32_PLATFORM" ]; then if [[ "${ENV_PLATFORM}" == "ESP32_PLATFORM" ]]; then
pio run -t mergebin -e $1 pio run -t mergebin -e "$1"
cp .pio/build/$1/firmware.bin out/${FIRMWARE_FILENAME}.bin 2>/dev/null || true cp ".pio/build/$1/firmware.bin" "out/${FIRMWARE_FILENAME}.bin" 2>/dev/null || true
cp .pio/build/$1/firmware-merged.bin out/${FIRMWARE_FILENAME}-merged.bin 2>/dev/null || true cp ".pio/build/$1/firmware-merged.bin" "out/${FIRMWARE_FILENAME}-merged.bin" 2>/dev/null || true
fi fi
# build .uf2 for nrf52 boards, copy .uf2 and .zip to out folder (e.g: RAK_4631_Repeater-v1.0.0-SHA.uf2) # build .uf2 for nrf52 boards, copy .uf2 and .zip to out folder (e.g: RAK_4631_Repeater-v1.0.0-SHA.uf2)
if [ "$ENV_PLATFORM" == "NRF52_PLATFORM" ]; then if [[ "${ENV_PLATFORM}" == "NRF52_PLATFORM" ]]; then
python3 bin/uf2conv/uf2conv.py .pio/build/$1/firmware.hex -c -o .pio/build/$1/firmware.uf2 -f 0xADA52840 python3 bin/uf2conv/uf2conv.py ".pio/build/$1/firmware.hex" -c -o ".pio/build/$1/firmware.uf2" -f 0xADA52840
cp .pio/build/$1/firmware.uf2 out/${FIRMWARE_FILENAME}.uf2 2>/dev/null || true cp ".pio/build/$1/firmware.uf2" "out/${FIRMWARE_FILENAME}.uf2" 2>/dev/null || true
cp .pio/build/$1/firmware.zip out/${FIRMWARE_FILENAME}.zip 2>/dev/null || true cp ".pio/build/$1/firmware.zip" "out/${FIRMWARE_FILENAME}.zip" 2>/dev/null || true
fi fi
# for stm32, copy .bin and .hex to out folder # for stm32, copy .bin and .hex to out folder
if [ "$ENV_PLATFORM" == "STM32_PLATFORM" ]; then if [[ "${ENV_PLATFORM}" == "STM32_PLATFORM" ]]; then
cp .pio/build/$1/firmware.bin out/${FIRMWARE_FILENAME}.bin 2>/dev/null || true cp ".pio/build/$1/firmware.bin" "out/${FIRMWARE_FILENAME}.bin" 2>/dev/null || true
cp .pio/build/$1/firmware.hex out/${FIRMWARE_FILENAME}.hex 2>/dev/null || true cp ".pio/build/$1/firmware.hex" "out/${FIRMWARE_FILENAME}.hex" 2>/dev/null || true
fi fi
# for rp2040, copy .bin and .uf2 to out folder # for rp2040, copy .bin and .uf2 to out folder
if [ "$ENV_PLATFORM" == "RP2040_PLATFORM" ]; then if [[ "${ENV_PLATFORM}" == "RP2040_PLATFORM" ]]; then
cp .pio/build/$1/firmware.bin out/${FIRMWARE_FILENAME}.bin 2>/dev/null || true cp ".pio/build/$1/firmware.bin" "out/${FIRMWARE_FILENAME}.bin" 2>/dev/null || true
cp .pio/build/$1/firmware.uf2 out/${FIRMWARE_FILENAME}.uf2 2>/dev/null || true cp ".pio/build/$1/firmware.uf2" "out/${FIRMWARE_FILENAME}.uf2" 2>/dev/null || true
fi fi
} }
# firmwares containing $1 will be built # firmwares containing $1 will be built
build_all_firmwares_matching() { build_all_firmwares_matching() {
envs=($(get_pio_envs_containing_string "$1")) local env_list envs
env_list=$(get_pio_envs_containing_string "$1") || return $?
mapfile -t envs <<< "${env_list}"
if [[ ${#envs[@]} -gt 0 && -z "${envs[-1]}" ]]; then
unset 'envs[-1]'
fi
for env in "${envs[@]}"; do for env in "${envs[@]}"; do
build_firmware $env build_firmware "${env}"
done done
} }
# firmwares ending with $1 will be built # firmwares ending with $1 will be built
build_all_firmwares_by_suffix() { build_all_firmwares_by_suffix() {
envs=($(get_pio_envs_ending_with_string "$1")) local env_list envs
env_list=$(get_pio_envs_ending_with_string "$1") || return $?
mapfile -t envs <<< "${env_list}"
if [[ ${#envs[@]} -gt 0 && -z "${envs[-1]}" ]]; then
unset 'envs[-1]'
fi
for env in "${envs[@]}"; do for env in "${envs[@]}"; do
build_firmware $env build_firmware "${env}"
done done
} }
@ -251,18 +277,18 @@ mkdir -p out
# handle script args # handle script args
if [[ $1 == "build-firmware" ]]; then if [[ $1 == "build-firmware" ]]; then
TARGETS=${@:2} TARGETS=("${@:2}")
if [ "$TARGETS" ]; then if [[ -n "${TARGETS[*]}" ]]; then
for env in $TARGETS; do for env in "${TARGETS[@]}"; do
build_firmware $env build_firmware "${env}"
done done
else else
echo "usage: $0 build-firmware <target>" echo "usage: $0 build-firmware <target>"
exit 1 exit 1
fi fi
elif [[ $1 == "build-matching-firmwares" ]]; then elif [[ $1 == "build-matching-firmwares" ]]; then
if [ "$2" ]; then if [[ -n "$2" ]]; then
build_all_firmwares_matching $2 build_all_firmwares_matching "$2"
else else
echo "usage: $0 build-matching-firmwares <build-match-spec>" echo "usage: $0 build-matching-firmwares <build-match-spec>"
exit 1 exit 1