build.sh 7.2 KB

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