From 6e7f78bc708d88bd6fb68ca0d6892a8ee83d60ef Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Wed, 18 Nov 2026 14:08:39 +0100 Subject: [PATCH] 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. --- Tools/astyle/astylerc | 4 -- Tools/astyle/check_code_style.sh | 58 +++++++++++++++++++--------- Tools/astyle/check_code_style_all.sh | 36 ++--------------- Tools/astyle/fix_code_style.sh | 46 ++++++++++++++++++---- Tools/astyle/pre-commit | 2 +- Tools/setup/arch.sh | 1 - Tools/setup/macos.sh | 1 - Tools/setup/requirements.txt | 1 + Tools/setup/ubuntu.sh | 1 - 9 files changed, 83 insertions(+), 67 deletions(-) diff --git a/Tools/astyle/astylerc b/Tools/astyle/astylerc index 3d1b437ef30..3ec7de132b9 100644 --- a/Tools/astyle/astylerc +++ b/Tools/astyle/astylerc @@ -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 diff --git a/Tools/astyle/check_code_style.sh b/Tools/astyle/check_code_style.sh index a25fe62e8b6..836e2d8baab 100755 --- a/Tools/astyle/check_code_style.sh +++ b/Tools/astyle/check_code_style.sh @@ -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 diff --git a/Tools/astyle/check_code_style_all.sh b/Tools/astyle/check_code_style_all.sh index 871ab622561..e8e11199d34 100755 --- a/Tools/astyle/check_code_style_all.sh +++ b/Tools/astyle/check_code_style_all.sh @@ -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" diff --git a/Tools/astyle/fix_code_style.sh b/Tools/astyle/fix_code_style.sh index af3a968e38a..8c5e390e549 100755 --- a/Tools/astyle/fix_code_style.sh +++ b/Tools/astyle/fix_code_style.sh @@ -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 diff --git a/Tools/astyle/pre-commit b/Tools/astyle/pre-commit index 6c9bb248e62..2172e3bdc42 100755 --- a/Tools/astyle/pre-commit +++ b/Tools/astyle/pre-commit @@ -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" diff --git a/Tools/setup/arch.sh b/Tools/setup/arch.sh index d72be01f45d..5d2eea59aca 100755 --- a/Tools/setup/arch.sh +++ b/Tools/setup/arch.sh @@ -44,7 +44,6 @@ echo echo "Installing PX4 general dependencies" sudo pacman -Sy --noconfirm --needed \ - astyle \ base-devel \ clang \ cmake \ diff --git a/Tools/setup/macos.sh b/Tools/setup/macos.sh index 092415a3f89..e5bef6df8b5 100755 --- a/Tools/setup/macos.sh +++ b/Tools/setup/macos.sh @@ -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 diff --git a/Tools/setup/requirements.txt b/Tools/setup/requirements.txt index 81a9c9facf0..94cc6c5a775 100644 --- a/Tools/setup/requirements.txt +++ b/Tools/setup/requirements.txt @@ -1,4 +1,5 @@ argcomplete +astyle-py==1.1.0 cerberus coverage empy>=3.3,<4 diff --git a/Tools/setup/ubuntu.sh b/Tools/setup/ubuntu.sh index a657f650e21..1d25b1094ab 100755 --- a/Tools/setup/ubuntu.sh +++ b/Tools/setup/ubuntu.sh @@ -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 \