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.

184 lines
6.4 KiB

  1. #!/usr/bin/env bash
  2. set -eEuo pipefail
  3. umask 022
  4. sinfo() { echo "$@" >&2 ; }
  5. shead() { sinfo "" ; sinfo "---------------------------------" ; sinfo "-- $@" ; sinfo "---------------------------------" ; }
  6. havecmd() { command command type "${1}" >/dev/null 2>&1 || return 1 ; }
  7. this_script="$(realpath "${BASH_SOURCE[0]}")"
  8. script_dir="$(realpath "$(dirname "$this_script")")"
  9. qmk_firmware_dir="$(realpath "$script_dir/../")"
  10. declare -A file_hashes
  11. export PATH="$PATH:$script_dir/fmpp/bin"
  12. build_fmpp() {
  13. [ -f "$script_dir/fmpp.tar.gz" ] \
  14. || wget -O"$script_dir/fmpp.tar.gz" https://github.com/freemarker/fmpp/archive/v0.9.16.tar.gz
  15. [ -d "$script_dir/fmpp" ] \
  16. || { mkdir "$script_dir/fmpp" && tar xf "$script_dir/fmpp.tar.gz" -C "$script_dir/fmpp" --strip-components=1 ; }
  17. pushd "$script_dir/fmpp" >/dev/null 2>&1
  18. sed -e "s#bootclasspath.path=.*#bootclasspath.path=$(find /usr/lib/jvm -name 'rt.jar' | sort | tail -n1)#g" \
  19. -e "s#ant.jar.path=.*#ant.jar.path=$(find /usr/share/java -name 'ant-1*.jar' | sort | tail -n1)#g" \
  20. build.properties.sample > build.properties
  21. sed -e 's#source="1.5"#source="1.8"#g' \
  22. -e 's#target="1.5"#target="1.8"#g' \
  23. build.xml > build.xml.new
  24. mv build.xml.new build.xml
  25. ant clean
  26. ant
  27. chmod +x "$script_dir/fmpp/bin/fmpp"
  28. popd >/dev/null 2>&1
  29. }
  30. find_chibi_files() {
  31. local search_path="$1"
  32. shift
  33. local conditions=( "$@" )
  34. for file in $(find -L "$search_path" -not -path '*/lib/chibios*' -and -not -path '*/lib/ugfx*' -and -not -path '*/util/*' -and \( "${conditions[@]}" \) | sort) ; do
  35. if [ -z "$(grep 'include_next' "$file")" ] ; then
  36. echo $file
  37. fi
  38. done
  39. }
  40. revert_chibi_files() {
  41. local search_path="$1"
  42. shead "Reverting ChibiOS config/board files..."
  43. for file in $(find_chibi_files "$search_path" -name chconf.h -or -name halconf.h -or -name mcuconf.h -or -name board.c -or -name board.h -or -name board.mk -or -name board.chcfg) ; do
  44. pushd "$search_path" >/dev/null 2>&1
  45. local relpath=$(realpath --relative-to="$search_path" "$file")
  46. git checkout upstream/develop -- "$relpath" || git checkout origin/develop -- "$relpath" || true
  47. popd >/dev/null 2>&1
  48. done
  49. }
  50. populate_file_hashes() {
  51. local search_path="$1"
  52. shead "Determining duplicate config/board files..."
  53. for file in $(find_chibi_files "$search_path" -name chconf.h -or -name halconf.h -or -name mcuconf.h -or -name board.c -or -name board.h) ; do
  54. local key="file_$(clang-format "$file" | sha1sum | cut -d' ' -f1)"
  55. local relpath=$(realpath --relative-to="$search_path" "$file")
  56. file_hashes[$key]="${file_hashes[$key]:-} $relpath"
  57. done
  58. for file in $(find_chibi_files "$search_path" -name board.mk -or -name board.chcfg) ; do
  59. local key="file_$(cat "$file" | sha1sum | cut -d' ' -f1)"
  60. local relpath=$(realpath --relative-to="$search_path" "$file")
  61. file_hashes[$key]="${file_hashes[$key]:-} $relpath"
  62. done
  63. }
  64. determine_equivalent_files() {
  65. local search_file="$1"
  66. for K in "${!file_hashes[@]}"; do
  67. for V in ${file_hashes[$K]}; do
  68. if [[ "$V" == "$search_file" ]] ; then
  69. for V in ${file_hashes[$K]}; do
  70. echo "$V"
  71. done
  72. return 0
  73. fi
  74. done
  75. done
  76. return 1
  77. }
  78. deploy_staged_files() {
  79. shead "Deploying staged files..."
  80. for file in $(find "$qmk_firmware_dir/util/chibios-upgrade-staging" -type f) ; do
  81. local relpath=$(realpath --relative-to="$qmk_firmware_dir/util/chibios-upgrade-staging" "$file")
  82. sinfo "Deploying staged file: $relpath"
  83. for other in $(determine_equivalent_files "$relpath") ; do
  84. sinfo " => $other"
  85. cp "$qmk_firmware_dir/util/chibios-upgrade-staging/$relpath" "$qmk_firmware_dir/$other"
  86. done
  87. done
  88. }
  89. swap_mcuconf_f3xx_f303() {
  90. shead "Swapping STM32F3xx_MCUCONF -> STM32F303_MCUCONF..."
  91. for file in $(find_chibi_files "$qmk_firmware_dir" -name mcuconf.h) ; do
  92. sed -i 's#STM32F3xx_MCUCONF#STM32F303_MCUCONF#g' "$file"
  93. dos2unix "$file" >/dev/null 2>&1
  94. done
  95. }
  96. upgrade_conf_files_generic() {
  97. local search_filename="$1"
  98. local update_script="$2"
  99. shead "Updating $search_filename files ($update_script)..."
  100. pushd "$qmk_firmware_dir/lib/chibios/tools/updater" >/dev/null 2>&1
  101. for file in $(find_chibi_files "$qmk_firmware_dir" -name "$search_filename") ; do
  102. cp -f "$file" "$file.orig"
  103. clang-format --style='{IndentPPDirectives: None}' -i "$file"
  104. cp -f "$file" "$file.formatted"
  105. bash "$update_script" "$file"
  106. if ! diff "$file" "$file.formatted" >/dev/null 2>&1 ; then
  107. dos2unix "$file" >/dev/null 2>&1
  108. else
  109. cp -f "$file.orig" "$file"
  110. fi
  111. rm -f "$file.orig" "$file.formatted"
  112. done
  113. popd >/dev/null 2>&1
  114. }
  115. upgrade_chconf_files() {
  116. upgrade_conf_files_generic chconf.h update_chconf_rt.sh
  117. }
  118. upgrade_halconf_files() {
  119. upgrade_conf_files_generic halconf.h update_halconf.sh
  120. OIFS=$IFS
  121. IFS=$'\n'
  122. for file in $(find_chibi_files "$qmk_firmware_dir" -name halconf.h) ; do
  123. echo $file
  124. sed -i 's@#include "mcuconf.h"@#include <mcuconf.h>@g' "$file"
  125. done
  126. IFS=$OIFS
  127. }
  128. upgrade_mcuconf_files() {
  129. pushd "$qmk_firmware_dir/lib/chibios/tools/updater" >/dev/null 2>&1
  130. for f in $(find . -name 'update_mcuconf*') ; do
  131. upgrade_conf_files_generic mcuconf.h $f
  132. done
  133. popd >/dev/null 2>&1
  134. }
  135. update_staged_files() {
  136. shead "Updating staged files with ChibiOS upgraded versions..."
  137. for file in $(find "$qmk_firmware_dir/util/chibios-upgrade-staging" -type f) ; do
  138. local relpath=$(realpath --relative-to="$qmk_firmware_dir/util/chibios-upgrade-staging" "$file")
  139. sinfo "Updating staged file: $relpath"
  140. cp "$qmk_firmware_dir/$relpath" "$qmk_firmware_dir/util/chibios-upgrade-staging/$relpath"
  141. done
  142. }
  143. havecmd fmpp || build_fmpp
  144. revert_chibi_files "$qmk_firmware_dir"
  145. populate_file_hashes "$qmk_firmware_dir"
  146. shead "Showing duplicate ChibiOS files..."
  147. for K in "${!file_hashes[@]}"; do
  148. sinfo ${K#file_}:
  149. for V in ${file_hashes[$K]}; do
  150. sinfo " $V"
  151. done
  152. done
  153. if [ "${1:-}" == "-r" ] ; then
  154. exit 0
  155. fi
  156. swap_mcuconf_f3xx_f303
  157. deploy_staged_files
  158. upgrade_mcuconf_files
  159. upgrade_chconf_files
  160. upgrade_halconf_files
  161. update_staged_files