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.

144 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. list_envs() {
  32. grep env: platformio.ini | sed 's/\[env:\(.*\)\]/\1/g'
  33. }
  34. travis=$(list_envs | grep travis | sort)
  35. available=$(list_envs | grep -Ev -- '-ota$|-ssl$|^travis' | sort)
  36. # Build tools settings
  37. export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -DAPP_REVISION='\"$git_revision\"'"
  38. # Functions
  39. print_available() {
  40. echo "--------------------------------------------------------------"
  41. echo "Available environments:"
  42. for environment in $available; do
  43. echo "* $environment"
  44. done
  45. }
  46. print_environments() {
  47. echo "--------------------------------------------------------------"
  48. echo "Current environments:"
  49. for environment in $environments; do
  50. echo "* $environment"
  51. done
  52. }
  53. set_default_environments() {
  54. # Hook to build in parallel when using travis
  55. if [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Release" ]] && ${par_build}; then
  56. environments=$(echo ${available} | \
  57. awk -v par_thread=${par_thread} -v par_total_threads=${par_total_threads} \
  58. '{ for (i = 1; i <= NF; i++) if (++j % par_total_threads == par_thread ) print $i; }')
  59. return
  60. fi
  61. # Only build travisN
  62. if [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Test" ]]; then
  63. environments=$travis
  64. return
  65. fi
  66. # Fallback to all available environments
  67. environments=$available
  68. }
  69. build_webui() {
  70. # Build system uses gulpscript.js to build web interface
  71. if [ ! -e node_modules/gulp/bin/gulp.js ]; then
  72. echo "--------------------------------------------------------------"
  73. echo "Installing dependencies..."
  74. npm install --only=dev
  75. fi
  76. # Recreate web interface (espurna/data/index.html.*.gz.h)
  77. echo "--------------------------------------------------------------"
  78. echo "Building web interface..."
  79. node node_modules/gulp/bin/gulp.js || exit
  80. }
  81. build_environments() {
  82. echo "--------------------------------------------------------------"
  83. echo "Building firmware images..."
  84. mkdir -p ../firmware/espurna-$version
  85. for environment in $environments; do
  86. echo -n "* espurna-$version-$environment.bin --- "
  87. platformio run --silent --environment $environment 2>/dev/null || exit 1
  88. stat -c %s .pioenvs/$environment/firmware.bin
  89. [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Test" ]] || \
  90. mv .pioenvs/$environment/firmware.bin ../firmware/espurna-$version/espurna-$version-$environment.bin
  91. done
  92. echo "--------------------------------------------------------------"
  93. }
  94. # Parameters
  95. while getopts "lp" opt; do
  96. case $opt in
  97. l)
  98. print_available
  99. exit
  100. ;;
  101. p)
  102. par_build=true
  103. ;;
  104. esac
  105. done
  106. shift $((OPTIND-1))
  107. # Welcome
  108. echo "--------------------------------------------------------------"
  109. echo "ESPURNA FIRMWARE BUILDER"
  110. echo "Building for version ${git_version}"
  111. # Environments to build
  112. environments=$@
  113. if [ $# -eq 0 ]; then
  114. set_default_environments
  115. fi
  116. if ${CI:-false}; then
  117. print_environments
  118. fi
  119. build_webui
  120. build_environments