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.

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