chibios-upgrader.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/bin/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. find -L "$search_path" -not -path '*/lib/chibios*' -and -not -path '*/lib/ugfx*' -and -not -path '*/util/*' -and \( "${conditions[@]}" \) | sort
  35. }
  36. revert_chibi_files() {
  37. local search_path="$1"
  38. shead "Reverting ChibiOS config/board files..."
  39. 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
  40. pushd "$search_path" >/dev/null 2>&1
  41. local relpath=$(realpath --relative-to="$search_path" "$file")
  42. git checkout upstream/master -- "$relpath" || git checkout origin/master -- "$relpath" || true
  43. popd >/dev/null 2>&1
  44. done
  45. }
  46. populate_file_hashes() {
  47. local search_path="$1"
  48. shead "Determining duplicate config/board files..."
  49. 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
  50. local key="file_$(clang-format "$file" | sha1sum | cut -d' ' -f1)"
  51. local relpath=$(realpath --relative-to="$search_path" "$file")
  52. file_hashes[$key]="${file_hashes[$key]:-} $relpath"
  53. done
  54. for file in $(find_chibi_files "$search_path" -name board.mk -or -name board.chcfg) ; do
  55. local key="file_$(cat "$file" | sha1sum | cut -d' ' -f1)"
  56. local relpath=$(realpath --relative-to="$search_path" "$file")
  57. file_hashes[$key]="${file_hashes[$key]:-} $relpath"
  58. done
  59. }
  60. determine_equivalent_files() {
  61. local search_file="$1"
  62. for K in "${!file_hashes[@]}"; do
  63. for V in ${file_hashes[$K]}; do
  64. if [[ "$V" == "$search_file" ]] ; then
  65. for V in ${file_hashes[$K]}; do
  66. echo "$V"
  67. done
  68. return 0
  69. fi
  70. done
  71. done
  72. return 1
  73. }
  74. deploy_staged_files() {
  75. shead "Deploying staged files..."
  76. for file in $(find "$qmk_firmware_dir/util/chibios-upgrade-staging" -type f) ; do
  77. local relpath=$(realpath --relative-to="$qmk_firmware_dir/util/chibios-upgrade-staging" "$file")
  78. sinfo "Deploying staged file: $relpath"
  79. for other in $(determine_equivalent_files "$relpath") ; do
  80. sinfo " => $other"
  81. cp "$qmk_firmware_dir/util/chibios-upgrade-staging/$relpath" "$qmk_firmware_dir/$other"
  82. done
  83. done
  84. }
  85. swap_mcuconf_f3xx_f303() {
  86. shead "Swapping STM32F3xx_MCUCONF -> STM32F303_MCUCONF..."
  87. for file in $(find_chibi_files "$qmk_firmware_dir" -name mcuconf.h) ; do
  88. sed -i 's#STM32F3xx_MCUCONF#STM32F303_MCUCONF#g' "$file"
  89. dos2unix "$file" >/dev/null 2>&1
  90. done
  91. }
  92. upgrade_conf_files_generic() {
  93. local search_filename="$1"
  94. local update_script="$2"
  95. shead "Updating $search_filename files ($update_script)..."
  96. pushd "$qmk_firmware_dir/lib/chibios/tools/updater" >/dev/null 2>&1
  97. for file in $(find_chibi_files "$qmk_firmware_dir" -name "$search_filename") ; do
  98. cp -f "$file" "$file.orig"
  99. clang-format --style='{IndentPPDirectives: None}' -i "$file"
  100. cp -f "$file" "$file.formatted"
  101. bash "$update_script" "$file"
  102. if ! diff "$file" "$file.formatted" >/dev/null 2>&1 ; then
  103. dos2unix "$file" >/dev/null 2>&1
  104. else
  105. cp -f "$file.orig" "$file"
  106. fi
  107. rm -f "$file.orig" "$file.formatted"
  108. done
  109. popd >/dev/null 2>&1
  110. }
  111. upgrade_chconf_files() {
  112. upgrade_conf_files_generic chconf.h update_chconf_rt.sh
  113. }
  114. upgrade_halconf_files() {
  115. upgrade_conf_files_generic halconf.h update_halconf.sh
  116. }
  117. upgrade_mcuconf_files() {
  118. pushd "$qmk_firmware_dir/lib/chibios/tools/updater" >/dev/null 2>&1
  119. for f in $(find . -name 'update_mcuconf*') ; do
  120. upgrade_conf_files_generic mcuconf.h $f
  121. done
  122. popd >/dev/null 2>&1
  123. }
  124. update_staged_files() {
  125. shead "Updating staged files with ChibiOS upgraded versions..."
  126. for file in $(find "$qmk_firmware_dir/util/chibios-upgrade-staging" -type f) ; do
  127. local relpath=$(realpath --relative-to="$qmk_firmware_dir/util/chibios-upgrade-staging" "$file")
  128. sinfo "Updating staged file: $relpath"
  129. cp "$qmk_firmware_dir/$relpath" "$qmk_firmware_dir/util/chibios-upgrade-staging/$relpath"
  130. done
  131. }
  132. havecmd fmpp || build_fmpp
  133. revert_chibi_files "$qmk_firmware_dir"
  134. populate_file_hashes "$qmk_firmware_dir"
  135. shead "Showing duplicate ChibiOS files..."
  136. for K in "${!file_hashes[@]}"; do
  137. sinfo ${K#file_}:
  138. for V in ${file_hashes[$K]}; do
  139. sinfo " $V"
  140. done
  141. done
  142. if [ "${1:-}" == "-r" ] ; then
  143. exit 0
  144. fi
  145. swap_mcuconf_f3xx_f303
  146. deploy_staged_files
  147. upgrade_mcuconf_files
  148. upgrade_chconf_files
  149. upgrade_halconf_files
  150. update_staged_files