#! /bin/bash12# This file is part of t8code.3# t8code is a C library to manage a collection (a forest) of multiple4# connected adaptive space-trees of general element classes in parallel.5#6# Copyright (C) 2023 the developers7#8# t8code is free software; you can redistribute it and/or modify9# it under the terms of the GNU General Public License as published by10# the Free Software Foundation; either version 2 of the License, or11# (at your option) any later version.12#13# t8code is distributed in the hope that it will be useful,14# but WITHOUT ANY WARRANTY; without even the implied warranty of15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16# GNU General Public License for more details.17#18# You should have received a copy of the GNU General Public License19# along with t8code; if not, write to the Free Software Foundation, Inc.,20# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.2122# We use clang-format to indent our code. There are different base styles23# available, but we use a modification. The options for this modification24# are located in the .clang-format file.25# We have to use the -i argument, so that clang-format directly alters the26# files instead of printing the changes to stdout. The --style=file27# arguments tells clang-format to look for a *.clang-format file.28#29# If you call this script with "NO_CHANGE" as first argument it will run30# in dry-mode, not changing the file contents.31FORMAT_OPTIONS="--Werror -i --style=file"3233# Required version of the clang format program.34REQUIRED_VERSION_MAJOR="17"35REQUIRED_VERSION_MINOR="0"36REQUIRED_VERSION_STRING="${REQUIRED_VERSION_MAJOR}.${REQUIRED_VERSION_MINOR}"3738FORMAT=`which clang-format 2> /dev/null`3940# Check if ${FORMAT} exists41if [ -z "$FORMAT" ]42then43# Exit if the indentation script was not found44echo "ERROR: clang-format not found."45echo "Please install clang-format version ${REQUIRED_VERSION_STRING}."46echo "See https://github.com/ssciwr/clang-format-wheel"47exit 148fi4950# Get indentation script version and split it into major, minor, patch51CLANG_VERSION_STRING=`$FORMAT --version`5253[[ $CLANG_VERSION_STRING =~ version[[:space:]]+([^[:space:]]+) ]] &&54VERSION="${BASH_REMATCH[1]}"55MAJOR=`echo $VERSION | cut -d. -f1`56MINOR=`echo $VERSION | cut -d. -f2`57PATCH=`echo $VERSION | cut -d. -f3`5859# Check if version matches required version60if [[ "$MAJOR" != "$REQUIRED_VERSION_MAJOR" || $MINOR != "$REQUIRED_VERSION_MINOR" ]]; then61echo "Please install clang-format version $REQUIRED_VERSION_STRING"62exit 163fi6465#66# Parsing of input files and throwing out files to be ignored67#68# Read all lines from the IGNORE_FILE69# that are not empty and are not comments (i.e. start with '#').70# Determine base directory of git repo71GIT_REPO_PATH=$(git rev-parse --show-toplevel)7273IGNORE_FILE=${GIT_REPO_PATH}/scripts/t8indent_ignore.sh74files_to_ignore=()75while read line; do76if [[ ${line:0:1} != "#" ]] && [[ $line != "" ]]77then78files_to_ignore+=("$line")79fi80done <$IGNORE_FILE8182#83# Check if first argument is "NO_CHANGE", if so84# the file content is not changed.85#86OUTFILE_OPTION=87NO_CHANGE=FALSE88if [[ $1 == "NO_CHANGE" ]]89then90shift # Removes first argument from $@ list91NO_CHANGE=TRUE92FORMAT_OPTIONS="${FORMAT_OPTIONS} --dry-run"93fi949596# Iterate over all arguments and throw97# away those filenames that we should ignore.98# Also check if suffix is ".c" ".cxx" ".h" or ".hxx"99for arg in "$@"100do101FILE_SUFFIX="${arg##*.}"102if ! [ $FILE_SUFFIX = "c" -o $FILE_SUFFIX = "h" -o $FILE_SUFFIX = "cxx" -o $FILE_SUFFIX = "hxx" ]103then104echo "ERROR: File "$arg" does not have valid suffix (.c .h .cxx .hxx)."105exit 1106fi107108ignore_arg=0109# Iterate over each ignore filename110for ignore_file in "${files_to_ignore[@]}"111do112echo Checking "$arg" against "${GIT_REPO_PATH}/$ignore_file"113if [[ "$arg" -ef "${GIT_REPO_PATH}/$ignore_file" ]]114then115# arg matches and will be ignored116echo The file \"$arg\" will be ignored by indentation as specified in \"$IGNORE_FILE\".117ignore_arg=1118fi119done120# Now add all non-ignored files to a new argument array121if [[ $ignore_arg == 0 ]]122then123newargs+=("$arg")124fi125done126127#128# Do the actual indentation.129# If NO_CHANGE was set, this does a dry run and we additionally print stderr to stdout.130#131if [[ $NO_CHANGE == "TRUE" ]]132then133$FORMAT $FORMAT_OPTIONS ${newargs[@]} 2>&1134status=$?135else136$FORMAT $FORMAT_OPTIONS ${newargs[@]}137status=$?138fi139140# If the file content was not changed, the return141# value determines whether or not the file was142# indented.143if [[ $NO_CHANGE == "TRUE" ]]144then145exit $status146fi147148149