build.sh 10 KB

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