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.

192 lines
4.8 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. release_mode=false
  22. if ${TRAVIS:-false}; then
  23. git_revision=${TRAVIS_COMMIT::7}
  24. git_tag=${TRAVIS_TAG}
  25. elif is_git; then
  26. git_revision=$(git rev-parse --short HEAD)
  27. git_tag=$(git tag --contains HEAD)
  28. else
  29. git_revision=unknown
  30. git_tag=
  31. fi
  32. if [[ -n $git_tag ]]; then
  33. new_version=${version/-*}
  34. sed -i -e "s@$version@$new_version@" $version_file
  35. version=$new_version
  36. trap "git checkout -- $version_file" EXIT
  37. fi
  38. # Available environments
  39. list_envs() {
  40. grep -E '^\[env:' platformio.ini | sed 's/\[env:\(.*\)\]/\1/g'
  41. }
  42. available=$(list_envs | grep -Ev -- '-ota$|-ssl$|-secure-client.*$|^esp8266-.*base$' | sort)
  43. # Functions
  44. print_available() {
  45. echo "--------------------------------------------------------------"
  46. echo "Available environments:"
  47. for environment in $available; do
  48. echo "* $environment"
  49. done
  50. }
  51. print_environments() {
  52. echo "--------------------------------------------------------------"
  53. echo "Current environments:"
  54. for environment in $environments; do
  55. echo "* $environment"
  56. done
  57. }
  58. set_default_environments() {
  59. # Fallback to all available environments
  60. environments=$available
  61. }
  62. build_webui() {
  63. # Build system uses gulpscript.js to build web interface
  64. if [ ! -e node_modules/gulp/bin/gulp.js ]; then
  65. echo "--------------------------------------------------------------"
  66. echo "Installing dependencies..."
  67. npm install --only=dev
  68. fi
  69. # Recreate web interface (espurna/data/index.html.*.gz.h)
  70. echo "--------------------------------------------------------------"
  71. echo "Building web interface..."
  72. node node_modules/gulp/bin/gulp.js || exit
  73. # TODO: do something if webui files are different
  74. # for now, just print in travis log
  75. if ${TRAVIS:-false}; then
  76. git --no-pager diff --stat
  77. fi
  78. }
  79. build_release() {
  80. echo "--------------------------------------------------------------"
  81. echo "Building release images..."
  82. python scripts/generate_release_sh.py \
  83. --ignore secure-client \
  84. --version $version \
  85. --destination $destination/espurna-$version > release.sh
  86. bash release.sh
  87. echo "--------------------------------------------------------------"
  88. }
  89. build_environments() {
  90. echo "--------------------------------------------------------------"
  91. echo "Building firmware images..."
  92. mkdir -p $destination/espurna-$version
  93. for environment in $environments; do
  94. echo "* espurna-$version-$environment.bin"
  95. platformio run --silent --environment $environment || exit 1
  96. echo -n "SIZE: "
  97. stat_bytes .pio/build/$environment/firmware.bin
  98. [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Test" ]] || \
  99. mv .pio/build/$environment/firmware.bin $destination/espurna-$version/espurna-$version-$environment.bin
  100. done
  101. echo "--------------------------------------------------------------"
  102. }
  103. # Parameters
  104. print_getopts_help() {
  105. cat <<EOF
  106. Usage: $(basename "$0") [OPTION] <ENVIRONMENT>...
  107. Where ENVIRONMENT is environment name(s) from platformio.ini
  108. Options:
  109. -f VALUE Filter build stage by name to skip it
  110. Supported VALUEs are "environments" and "webui"
  111. Can be specified multiple times.
  112. -r Release mode
  113. Generate build list through an external script.
  114. -l Print available environments
  115. -d VALUE Destination to move .bin files after building environments
  116. -h Display this message
  117. EOF
  118. }
  119. while getopts "f:lrpd:h" opt; do
  120. case $opt in
  121. f)
  122. case "$OPTARG" in
  123. webui) script_build_webui=false ;;
  124. environments) script_build_environments=false ;;
  125. esac
  126. ;;
  127. l)
  128. print_available
  129. exit
  130. ;;
  131. d)
  132. destination=$OPTARG
  133. ;;
  134. r)
  135. release_mode=true
  136. ;;
  137. h)
  138. print_getopts_help
  139. exit
  140. esac
  141. done
  142. shift $((OPTIND-1))
  143. # Welcome
  144. echo "--------------------------------------------------------------"
  145. echo "ESPURNA FIRMWARE BUILDER"
  146. echo "Building for version ${version}" ${git_revision:+($git_revision)}
  147. # Environments to build
  148. environments=$@
  149. if $script_build_webui ; then
  150. build_webui
  151. fi
  152. if $script_build_environments ; then
  153. if [ $# -eq 0 ]; then
  154. set_default_environments
  155. fi
  156. if $release_mode ; then
  157. build_release
  158. else
  159. build_environments
  160. fi
  161. fi