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.

170 lines
4.4 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. # Functions
  54. print_available() {
  55. echo "--------------------------------------------------------------"
  56. echo "Available environments:"
  57. for environment in $available; do
  58. echo "* $environment"
  59. done
  60. }
  61. print_environments() {
  62. echo "--------------------------------------------------------------"
  63. echo "Current environments:"
  64. for environment in $environments; do
  65. echo "* $environment"
  66. done
  67. }
  68. set_default_environments() {
  69. # Hook to build in parallel when using travis
  70. if [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Release" ]] && ${par_build}; then
  71. environments=$(echo ${available} | \
  72. awk -v par_thread=${par_thread} -v par_total_threads=${par_total_threads} \
  73. '{ for (i = 1; i <= NF; i++) if (++j % par_total_threads == par_thread ) print $i; }')
  74. return
  75. fi
  76. # Only build travisN
  77. if [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Test" ]]; then
  78. environments=$travis
  79. return
  80. fi
  81. # Fallback to all available environments
  82. environments=$available
  83. }
  84. build_webui() {
  85. # Build system uses gulpscript.js to build web interface
  86. if [ ! -e node_modules/gulp/bin/gulp.js ]; then
  87. echo "--------------------------------------------------------------"
  88. echo "Installing dependencies..."
  89. npm install --only=dev
  90. fi
  91. # Recreate web interface (espurna/data/index.html.*.gz.h)
  92. echo "--------------------------------------------------------------"
  93. echo "Building web interface..."
  94. node node_modules/gulp/bin/gulp.js || exit
  95. # TODO: do something if webui files are different
  96. # for now, just print in travis log
  97. if ${TRAVIS:-false}; then
  98. git --no-pager diff --stat
  99. fi
  100. }
  101. build_environments() {
  102. echo "--------------------------------------------------------------"
  103. echo "Building firmware images..."
  104. mkdir -p $destination/espurna-$version
  105. for environment in $environments; do
  106. echo -n "* espurna-$version-$environment.bin --- "
  107. platformio run --silent --environment $environment || exit 1
  108. stat_bytes .pio/build/$environment/firmware.bin
  109. [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Test" ]] || \
  110. mv .pio/build/$environment/firmware.bin $destination/espurna-$version/espurna-$version-$environment.bin
  111. done
  112. echo "--------------------------------------------------------------"
  113. }
  114. # Parameters
  115. while getopts "lpd:" opt; do
  116. case $opt in
  117. l)
  118. print_available
  119. exit
  120. ;;
  121. p)
  122. par_build=true
  123. ;;
  124. d)
  125. destination=$OPTARG
  126. ;;
  127. esac
  128. done
  129. shift $((OPTIND-1))
  130. # Welcome
  131. echo "--------------------------------------------------------------"
  132. echo "ESPURNA FIRMWARE BUILDER"
  133. echo "Building for version ${version}" ${git_revision:+($git_revision)}
  134. # Environments to build
  135. environments=$@
  136. if [ $# -eq 0 ]; then
  137. set_default_environments
  138. fi
  139. if ${CI:-false}; then
  140. print_environments
  141. fi
  142. build_webui
  143. build_environments