@@ -7,6 +7,7 @@ sh build.sh <command> [target]
77
88Commands:
99 help|usage|-h|--help: Shows this message.
10+ list|-l: List firmwares available to build.
1011 build-firmware <target>: Build the firmware for the given build target.
1112 build-firmwares: Build all firmwares for all targets.
1213 build-matching-firmwares <build-match-spec>: Build all firmwares for build targets containing the string given for <build-match-spec>.
@@ -46,19 +47,25 @@ $ sh build.sh build-firmware RAK_4631_repeater
4647EOF
4748}
4849
50+ # get a list of pio env names that start with "env:"
51+ get_pio_envs () {
52+ pio project config | grep ' env:' | sed ' s/env://'
53+ }
54+
4955# Catch cries for help before doing anything else.
5056case $1 in
5157 help|usage|-h|--help)
5258 global_usage
5359 exit 1
5460 ;;
61+ list|-l)
62+ get_pio_envs
63+ exit 0
64+ ;;
5565esac
5666
57-
58- # get a list of pio env names that start with "env:"
59- get_pio_envs () {
60- echo $( pio project config | grep ' env:' | sed ' s/env://' )
61- }
67+ # cache project config json for use in get_platform_for_env()
68+ PIO_CONFIG_JSON=$( pio project config --json-output)
6269
6370# $1 should be the string to find (case insensitive)
6471get_pio_envs_containing_string () {
@@ -82,6 +89,25 @@ get_pio_envs_ending_with_string() {
8289 done
8390}
8491
92+ # get platform flag for a given environment
93+ # $1 should be the environment name
94+ get_platform_for_env () {
95+ local env_name=$1
96+ echo " $PIO_CONFIG_JSON " | python3 -c "
97+ import sys, json, re
98+ data = json.load(sys.stdin)
99+ for section, options in data:
100+ if section == 'env:$env_name ':
101+ for key, value in options:
102+ if key == 'build_flags':
103+ for flag in value:
104+ match = re.search(r'(ESP32_PLATFORM|NRF52_PLATFORM|STM32_PLATFORM|RP2040_PLATFORM)', flag)
105+ if match:
106+ print(match.group(1))
107+ sys.exit(0)
108+ "
109+ }
110+
85111# disable all debug logging flags if DISABLE_DEBUG=1 is set
86112disable_debug_flags () {
87113 if [ " $DISABLE_DEBUG " == " 1" ]; then
@@ -91,6 +117,8 @@ disable_debug_flags() {
91117
92118# build firmware for the provided pio env in $1
93119build_firmware () {
120+ # get env platform for post build actions
121+ ENV_PLATFORM=($( get_platform_for_env $1 ) )
94122
95123 # get git commit sha
96124 COMMIT_HASH=$( git rev-parse --short HEAD)
@@ -121,27 +149,31 @@ build_firmware() {
121149 # build firmware target
122150 pio run -e $1
123151
124- # build merge-bin for esp32 fresh install
125- if [ -f .pio/build/ $1 /firmware.bin ]; then
152+ # build merge-bin for esp32 fresh install, copy .bins to out folder (e.g: Heltec_v3_room_server-v1.0.0-SHA.bin)
153+ if [ " $ENV_PLATFORM " == " ESP32_PLATFORM " ]; then
126154 pio run -t mergebin -e $1
155+ cp .pio/build/$1 /firmware.bin out/${FIRMWARE_FILENAME} .bin 2> /dev/null || true
156+ cp .pio/build/$1 /firmware-merged.bin out/${FIRMWARE_FILENAME} -merged.bin 2> /dev/null || true
127157 fi
128158
129- # build .uf2 for nrf52 boards
130- if [[ -f .pio/build/ $1 /firmware.zip && -f .pio/build/ $1 /firmware.hex ] ]; then
159+ # build .uf2 for nrf52 boards, copy .uf2 and .zip to out folder (e.g: RAK_4631_Repeater-v1.0.0-SHA.uf2)
160+ if [ " $ENV_PLATFORM " == " NRF52_PLATFORM " ]; then
131161 python3 bin/uf2conv/uf2conv.py .pio/build/$1 /firmware.hex -c -o .pio/build/$1 /firmware.uf2 -f 0xADA52840
162+ cp .pio/build/$1 /firmware.uf2 out/${FIRMWARE_FILENAME} .uf2 2> /dev/null || true
163+ cp .pio/build/$1 /firmware.zip out/${FIRMWARE_FILENAME} .zip 2> /dev/null || true
132164 fi
133165
134- # copy .bin, .uf2, and .zip to out folder
135- # e.g: Heltec_v3_room_server-v1.0.0-SHA.bin
136- # e.g: RAK_4631_Repeater-v1.0.0-SHA.uf2
137-
138- # copy .bin for esp32 boards
139- cp .pio/build/$1 /firmware.bin out/${FIRMWARE_FILENAME} .bin 2> /dev/null || true
140- cp .pio/build/$1 /firmware-merged.bin out/${FIRMWARE_FILENAME} -merged.bin 2> /dev/null || true
166+ # for stm32, copy .bin and .hex to out folder
167+ if [ " $ENV_PLATFORM " == " STM32_PLATFORM" ]; then
168+ cp .pio/build/$1 /firmware.bin out/${FIRMWARE_FILENAME} .bin 2> /dev/null || true
169+ cp .pio/build/$1 /firmware.hex out/${FIRMWARE_FILENAME} .hex 2> /dev/null || true
170+ fi
141171
142- # copy .zip and .uf2 of nrf52 boards
143- cp .pio/build/$1 /firmware.uf2 out/${FIRMWARE_FILENAME} .uf2 2> /dev/null || true
144- cp .pio/build/$1 /firmware.zip out/${FIRMWARE_FILENAME} .zip 2> /dev/null || true
172+ # for rp2040, copy .bin and .uf2 to out folder
173+ if [ " $ENV_PLATFORM " == " RP2040_PLATFORM" ]; then
174+ cp .pio/build/$1 /firmware.bin out/${FIRMWARE_FILENAME} .bin 2> /dev/null || true
175+ cp .pio/build/$1 /firmware.uf2 out/${FIRMWARE_FILENAME} .uf2 2> /dev/null || true
176+ fi
145177
146178}
147179
0 commit comments