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.

209 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 "* espurna-$version-$environment.bin"
  109. platformio run --silent --environment $environment || exit 1
  110. echo -n "SIZE: "
  111. stat_bytes .pio/build/$environment/firmware.bin
  112. [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Test" ]] || \
  113. mv .pio/build/$environment/firmware.bin $destination/espurna-$version/espurna-$version-$environment.bin
  114. done
  115. echo "--------------------------------------------------------------"
  116. }
  117. # Parameters
  118. print_getopts_help() {
  119. cat <<EOF
  120. Usage: $(basename "$0") [OPTION] <ENVIRONMENT>...
  121. Where ENVIRONMENT is environment name(s) from platformio.ini
  122. Options:
  123. -f VALUE Filter build stage by name to skip it
  124. Supported VALUEs are "environments" and "webui"
  125. Can be specified multiple times
  126. -l Print available environments
  127. -d VALUE Destination to move .bin files after building environments
  128. -p Enable parallel build
  129. Depends on following exported variables:
  130. BUILDER_THREAD=<number> (default 0...4)
  131. BUILDER_TOTAL_THREADS=<number> (default 4)
  132. When building platformio environments, will only pick every <BUILDER_THREAD>th
  133. -h Display this message
  134. EOF
  135. }
  136. while getopts "f:lpd:h" opt; do
  137. case $opt in
  138. f)
  139. case "$OPTARG" in
  140. webui) script_build_webui=false ;;
  141. environments) script_build_environments=false ;;
  142. esac
  143. ;;
  144. l)
  145. print_available
  146. exit
  147. ;;
  148. p)
  149. par_build=true
  150. ;;
  151. d)
  152. destination=$OPTARG
  153. ;;
  154. h)
  155. print_getopts_help
  156. exit
  157. esac
  158. done
  159. shift $((OPTIND-1))
  160. # Welcome
  161. echo "--------------------------------------------------------------"
  162. echo "ESPURNA FIRMWARE BUILDER"
  163. echo "Building for version ${version}" ${git_revision:+($git_revision)}
  164. # Environments to build
  165. environments=$@
  166. if $script_build_webui ; then
  167. build_webui
  168. fi
  169. if $script_build_environments ; then
  170. if [ $# -eq 0 ]; then
  171. set_default_environments
  172. fi
  173. if ${CI:-false}; then
  174. print_environments
  175. fi
  176. build_environments
  177. fi