2020-03-18 21:56:43 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2022-01-17 00:43:07 +00:00
|
|
|
FORMAT_VER="11"
|
2020-03-18 21:56:43 +00:00
|
|
|
FORMAT_OPTS="-i -style=file"
|
|
|
|
TIDY_OPTS="-p . --fix --fix-errors"
|
2022-06-07 04:34:28 +00:00
|
|
|
COMPILER_OPTS="-fno-builtin -fsyntax-only -std=gnu90 -Iinclude -Isrc -Ibuild -I. -D_LANGUAGE_C -DNON_MATCHING"
|
2020-03-18 21:56:43 +00:00
|
|
|
|
2022-01-17 00:43:07 +00:00
|
|
|
# https://backreference.org/2010/05/23/sanitizing-files-with-no-trailing-newline/index.html
|
|
|
|
# "gets the last character of the file pipes it into read, which will exit with
|
2022-01-23 23:09:02 +00:00
|
|
|
# a nonzero exit code if it encounters EOF before newline (so, if the last
|
2022-01-17 00:43:07 +00:00
|
|
|
# character of the file isn't a newline). If read exits nonzero, then append a
|
2022-01-23 23:09:02 +00:00
|
|
|
# newline onto the file using echo (if read exits 0, that satisfies the ||, so
|
2022-01-17 00:43:07 +00:00
|
|
|
# the echo command isn't run)." (https://stackoverflow.com/a/34865616)
|
|
|
|
function add_final_newline () {
|
|
|
|
for file in "$@"
|
|
|
|
do
|
|
|
|
tail -c1 $file | read -r _ || echo >> $file
|
|
|
|
done
|
|
|
|
}
|
|
|
|
export -f add_final_newline
|
|
|
|
|
2020-03-18 21:56:43 +00:00
|
|
|
shopt -s globstar
|
|
|
|
|
2022-04-09 00:20:23 +00:00
|
|
|
if [ $(command -v clang-format-${FORMAT_VER}) ]
|
2022-01-17 00:43:07 +00:00
|
|
|
then
|
2022-04-09 00:20:23 +00:00
|
|
|
CLANG_FORMAT="clang-format-${FORMAT_VER}"
|
2022-01-17 00:43:07 +00:00
|
|
|
else
|
2022-04-09 00:20:23 +00:00
|
|
|
if [ $(command -v clang-format) ]
|
2022-01-17 00:43:07 +00:00
|
|
|
then
|
2022-04-09 00:20:23 +00:00
|
|
|
CLANG_FORMAT="clang-format"
|
2022-01-17 00:43:07 +00:00
|
|
|
else
|
|
|
|
echo "Neither clang-format nor clang-format-${FORMAT_VER} found. Exiting."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-06-07 04:34:28 +00:00
|
|
|
if [ ! $(command -v clang-tidy) ]
|
|
|
|
then
|
|
|
|
echo "clang-tidy not found. Exiting."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-03-18 21:56:43 +00:00
|
|
|
if (( $# > 0 )); then
|
|
|
|
echo "Formatting file(s) $*"
|
|
|
|
echo "Running clang-format..."
|
2022-01-17 00:43:07 +00:00
|
|
|
${CLANG_FORMAT} ${FORMAT_OPTS} "$@"
|
2020-03-18 21:56:43 +00:00
|
|
|
echo "Running clang-tidy..."
|
|
|
|
clang-tidy ${TIDY_OPTS} "$@" -- ${COMPILER_OPTS} &> /dev/null
|
|
|
|
echo "Adding missing final new lines..."
|
2022-01-17 00:43:07 +00:00
|
|
|
add_final_newline "$@"
|
2020-03-18 21:56:43 +00:00
|
|
|
echo "Done formatting file(s) $*"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Formatting C files. This will take a bit"
|
|
|
|
echo "Running clang-format..."
|
2022-01-17 00:43:07 +00:00
|
|
|
${CLANG_FORMAT} ${FORMAT_OPTS} src/**/*.c
|
2020-03-18 21:56:43 +00:00
|
|
|
echo "Running clang-tidy..."
|
|
|
|
clang-tidy ${TIDY_OPTS} src/**/*.c -- ${COMPILER_OPTS} &> /dev/null
|
|
|
|
echo "Adding missing final new lines..."
|
2022-01-17 00:43:07 +00:00
|
|
|
find src/ -type f -name "*.c" -exec bash -c 'add_final_newline "$@"' bash {} +
|
|
|
|
find assets/xml/ -type f -name "*.xml" -exec bash -c 'add_final_newline "$@"' bash {} +
|
2021-05-16 16:36:40 +00:00
|
|
|
echo "Done formatting all files."
|