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.

208 lines
5.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. script_build_environments=true
  20. script_build_webui=true
  21. if ${TRAVIS:-false}; then
  22. git_revision=${TRAVIS_COMMIT::7}
  23. git_tag=${TRAVIS_TAG}
  24. elif is_git; then
  25. git_revision=$(git rev-parse --short HEAD)
  26. git_tag=$(git tag --contains HEAD)
  27. else
  28. git_revision=unknown
  29. git_tag=
  30. fi
  31. if [[ -n $git_tag ]]; then
  32. new_version=${version/-*}
  33. sed -i -e "s@$version@$new_version@" $version_file
  34. version=$new_version
  35. trap "git checkout -- $version_file" EXIT
  36. fi
  37. par_build=false
  38. par_thread=${BUILDER_THREAD:-0}
  39. par_total_threads=${BUILDER_TOTAL_THREADS:-4}
  40. if [ ${par_thread} -ne ${par_thread} -o \
  41. ${par_total_threads} -ne ${par_total_threads} ]; then
  42. echo "Parallel threads should be a number."
  43. exit
  44. fi
  45. if [ ${par_thread} -ge ${par_total_threads} ]; then
  46. echo "Current thread is greater than total threads. Doesn't make sense"
  47. exit
  48. fi
  49. # Available environments
  50. list_envs() {
  51. grep env: platformio.ini | sed 's/\[env:\(.*\)\]/\1/g'
  52. }
  53. travis=$(list_envs | grep travis | sort)
  54. available=$(list_envs | grep -Ev -- '-ota$|-ssl$|^travis' | sort)
  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. print_getopts_help() {
  118. cat <<EOF
  119. Usage: $(basename "$0") [OPTION] <ENVIRONMENT>...
  120. Where ENVIRONMENT is environment name(s) from platformio.ini
  121. Options:
  122. -f VALUE Filter build stage by name to skip it
  123. Supported VALUEs are "environments" and "webui"
  124. Can be specified multiple times
  125. -l Print available environments
  126. -d VALUE Destination to move .bin files after building environments
  127. -p Enable parallel build
  128. Depends on following exported variables:
  129. BUILDER_THREAD=<number> (default 0...4)
  130. BUILDER_TOTAL_THREADS=<number> (default 4)
  131. When building platformio environments, will only pick every <BUILDER_THREAD>th
  132. -h Display this message
  133. EOF
  134. }
  135. while getopts "f:lpd:h" opt; do
  136. case $opt in
  137. f)
  138. case "$OPTARG" in
  139. webui) script_build_webui=false ;;
  140. environments) script_build_environments=false ;;
  141. esac
  142. ;;
  143. l)
  144. print_available
  145. exit
  146. ;;
  147. p)
  148. par_build=true
  149. ;;
  150. d)
  151. destination=$OPTARG
  152. ;;
  153. h)
  154. print_getopts_help
  155. exit
  156. esac
  157. done
  158. shift $((OPTIND-1))
  159. # Welcome
  160. echo "--------------------------------------------------------------"
  161. echo "ESPURNA FIRMWARE BUILDER"
  162. echo "Building for version ${version}" ${git_revision:+($git_revision)}
  163. # Environments to build
  164. environments=$@
  165. if $script_build_webui ; then
  166. build_webui
  167. fi
  168. if $script_build_environments ; then
  169. if [ $# -eq 0 ]; then
  170. set_default_environments
  171. fi
  172. if ${CI:-false}; then
  173. print_environments
  174. fi
  175. build_environments
  176. fi