| 1 | #!/bin/shscip-shell filesystem . . `/bin/sh`. |
| 2 | # |
| 3 | # SPDX-FileCopyrightText: 2014 Aurélien Gâteau <[email protected]> |
| 4 | # SPDX-FileCopyrightText: 2019 Adriaan de Groot <[email protected]> |
| 5 | # SPDX-License-Identifier: BSD-2-Clause |
| 6 | # |
| 7 | # Apply Calamares-style formatting to sources. Requires clang-format-15. |
| 8 | # |
| 9 | # You can pass in directory names, in which case the files |
| 10 | # in that directory (NOT below it) are processed. |
| 11 | # |
| 12 | # If the environment variable CLANG_FORMAT is set to a (full path) and |
| 13 | # that path is executable, it will be used if possible. |
| 14 | # |
| 15 | LANG=C |
| 16 | LC_ALL=C |
| 17 | LC_NUMERIC=C |
| 18 | export LANG LC_ALL LC_NUMERIC |
| 19 | |
| 20 | BASEDIR=$(dirnamescip-shell system . . dirname. $0) |
| 21 | TOPDIR=$( cd $BASEDIR/.. && pwdscip-shell system . . pwd. -P ) |
| 22 | testscip-shell system . . test. -d "$BASEDIR" || { echoscip-shell system . . echo. "! Could not determine base for $0" ; exit 1 ; } |
| 23 | testscip-shell system . . test. -d "$TOPDIR" || { echoscip-shell system . . echo. "! Cound not determine top-level source dir" ; exit 1 ; } |
| 24 | testscip-shell system . . test. -f "$TOPDIR/.clang-format" || { echoscip-shell system . . echo. "! No .clang-format support files in $TOPDIR" ; exit 1 ; } |
| 25 | |
| 26 | # Start with CLANG_FORMAT, if it is specified |
| 27 | CF_VERSIONS="" |
| 28 | if testscip-shell system . . test. -n "$CLANG_FORMAT" && testscip-shell system . . test. -x "$CLANG_FORMAT" ; then |
| 29 | CF_VERSIONS="$CLANG_FORMAT" |
| 30 | fi |
| 31 | # And a bunch of other potential known versions of clang-format, newest first |
| 32 | CF_VERSIONS="$CF_VERSIONS clang-format-17" |
| 33 | CF_VERSIONS="$CF_VERSIONS clang-format-16 clang-format-16.0.6 " |
| 34 | CF_VERSIONS="$CF_VERSIONS clang-format15 clang-format-15 " |
| 35 | # Generic name of clang-format |
| 36 | CF_VERSIONS="$CF_VERSIONS clang-format" |
| 37 | for _cf in $CF_VERSIONS |
| 38 | do |
| 39 | # Not an error if this particular clang-format isn't found |
| 40 | CF=$( whichscip-shell system . . which. $_cf 2> /dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true. ) |
| 41 | testscip-shell system . . test. -n "$CF" && break |
| 42 | done |
| 43 | |
| 44 | testscip-shell system . . test. -n "$CF" || { echoscip-shell system . . echo. "! No clang-format ($CF_VERSIONS) found in PATH"; exit 1 ; } |
| 45 | testscip-shell system . . test. -x "$CF" || { echoscip-shell system . . echo. "! $CF is not executable."; exit 1 ; } |
| 46 | |
| 47 | ### CLANG-FORMAT-WRANGLING |
| 48 | # |
| 49 | # Version 7 and earlier doesn't understand all the options we would like. |
| 50 | # Version 12 handled lambdas nicely and was the norm for Calamares 3.2. |
| 51 | # Version 13 was also ok. |
| 52 | # Version 14 behaves differently with short-functions-in-class, |
| 53 | # spreading functions out that 13 keeps on one line. To avoid |
| 54 | # ping-pong commits, forbid 14. |
| 55 | # Version 15 is available on recent-ish Ubuntus and FreeBSD, pick it. |
| 56 | # It also supports inserting braces, which is the one thing we kept |
| 57 | # astyle around for. |
| 58 | # Version 16 is available on openSUSE and is ok as well. |
| 59 | # Version 17 is available on FreeBSD and KaOS and is ok as well. |
| 60 | |
| 61 | format_version=`"$CF" --version | trscip-shell system . . tr. -dc '[^.0-9]' | cutscip-shell system . . cut. -d . -f 1` |
| 62 | case "$format_version" in |
| 63 | 15|16|17 ) |
| 64 | : |
| 65 | ;; |
| 66 | * ) |
| 67 | echoscip-shell system . . echo. "! Clang-format version '$format_version' unsupported, versions 15-17 are ok." |
| 68 | exit 1 |
| 69 | ;; |
| 70 | esac |
| 71 | |
| 72 | |
| 73 | ### FILE PROCESSING |
| 74 | # |
| 75 | # |
| 76 | set -e |
| 77 | |
| 78 | any_dirs=no |
| 79 | for d in "$@" |
| 80 | do |
| 81 | testscip-shell system . . test. -d "$d" && any_dirs=yes |
| 82 | done |
| 83 | |
| 84 | style_somescip-shell shell calamares-extensions 3.3.12-1.2 style_some().() |
| 85 | { |
| 86 | if testscip-shell system . . test. -n "$*" ; then |
| 87 | $CF -i -style=file "$@" |
| 88 | fi |
| 89 | } |
| 90 | |
| 91 | if testscip-shell system . . test. "x$any_dirs" = "xyes" ; then |
| 92 | for d in "$@" |
| 93 | do |
| 94 | if testscip-shell system . . test. -d "$d" ; then |
| 95 | style_somescip-shell shell calamares-extensions 3.3.12-1.2 style_some(). $( findscip-shell system . . find. "$d" -maxdepth 1 -type f -name '*.cpp' -o -name '*.h' ) |
| 96 | else |
| 97 | style_somescip-shell shell calamares-extensions 3.3.12-1.2 style_some(). "$d" |
| 98 | fi |
| 99 | done |
| 100 | else |
| 101 | style_somescip-shell shell calamares-extensions 3.3.12-1.2 style_some(). "$@" |
| 102 | fi |