Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.8 KiB

  1. #!/bin/bash
  2. set -e
  3. # Script settings
  4. version=$(grep APP_VERSION espurna/config/version.h | awk '{print $3}' | sed 's/"//g')
  5. (command -v git && git rev-parse --is-inside-work-tree) 2>&1>/dev/null
  6. if [ $? -eq 0 ]; then
  7. git_revision=$(git rev-parse --short HEAD)
  8. git_version=$(git describe --tags)
  9. else
  10. git_revision=
  11. git_version=$version
  12. fi
  13. par_build=0
  14. par_thread=${BUILDER_THREAD:-0}
  15. par_total_threads=${BUILDER_TOTAL_THREADS:-4}
  16. if [ ${par_thread} -ne ${par_thread} -o \
  17. ${par_total_threads} -ne ${par_total_threads} ]; then
  18. echo "Parallel threads should be a number."
  19. exit
  20. fi
  21. if [ ${par_thread} -ge ${par_total_threads} ]; then
  22. echo "Current thread is greater than total threads. Doesn't make sense"
  23. exit
  24. fi
  25. # Available environments
  26. travis=$(grep env: platformio.ini | grep travis | sed 's/\[env://' | sed 's/\]/ /' | sort)
  27. available=$(grep env: platformio.ini | grep -v ota | grep -v ssl | grep -v travis | sed 's/\[env://' | sed 's/\]/ /' | sort)
  28. # Build tools settings
  29. export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -DAPP_REVISION='\"$git_revision\"'"
  30. # Functions
  31. print_available() {
  32. echo "--------------------------------------------------------------"
  33. echo "Available environments:"
  34. for environment in $available; do
  35. echo "* $environment"
  36. done
  37. }
  38. print_environments() {
  39. echo "--------------------------------------------------------------"
  40. echo "Current environments:"
  41. for environment in $environments; do
  42. echo "* $environment"
  43. done
  44. }
  45. set_default_environments() {
  46. # Hook to build in parallel when using travis
  47. if [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Release" ]] && [ ${par_build} ]; then
  48. environments=$(echo ${available} | \
  49. awk -v par_thread=${par_thread} -v par_total_threads=${par_total_threads} \
  50. '{ for (i = 1; i <= NF; i++) if (++j % par_total_threads == par_thread ) print $i; }')
  51. return
  52. fi
  53. # Only build travisN
  54. if [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Test" ]]; then
  55. environments=$travis
  56. return
  57. fi
  58. # Fallback to all available environments
  59. environments=$available
  60. }
  61. build_webui() {
  62. # Build system uses gulpscript.js to build web interface
  63. if [ ! -e node_modules/gulp/bin/gulp.js ]; then
  64. echo "--------------------------------------------------------------"
  65. echo "Installing dependencies..."
  66. npm install --only=dev
  67. fi
  68. # Recreate web interface (espurna/data/index.html.*.gz.h)
  69. echo "--------------------------------------------------------------"
  70. echo "Building web interface..."
  71. node node_modules/gulp/bin/gulp.js || exit
  72. }
  73. build_environments() {
  74. echo "--------------------------------------------------------------"
  75. echo "Building firmware images..."
  76. mkdir -p ../firmware/espurna-$version
  77. for environment in $environments; do
  78. echo -n "* espurna-$version-$environment.bin --- "
  79. platformio run --silent --environment $environment || exit 1
  80. stat -c %s .pioenvs/$environment/firmware.bin
  81. [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Test" ]] || \
  82. mv .pioenvs/$environment/firmware.bin ../firmware/espurna-$version/espurna-$version-$environment.bin
  83. done
  84. echo "--------------------------------------------------------------"
  85. }
  86. # Parameters
  87. while getopts "lp" opt; do
  88. case $opt in
  89. l)
  90. print_available
  91. exit
  92. ;;
  93. p)
  94. par_build=1
  95. ;;
  96. esac
  97. done
  98. shift $((OPTIND-1))
  99. # Welcome
  100. echo "--------------------------------------------------------------"
  101. echo "ESPURNA FIRMWARE BUILDER"
  102. echo "Building for version ${git_version}"
  103. # Environments to build
  104. environments=$@
  105. if [ $# -eq 0 ]; then
  106. set_default_environments
  107. fi
  108. if [[ "${CI}" = true ]]; then
  109. print_environments
  110. fi
  111. build_webui
  112. build_environments