build.sh 6.2 KB

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