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