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.

140 lines
3.8 KiB

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