diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml index a7fdb067..f5fb8f17 100644 --- a/.github/workflows/clang-format.yml +++ b/.github/workflows/clang-format.yml @@ -7,6 +7,7 @@ on: - 'Samples/Common/Sources/CrashTriggers/**' - '.github/workflows/clang-format.yml' - '.clang-format-ignore' + - 'Makefile' jobs: formatting-check: @@ -27,8 +28,7 @@ jobs: - name: Check formatting id: check_format run: | - find $DIRS -name '*.c' -o -name '*.cpp' -o -name '*.h' -o -name '*.m' -o -name '*.mm' | \ - xargs -r clang-format-18 -style=file -n -Werror 2> clang_format_errors.log + make check-format 2>clang_format_errors.log - name: Suggest formatting fixes if: failure() diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..6638d805 --- /dev/null +++ b/Makefile @@ -0,0 +1,33 @@ +# Directories to search +SEARCH_DIRS = Sources Tests Samples/Common/Sources/CrashTriggers + +# File extensions to format +FILE_EXTENSIONS = c cpp h m mm + +# Check for clang-format-18 first, then fall back to clang-format +CLANG_FORMAT := $(shell command -v clang-format-18 2> /dev/null || command -v clang-format 2> /dev/null) + +# Define the default target +.PHONY: format check-format + +all: format + +format: +ifeq ($(CLANG_FORMAT),) + @echo "Error: clang-format or clang-format-18 is not installed. Please install it and try again." + @exit 1 +else + @echo "Using $(CLANG_FORMAT)" + find $(SEARCH_DIRS) $(foreach ext,$(FILE_EXTENSIONS),-name '*.$(ext)' -o) -false | \ + xargs -r $(CLANG_FORMAT) -style=file -i +endif + +check-format: +ifeq ($(CLANG_FORMAT),) + @echo "Error: clang-format or clang-format-18 is not installed. Please install it and try again." + @exit 1 +else + @echo "Checking format using $(CLANG_FORMAT)" + @find $(SEARCH_DIRS) $(foreach ext,$(FILE_EXTENSIONS),-name '*.$(ext)' -o) -false | \ + xargs -r $(CLANG_FORMAT) -style=file -n -Werror +endif