build.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!/usr/bin/env bash
  2. global_usage() {
  3. cat - <<EOF
  4. Usage:
  5. sh build.sh <command> [target]
  6. Commands:
  7. help|usage|-h|--help: Shows this message.
  8. list|-l: List firmwares available to build.
  9. build-firmware <target>: Build the firmware for the given build target.
  10. build-firmwares: Build all firmwares for all targets.
  11. build-matching-firmwares <build-match-spec>: Build all firmwares for build targets containing the string given for <build-match-spec>.
  12. build-companion-firmwares: Build all companion firmwares for all build targets.
  13. build-repeater-firmwares: Build all repeater firmwares for all build targets.
  14. build-room-server-firmwares: Build all chat room server firmwares for all build targets.
  15. Examples:
  16. Build firmware for the "RAK_4631_repeater" device target
  17. $ sh build.sh build-firmware RAK_4631_repeater
  18. Build all firmwares for device targets containing the string "RAK_4631"
  19. $ sh build.sh build-matching-firmwares <build-match-spec>
  20. Build all companion firmwares
  21. $ sh build.sh build-companion-firmwares
  22. Build all repeater firmwares
  23. $ sh build.sh build-repeater-firmwares
  24. Build all chat room server firmwares
  25. $ sh build.sh build-room-server-firmwares
  26. Environment Variables:
  27. DISABLE_DEBUG=1: Disables all debug logging flags (MESH_DEBUG, MESH_PACKET_LOGGING, etc.)
  28. If not set, debug flags from variant platformio.ini files are used.
  29. Examples:
  30. Build without debug logging:
  31. $ export FIRMWARE_VERSION=v1.0.0
  32. $ export DISABLE_DEBUG=1
  33. $ sh build.sh build-firmware RAK_4631_repeater
  34. Build with debug logging (default, uses flags from variant files):
  35. $ export FIRMWARE_VERSION=v1.0.0
  36. $ sh build.sh build-firmware RAK_4631_repeater
  37. EOF
  38. }
  39. # get a list of pio env names that start with "env:"
  40. get_pio_envs() {
  41. pio project config | grep 'env:' | sed 's/env://'
  42. }
  43. # Catch cries for help before doing anything else.
  44. case $1 in
  45. help|usage|-h|--help)
  46. global_usage
  47. exit 1
  48. ;;
  49. list|-l)
  50. get_pio_envs
  51. exit 0
  52. ;;
  53. esac
  54. # $1 should be the string to find (case insensitive)
  55. get_pio_envs_containing_string() {
  56. shopt -s nocasematch
  57. envs=($(get_pio_envs))
  58. for env in "${envs[@]}"; do
  59. if [[ "$env" == *${1}* ]]; then
  60. echo $env
  61. fi
  62. done
  63. }
  64. # $1 should be the string to find (case insensitive)
  65. get_pio_envs_ending_with_string() {
  66. shopt -s nocasematch
  67. envs=($(get_pio_envs))
  68. for env in "${envs[@]}"; do
  69. if [[ "$env" == *${1} ]]; then
  70. echo $env
  71. fi
  72. done
  73. }
  74. # disable all debug logging flags if DISABLE_DEBUG=1 is set
  75. disable_debug_flags() {
  76. if [ "$DISABLE_DEBUG" == "1" ]; then
  77. 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"
  78. fi
  79. }
  80. # build firmware for the provided pio env in $1
  81. build_firmware() {
  82. # get git commit sha
  83. COMMIT_HASH=$(git rev-parse --short HEAD)
  84. # set firmware build date
  85. FIRMWARE_BUILD_DATE=$(date '+%d-%b-%Y')
  86. # get FIRMWARE_VERSION, which should be provided by the environment
  87. if [ -z "$FIRMWARE_VERSION" ]; then
  88. echo "FIRMWARE_VERSION must be set in environment"
  89. exit 1
  90. fi
  91. # set firmware version string
  92. # e.g: v1.0.0-abcdef
  93. FIRMWARE_VERSION_STRING="${FIRMWARE_VERSION}-${COMMIT_HASH}"
  94. # craft filename
  95. # e.g: RAK_4631_Repeater-v1.0.0-SHA
  96. FIRMWARE_FILENAME="$1-${FIRMWARE_VERSION_STRING}"
  97. # add firmware version info to end of existing platformio build flags in environment vars
  98. export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -DFIRMWARE_BUILD_DATE='\"${FIRMWARE_BUILD_DATE}\"' -DFIRMWARE_VERSION='\"${FIRMWARE_VERSION_STRING}\"'"
  99. # disable debug flags if requested
  100. disable_debug_flags
  101. # build firmware target
  102. pio run -e $1
  103. # build merge-bin for esp32 fresh install
  104. if [ -f .pio/build/$1/firmware.bin ]; then
  105. pio run -t mergebin -e $1
  106. fi
  107. # build .uf2 for nrf52 boards
  108. if [[ -f .pio/build/$1/firmware.zip && -f .pio/build/$1/firmware.hex ]]; then
  109. python3 bin/uf2conv/uf2conv.py .pio/build/$1/firmware.hex -c -o .pio/build/$1/firmware.uf2 -f 0xADA52840
  110. fi
  111. # copy .bin, .uf2, and .zip to out folder
  112. # e.g: Heltec_v3_room_server-v1.0.0-SHA.bin
  113. # e.g: RAK_4631_Repeater-v1.0.0-SHA.uf2
  114. # copy .bin for esp32 boards
  115. cp .pio/build/$1/firmware.bin out/${FIRMWARE_FILENAME}.bin 2>/dev/null || true
  116. cp .pio/build/$1/firmware-merged.bin out/${FIRMWARE_FILENAME}-merged.bin 2>/dev/null || true
  117. # copy .zip and .uf2 of nrf52 boards
  118. cp .pio/build/$1/firmware.uf2 out/${FIRMWARE_FILENAME}.uf2 2>/dev/null || true
  119. cp .pio/build/$1/firmware.zip out/${FIRMWARE_FILENAME}.zip 2>/dev/null || true
  120. }
  121. # firmwares containing $1 will be built
  122. build_all_firmwares_matching() {
  123. envs=($(get_pio_envs_containing_string "$1"))
  124. for env in "${envs[@]}"; do
  125. build_firmware $env
  126. done
  127. }
  128. # firmwares ending with $1 will be built
  129. build_all_firmwares_by_suffix() {
  130. envs=($(get_pio_envs_ending_with_string "$1"))
  131. for env in "${envs[@]}"; do
  132. build_firmware $env
  133. done
  134. }
  135. build_repeater_firmwares() {
  136. # # build specific repeater firmwares
  137. # build_firmware "Heltec_v2_repeater"
  138. # build_firmware "Heltec_v3_repeater"
  139. # build_firmware "Xiao_C3_Repeater_sx1262"
  140. # build_firmware "Xiao_S3_WIO_Repeater"
  141. # build_firmware "LilyGo_T3S3_sx1262_Repeater"
  142. # build_firmware "RAK_4631_Repeater"
  143. # build all repeater firmwares
  144. build_all_firmwares_by_suffix "_repeater"
  145. }
  146. build_companion_firmwares() {
  147. # # build specific companion firmwares
  148. # build_firmware "Heltec_v2_companion_radio_usb"
  149. # build_firmware "Heltec_v2_companion_radio_ble"
  150. # build_firmware "Heltec_v3_companion_radio_usb"
  151. # build_firmware "Heltec_v3_companion_radio_ble"
  152. # build_firmware "Xiao_S3_WIO_companion_radio_ble"
  153. # build_firmware "LilyGo_T3S3_sx1262_companion_radio_usb"
  154. # build_firmware "LilyGo_T3S3_sx1262_companion_radio_ble"
  155. # build_firmware "RAK_4631_companion_radio_usb"
  156. # build_firmware "RAK_4631_companion_radio_ble"
  157. # build_firmware "t1000e_companion_radio_ble"
  158. # build all companion firmwares
  159. build_all_firmwares_by_suffix "_companion_radio_usb"
  160. build_all_firmwares_by_suffix "_companion_radio_ble"
  161. }
  162. build_room_server_firmwares() {
  163. # # build specific room server firmwares
  164. # build_firmware "Heltec_v3_room_server"
  165. # build_firmware "RAK_4631_room_server"
  166. # build all room server firmwares
  167. build_all_firmwares_by_suffix "_room_server"
  168. }
  169. build_firmwares() {
  170. build_companion_firmwares
  171. build_repeater_firmwares
  172. build_room_server_firmwares
  173. }
  174. # clean build dir
  175. rm -rf out
  176. mkdir -p out
  177. # handle script args
  178. if [[ $1 == "build-firmware" ]]; then
  179. TARGETS=${@:2}
  180. if [ "$TARGETS" ]; then
  181. for env in $TARGETS; do
  182. build_firmware $env
  183. done
  184. else
  185. echo "usage: $0 build-firmware <target>"
  186. exit 1
  187. fi
  188. elif [[ $1 == "build-matching-firmwares" ]]; then
  189. if [ "$2" ]; then
  190. build_all_firmwares_matching $2
  191. else
  192. echo "usage: $0 build-matching-firmwares <build-match-spec>"
  193. exit 1
  194. fi
  195. elif [[ $1 == "build-firmwares" ]]; then
  196. build_firmwares
  197. elif [[ $1 == "build-companion-firmwares" ]]; then
  198. build_companion_firmwares
  199. elif [[ $1 == "build-repeater-firmwares" ]]; then
  200. build_repeater_firmwares
  201. elif [[ $1 == "build-room-server-firmwares" ]]; then
  202. build_room_server_firmwares
  203. fi