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.

190 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 ${CI:-false}; then
  23. git_revision=${GITHUB_SHA::8}
  24. git_tag=$(echo -n $GITHUB_REF | grep '^refs/tags/' | cut -d '/' -f 2)
  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. if ${CI:-false}; then
  75. git --no-pager diff --stat
  76. fi
  77. }
  78. build_release() {
  79. echo "--------------------------------------------------------------"
  80. echo "Building release images..."
  81. python scripts/generate_release_sh.py \
  82. --ignore secure-client \
  83. --version $version \
  84. --destination $destination/espurna-$version > release.sh
  85. bash release.sh
  86. echo "--------------------------------------------------------------"
  87. }
  88. build_environments() {
  89. echo "--------------------------------------------------------------"
  90. echo "Building firmware images..."
  91. mkdir -p $destination/espurna-$version
  92. for environment in $environments; do
  93. echo "* espurna-$version-$environment.bin"
  94. platformio run --silent --environment $environment || exit 1
  95. echo -n "SIZE: "
  96. stat_bytes .pio/build/$environment/firmware.bin
  97. mv .pio/build/$environment/firmware.bin $destination/espurna-$version/espurna-$version-$environment.bin
  98. done
  99. echo "--------------------------------------------------------------"
  100. }
  101. # Parameters
  102. print_getopts_help() {
  103. cat <<EOF
  104. Usage: $(basename "$0") [OPTION] <ENVIRONMENT>...
  105. Where ENVIRONMENT is environment name(s) from platformio.ini
  106. Options:
  107. -f VALUE Filter build stage by name to skip it
  108. Supported VALUEs are "environments" and "webui"
  109. Can be specified multiple times.
  110. -r Release mode
  111. Generate build list through an external script.
  112. -l Print available environments
  113. -d VALUE Destination to move .bin files after building environments
  114. -h Display this message
  115. EOF
  116. }
  117. while getopts "f:lrpd:h" opt; do
  118. case $opt in
  119. f)
  120. case "$OPTARG" in
  121. webui) script_build_webui=false ;;
  122. environments) script_build_environments=false ;;
  123. esac
  124. ;;
  125. l)
  126. print_available
  127. exit
  128. ;;
  129. d)
  130. destination=$OPTARG
  131. ;;
  132. r)
  133. release_mode=true
  134. ;;
  135. h)
  136. print_getopts_help
  137. exit
  138. esac
  139. done
  140. shift $((OPTIND-1))
  141. # Welcome
  142. echo "--------------------------------------------------------------"
  143. echo "ESPURNA FIRMWARE BUILDER"
  144. echo "Building for version ${version}" ${git_revision:+($git_revision)}
  145. # Environments to build
  146. environments=$@
  147. if $script_build_webui ; then
  148. build_webui
  149. fi
  150. if $script_build_environments ; then
  151. if [ $# -eq 0 ]; then
  152. set_default_environments
  153. fi
  154. if $release_mode ; then
  155. build_release
  156. else
  157. build_environments
  158. fi
  159. fi