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.
This commit is contained in:
Matthias Grob committed 2026-11-18 14:08:39 +01:00
1 parent 95c82e84ea
commit 6e7f78bc70
9 files changed
+83 -67

No files matched your search

-4
View File
@@ -10,9 +10,5 @@ keep-one-line-blocks
keep-one-line-statements
align-pointer=name
align-reference=name
-n #--suffix=none
ignore-exclude-errors-x
lineend=linux
exclude=EASTL
add-brackets
max-code-length=140
+39 -19
View File
@@ -1,22 +1,42 @@
#!/usr/bin/env bash
# Check the formatting of the given files, print a diff for each badly formatted
# one and fail. With PX4_ASTYLE_FIX=1 the files get fixed instead.
#
# All files are formatted in one batch on a temporary copy to keep the
# formatter startup cost independent of the number of files.
FILE=$1
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
CHECK_FAILED=$(${DIR}/fix_code_style.sh --dry-run --formatted $FILE)
if [ -n "$CHECK_FAILED" ]; then
${DIR}/fix_code_style.sh --quiet < $FILE > $FILE.pretty
echo -e 'Formatting issue found in' $FILE
echo
git --no-pager diff --no-index --minimal --histogram --color=always $FILE $FILE.pretty | grep -vE -e "^.{,4}diff.*\.pretty.{,3}$" -e "^.{,4}--- a/.*$" -e "^.{,4}\+\+\+ b/.*$" -e "^.{,5}@@ .* @@.*$" -e "^.{,4}index .{10}\.\."
rm -f $FILE.pretty
echo
if [[ $PX4_ASTYLE_FIX -eq 1 ]]; then
${DIR}/fix_code_style.sh $FILE
else
echo 'to fix automatically run "make format", "make format_changed" or "./Tools/astyle/fix_code_style.sh' $FILE'"'
exit 1
fi
if [[ $# -eq 0 ]]; then
exit 0
fi
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
TMP_DIR=$(mktemp -d)
trap 'rm -rf "$TMP_DIR"' EXIT
FORMATTED=()
for FILE in "$@"; do
mkdir -p "$TMP_DIR/$(dirname "$FILE")"
cp "$FILE" "$TMP_DIR/$FILE"
FORMATTED+=("$TMP_DIR/$FILE")
done
"${DIR}/fix_code_style.sh" "${FORMATTED[@]}" || exit 1
CHECK_FAILED=0
for FILE in "$@"; do
if ! cmp -s "$FILE" "$TMP_DIR/$FILE"; then
echo -e 'Formatting issue found in' "$FILE"
echo
git --no-pager diff --no-index --minimal --histogram --color=always "$FILE" "$TMP_DIR/$FILE" | tail -n +5
echo
if [[ $PX4_ASTYLE_FIX -eq 1 ]]; then
cp "$TMP_DIR/$FILE" "$FILE"
else
echo 'to fix automatically run "make format", "make format_changed" or "./Tools/astyle/fix_code_style.sh' "$FILE"'"'
CHECK_FAILED=1
fi
fi
done
exit $CHECK_FAILED
+4 -32
View File
@@ -1,40 +1,12 @@
#!/usr/bin/env bash
set -eu
# Check for the latest astyle version
ASTYLE_VER_REQUIRED_1="Artistic Style Version 2.06"
ASTYLE_VER_REQUIRED_2="Artistic Style Version 3.0"
ASTYLE_VER_REQUIRED_3="Artistic Style Version 3.0.1"
ASTYLE_VER_REQUIRED_4="Artistic Style Version 3.1"
DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
astyle_ver() {
echo "PX4 requires at least ${ASTYLE_VER_REQUIRED_1}"
echo "You can get the correct version here: https://sourceforge.net/projects/astyle/files/astyle/astyle%202.06/"
}
# check if astyle is installed
condition=$(which astyle 2>/dev/null | grep -v "not found" | wc -l)
if [ $condition -eq 0 ]; then
echo "astyle is not installed"
astyle_ver
exit 1
else
ASTYLE_VER=`astyle --version`
if [ "$ASTYLE_VER" != "$ASTYLE_VER_REQUIRED_1" -a \
"$ASTYLE_VER" != "$ASTYLE_VER_REQUIRED_2" -a \
"$ASTYLE_VER" != "$ASTYLE_VER_REQUIRED_3" -a \
"$ASTYLE_VER" != "$ASTYLE_VER_REQUIRED_4" ]
then
echo "Error: you're using ${ASTYLE_VER}"
echo "but should be using ${ASTYLE_VER_REQUIRED_1}, ${ASTYLE_VER_REQUIRED_2}, ${ASTYLE_VER_REQUIRED_3}, or ${ASTYLE_VER_REQUIRED_4} instead"
exit 1
fi
fi
# fail early if no usable astyle is installed
"${DIR}/fix_code_style.sh"
CI="${CI:-false}"
DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
DIFF_ONLY=false
for arg in "$@"; do
@@ -80,7 +52,7 @@ if [ -z "$FILE_LIST" ]; then
exit 0
fi
echo "$FILE_LIST" | xargs -P 8 -I % ${DIR}/check_code_style.sh %
echo "$FILE_LIST" | xargs -P 8 -n 100 "${DIR}/check_code_style.sh"
if [ $? -eq 0 ]; then
echo "Format checks passed"
+38 -8
View File
@@ -1,11 +1,41 @@
#!/usr/bin/env bash
if [[ $# -eq 0 ]] ; then
exit 0
fi
# Format the given files in place with astyle 3.1 and the PX4 rules in astylerc.
#
# Prefers astyle-py (pip install astyle-py, see Tools/setup/requirements.txt)
# which bundles astyle 3.1 independent of the OS. Falls back to a natively
# installed astyle of a supported version.
#
# Without file arguments it only checks that a usable astyle is available.
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
astyle \
--options=$DIR/astylerc \
--preserve-date \
$*
ASTYLE_PY_VERSION="3.1"
ASTYLE_NATIVE_VERSIONS=("2.06" "3.0" "3.0.1" "3.1")
if command -v astyle_py >/dev/null 2>&1; then
ASTYLE=(astyle_py)
elif python3 -c "import astyle_py" >/dev/null 2>&1; then
ASTYLE=(python3 -m astyle_py)
fi
if [[ -n "${ASTYLE[*]}" ]]; then
ASTYLE+=("--astyle-version=${ASTYLE_PY_VERSION}" --options="${DIR}/astylerc" --quiet)
else
NATIVE_VERSION=$(astyle --version 2>/dev/null | sed -n 's/^Artistic Style Version //p')
for v in "${ASTYLE_NATIVE_VERSIONS[@]}"; do
if [[ "$NATIVE_VERSION" == "$v" ]]; then
ASTYLE=(astyle --options="${DIR}/astylerc" --suffix=none --lineend=linux --preserve-date --quiet)
fi
done
fi
if [[ -z "${ASTYLE[*]}" ]]; then
echo "Error: no usable astyle found${NATIVE_VERSION:+ (installed astyle ${NATIVE_VERSION} is not supported)}." >&2
echo "Install astyle 3.1 via: python3 -m pip install -r ${DIR}/../setup/requirements.txt" >&2
exit 1
fi
if [[ $# -eq 0 ]]; then
exit 0
fi
"${ASTYLE[@]}" "$@" >/dev/null
+1 -1
View File
@@ -54,7 +54,7 @@ if [ $? -ne 0 ]; then
fi
# Check for code style, only in changed files
bash -c "comm -12 <(./Tools/astyle/files_to_check_code_style.sh | sort) <(git diff --cached --name-only --diff-filter=ACM) | xargs -P 8 -I % ./Tools/astyle/check_code_style.sh %"
bash -c "comm -12 <(./Tools/astyle/files_to_check_code_style.sh | sort) <(git diff --cached --name-only --diff-filter=ACM) | xargs ./Tools/astyle/check_code_style.sh"
if [ $? -ne 0 ]
then
echo "Pre-commit style error: Bad formatting according to astyle rules"
-1
View File
@@ -44,7 +44,6 @@ echo
echo "Installing PX4 general dependencies"
sudo pacman -Sy --noconfirm --needed \
astyle \
base-devel \
clang \
cmake \
-1
View File
@@ -84,7 +84,6 @@ brew tap PX4/px4
# Package list. This replaces the px4-dev meta-formula, which is kept
# as a deprecated no-op upstream. See PX4/homebrew-px4 for history.
PX4_BREW_PACKAGES=(
astyle
bash-completion
ccache
cmake
+1
View File
@@ -1,4 +1,5 @@
argcomplete
astyle-py==1.1.0
cerberus
coverage
empy>=3.3,<4
-1
View File
@@ -79,7 +79,6 @@ echo "[ubuntu.sh] Installing PX4 general dependencies"
sudo apt-get update -y --quiet
sudo DEBIAN_FRONTEND=noninteractive apt-get -y --quiet --no-install-recommends install \
astyle \
build-essential \
ccache \
cmake \