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.

166 lines
4.3 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 [ ! -z $git_tag ]; then
  30. version=${version/-dev}
  31. sed -i -e "s@$version-dev@$version@" $version_file
  32. trap "git checkout -- $version_file" EXIT
  33. fi
  34. par_build=false
  35. par_thread=${BUILDER_THREAD:-0}
  36. par_total_threads=${BUILDER_TOTAL_THREADS:-4}
  37. if [ ${par_thread} -ne ${par_thread} -o \
  38. ${par_total_threads} -ne ${par_total_threads} ]; then
  39. echo "Parallel threads should be a number."
  40. exit
  41. fi
  42. if [ ${par_thread} -ge ${par_total_threads} ]; then
  43. echo "Current thread is greater than total threads. Doesn't make sense"
  44. exit
  45. fi
  46. # Available environments
  47. list_envs() {
  48. grep env: platformio.ini | sed 's/\[env:\(.*\)\]/\1/g'
  49. }
  50. travis=$(list_envs | grep travis | sort)
  51. available=$(list_envs | grep -Ev -- '-ota$|-ssl$|^travis' | sort)
  52. # Build tools settings
  53. export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -DAPP_REVISION='\"$git_revision\"'"
  54. # Functions
  55. print_available() {
  56. echo "--------------------------------------------------------------"
  57. echo "Available environments:"
  58. for environment in $available; do
  59. echo "* $environment"
  60. done
  61. }
  62. print_environments() {
  63. echo "--------------------------------------------------------------"
  64. echo "Current environments:"
  65. for environment in $environments; do
  66. echo "* $environment"
  67. done
  68. }
  69. set_default_environments() {
  70. # Hook to build in parallel when using travis
  71. if [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Release" ]] && ${par_build}; then
  72. environments=$(echo ${available} | \
  73. awk -v par_thread=${par_thread} -v par_total_threads=${par_total_threads} \
  74. '{ for (i = 1; i <= NF; i++) if (++j % par_total_threads == par_thread ) print $i; }')
  75. return
  76. fi
  77. # Only build travisN
  78. if [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Test" ]]; then
  79. environments=$travis
  80. return
  81. fi
  82. # Fallback to all available environments
  83. environments=$available
  84. }
  85. build_webui() {
  86. # Build system uses gulpscript.js to build web interface
  87. if [ ! -e node_modules/gulp/bin/gulp.js ]; then
  88. echo "--------------------------------------------------------------"
  89. echo "Installing dependencies..."
  90. npm install --only=dev
  91. fi
  92. # Recreate web interface (espurna/data/index.html.*.gz.h)
  93. echo "--------------------------------------------------------------"
  94. echo "Building web interface..."
  95. node node_modules/gulp/bin/gulp.js || exit
  96. }
  97. build_environments() {
  98. echo "--------------------------------------------------------------"
  99. echo "Building firmware images..."
  100. mkdir -p ../firmware/espurna-$version
  101. for environment in $environments; do
  102. echo -n "* espurna-$version-$environment.bin --- "
  103. platformio run --silent --environment $environment || exit 1
  104. stat_bytes .pioenvs/$environment/firmware.bin
  105. [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Test" ]] || \
  106. mv .pioenvs/$environment/firmware.bin $destination/espurna-$version/espurna-$version-$environment.bin
  107. done
  108. echo "--------------------------------------------------------------"
  109. }
  110. # Parameters
  111. while getopts "lpd:" opt; do
  112. case $opt in
  113. l)
  114. print_available
  115. exit
  116. ;;
  117. p)
  118. par_build=true
  119. ;;
  120. d)
  121. destination=$OPTARG
  122. ;;
  123. esac
  124. done
  125. shift $((OPTIND-1))
  126. # Welcome
  127. echo "--------------------------------------------------------------"
  128. echo "ESPURNA FIRMWARE BUILDER"
  129. echo "Building for version ${version}" ${git_revision:+($git_revision)}
  130. # Environments to build
  131. environments=$@
  132. if [ $# -eq 0 ]; then
  133. set_default_environments
  134. fi
  135. if ${CI:-false}; then
  136. print_environments
  137. fi
  138. build_webui
  139. build_environments