build.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. if [ -z "$COMMIT_HASH" ]; then
  130. COMMIT_HASH=$(git rev-parse --short HEAD)
  131. fi
  132. # set firmware build date
  133. FIRMWARE_BUILD_DATE=$(date '+%d-%b-%Y')
  134. # get FIRMWARE_VERSION, which should be provided by the environment
  135. if [ -z "$FIRMWARE_VERSION" ]; then
  136. echo "FIRMWARE_VERSION must be set in environment"
  137. exit 1
  138. fi
  139. # set artifact version string
  140. # e.g: v1.2.3-meshcoretel-v1.0.1-abcdef
  141. FIRMWARE_VERSION_BASE="${FIRMWARE_VERSION}"
  142. if [ -n "$MESHCORETEL_VERSION" ]; then
  143. FIRMWARE_VERSION_BASE="${FIRMWARE_VERSION_BASE}-vbart-meshcoretel-${MESHCORETEL_VERSION}"
  144. fi
  145. FIRMWARE_VERSION_STRING="${FIRMWARE_VERSION_BASE}-${COMMIT_HASH}"
  146. CLIENT_VERSION_STRING="meshcoretel-${COMMIT_HASH}"
  147. if [ -n "$MESHCORETEL_VERSION" ]; then
  148. CLIENT_VERSION_STRING="vbart-meshcoretel-${MESHCORETEL_VERSION}-${COMMIT_HASH}"
  149. fi
  150. # craft filename
  151. # e.g: RAK_4631_Repeater-v1.0.0-SHA
  152. FIRMWARE_FILENAME="$1-${FIRMWARE_VERSION_STRING}"
  153. # add build metadata to end of existing platformio build flags in environment vars
  154. export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -DFIRMWARE_BUILD_DATE='\"${FIRMWARE_BUILD_DATE}\"' -DFIRMWARE_VERSION='\"${FIRMWARE_VERSION_STRING}\"' -DCLIENT_VERSION='\"${CLIENT_VERSION_STRING}\"'"
  155. # disable debug flags if requested
  156. disable_debug_flags
  157. # build firmware target
  158. run_pio run -e $1
  159. # build merge-bin for esp32 fresh install, copy .bins to out folder (e.g: Heltec_v3_room_server-v1.0.0-SHA.bin)
  160. if [ "$ENV_PLATFORM" == "ESP32_PLATFORM" ]; then
  161. run_pio run -t mergebin -e $1
  162. cp .pio/build/$1/firmware.bin out/${FIRMWARE_FILENAME}.bin 2>/dev/null || true
  163. cp .pio/build/$1/firmware-merged.bin out/${FIRMWARE_FILENAME}-merged.bin 2>/dev/null || true
  164. fi
  165. # build .uf2 for nrf52 boards, copy .uf2 and .zip to out folder (e.g: RAK_4631_Repeater-v1.0.0-SHA.uf2)
  166. if [ "$ENV_PLATFORM" == "NRF52_PLATFORM" ]; then
  167. run_python bin/uf2conv/uf2conv.py .pio/build/$1/firmware.hex -c -o .pio/build/$1/firmware.uf2 -f 0xADA52840
  168. cp .pio/build/$1/firmware.uf2 out/${FIRMWARE_FILENAME}.uf2 2>/dev/null || true
  169. cp .pio/build/$1/firmware.zip out/${FIRMWARE_FILENAME}.zip 2>/dev/null || true
  170. fi
  171. # for stm32, copy .bin and .hex to out folder
  172. if [ "$ENV_PLATFORM" == "STM32_PLATFORM" ]; then
  173. cp .pio/build/$1/firmware.bin out/${FIRMWARE_FILENAME}.bin 2>/dev/null || true
  174. cp .pio/build/$1/firmware.hex out/${FIRMWARE_FILENAME}.hex 2>/dev/null || true
  175. fi
  176. # for rp2040, copy .bin and .uf2 to out folder
  177. if [ "$ENV_PLATFORM" == "RP2040_PLATFORM" ]; then
  178. cp .pio/build/$1/firmware.bin out/${FIRMWARE_FILENAME}.bin 2>/dev/null || true
  179. cp .pio/build/$1/firmware.uf2 out/${FIRMWARE_FILENAME}.uf2 2>/dev/null || true
  180. fi
  181. }
  182. # firmwares containing $1 will be built
  183. build_all_firmwares_matching() {
  184. envs=($(get_pio_envs_containing_string "$1"))
  185. for env in "${envs[@]}"; do
  186. build_firmware $env
  187. done
  188. }
  189. # firmwares ending with $1 will be built
  190. build_all_firmwares_by_suffix() {
  191. envs=($(get_pio_envs_ending_with_string "$1"))
  192. for env in "${envs[@]}"; do
  193. build_firmware $env
  194. done
  195. }
  196. build_repeater_firmwares() {
  197. # # build specific repeater firmwares
  198. # build_firmware "Heltec_v2_repeater"
  199. # build_firmware "Heltec_v3_repeater"
  200. # build_firmware "Xiao_C3_Repeater_sx1262"
  201. # build_firmware "Xiao_S3_WIO_Repeater"
  202. # build_firmware "LilyGo_T3S3_sx1262_Repeater"
  203. # build_firmware "RAK_4631_Repeater"
  204. # build all repeater firmwares
  205. build_all_firmwares_by_suffix "_repeater"
  206. }
  207. build_companion_firmwares() {
  208. # # build specific companion firmwares
  209. # build_firmware "Heltec_v2_companion_radio_usb"
  210. # build_firmware "Heltec_v2_companion_radio_ble"
  211. # build_firmware "Heltec_v3_companion_radio_usb"
  212. # build_firmware "Heltec_v3_companion_radio_ble"
  213. # build_firmware "Xiao_S3_WIO_companion_radio_ble"
  214. # build_firmware "LilyGo_T3S3_sx1262_companion_radio_usb"
  215. # build_firmware "LilyGo_T3S3_sx1262_companion_radio_ble"
  216. # build_firmware "RAK_4631_companion_radio_usb"
  217. # build_firmware "RAK_4631_companion_radio_ble"
  218. # build_firmware "t1000e_companion_radio_ble"
  219. # build all companion firmwares
  220. build_all_firmwares_by_suffix "_companion_radio_usb"
  221. build_all_firmwares_by_suffix "_companion_radio_ble"
  222. }
  223. build_companion_wifi_firmwares() {
  224. build_all_firmwares_by_suffix "_companion_radio_wifi"
  225. }
  226. build_repeater_mqtt_firmwares() {
  227. build_all_firmwares_by_suffix "_repeater_mqtt"
  228. }
  229. build_room_server_firmwares() {
  230. # # build specific room server firmwares
  231. # build_firmware "Heltec_v3_room_server"
  232. # build_firmware "RAK_4631_room_server"
  233. # build all room server firmwares
  234. build_all_firmwares_by_suffix "_room_server"
  235. }
  236. build_firmwares() {
  237. build_companion_firmwares
  238. build_repeater_firmwares
  239. build_room_server_firmwares
  240. }
  241. # clean build dir
  242. rm -rf out
  243. mkdir -p out
  244. # handle script args
  245. if [[ $1 == "build-firmware" ]]; then
  246. TARGETS=${@:2}
  247. if [ "$TARGETS" ]; then
  248. for env in $TARGETS; do
  249. build_firmware $env
  250. done
  251. else
  252. echo "usage: $0 build-firmware <target>"
  253. exit 1
  254. fi
  255. elif [[ $1 == "build-matching-firmwares" ]]; then
  256. if [ "$2" ]; then
  257. build_all_firmwares_matching $2
  258. else
  259. echo "usage: $0 build-matching-firmwares <build-match-spec>"
  260. exit 1
  261. fi
  262. elif [[ $1 == "build-firmwares" ]]; then
  263. build_firmwares
  264. elif [[ $1 == "build-companion-firmwares" ]]; then
  265. build_companion_firmwares
  266. elif [[ $1 == "build-companion-wifi-firmwares" ]]; then
  267. build_companion_wifi_firmwares
  268. elif [[ $1 == "build-repeater-firmwares" ]]; then
  269. build_repeater_firmwares
  270. elif [[ $1 == "build-repeater-mqtt-firmwares" ]]; then
  271. build_repeater_mqtt_firmwares
  272. elif [[ $1 == "build-room-server-firmwares" ]]; then
  273. build_room_server_firmwares
  274. fi