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.

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