Files
PX4-Autopilot/Tools/astyle/check_code_style_all.sh
T
Matthias Grob 3d820c5e80 build(astyle): pin astyle 3.1 via astyle-py
Distros ship newer astyle versions (Ubuntu 26.04: 3.6) which are
incompatible with our options and format differently, so make
check_format failed out of the box and required manually installing
an old .deb. astyle-py bundles astyle 3.1 as WebAssembly and installs
via pip on any OS, giving byte-identical results to native 3.1.

Native astyle 2.06-3.1 is still accepted as fallback, so existing
setups and the current CI container keep working.

Files are now formatted in batches on a temporary copy instead of one
astyle call per file, which keeps make check_format fast despite the
interpreter startup.
2026-11-18 08:23:56 -08:00

61 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -eu
DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
# fail early if no usable astyle is installed
"${DIR}/fix_code_style.sh"
CI="${CI:-false}"
DIFF_ONLY=false
for arg in "$@"; do
case "$arg" in
--fix) export PX4_ASTYLE_FIX=1 ;;
--diff-only) DIFF_ONLY=true ;;
esac
done
# install git pre-commit hook
HOOK_FILE="$DIR/../../.git/hooks/pre-commit"
if [[ ! -f "$HOOK_FILE" && "$CI" != "true" && $- == *i* ]]; then
echo ""
echo -e "\033[31mNinja tip: add a git pre-commit hook to automatically check code style\033[0m"
echo -e "Would you like to install one now? (\033[94mcp ./Tools/astyle/pre-commit .git/hooks/pre-commit\033[0m): [y/\033[1mN\033[0m]"
read user_cmd
if [ "$user_cmd" == "y" ]; then
echo -e "copying ./Tools/astyle/pre-commit -> .git/hooks/pre-commit"
mkdir -p $DIR/../.git/hooks
cp $DIR/pre-commit $HOOK_FILE
echo -e "\033[94mGreat, hook installed!\033[0m (checking style now)"
else
echo -e "\033[94mOk, I will remind you again later!\033[0m (checking style now)"
fi
fi
FILE_LIST=$(${DIR}/files_to_check_code_style.sh)
if [ "$DIFF_ONLY" = true ]; then
# --diff-filter=d: do not list deleted files (no need to format)
CHANGED=$(git -C "$DIR/../.." diff --name-only --diff-filter=d HEAD -- '*.c' '*.h' '*.cpp' '*.hpp')
if [ -z "$CHANGED" ]; then
FILE_LIST=""
else
FILE_LIST=$(echo "$FILE_LIST" | grep -Fx -f <(echo "$CHANGED") || true)
fi
fi
if [ -z "$FILE_LIST" ]; then
echo "No files to check"
exit 0
fi
echo "$FILE_LIST" | xargs -P 8 -n 100 "${DIR}/check_code_style.sh"
if [ $? -eq 0 ]; then
echo "Format checks passed"
exit 0
fi