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.

167 lines
4.3 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. }
  98. build_environments() {
  99. echo "--------------------------------------------------------------"
  100. echo "Building firmware images..."
  101. mkdir -p ../firmware/espurna-$version
  102. for environment in $environments; do
  103. echo -n "* espurna-$version-$environment.bin --- "
  104. platformio run --silent --environment $environment || exit 1
  105. stat_bytes .pioenvs/$environment/firmware.bin
  106. [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Test" ]] || \
  107. mv .pioenvs/$environment/firmware.bin $destination/espurna-$version/espurna-$version-$environment.bin
  108. done
  109. echo "--------------------------------------------------------------"
  110. }
  111. # Parameters
  112. while getopts "lpd:" opt; do
  113. case $opt in
  114. l)
  115. print_available
  116. exit
  117. ;;
  118. p)
  119. par_build=true
  120. ;;
  121. d)
  122. destination=$OPTARG
  123. ;;
  124. esac
  125. done
  126. shift $((OPTIND-1))
  127. # Welcome
  128. echo "--------------------------------------------------------------"
  129. echo "ESPURNA FIRMWARE BUILDER"
  130. echo "Building for version ${version}" ${git_revision:+($git_revision)}
  131. # Environments to build
  132. environments=$@
  133. if [ $# -eq 0 ]; then
  134. set_default_environments
  135. fi
  136. if ${CI:-false}; then
  137. print_environments
  138. fi
  139. build_webui
  140. build_environments