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.

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