build.sh 9.1 KB

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