Skip to content

Commit

Permalink
Add Makefile with formatter helper (#521)
Browse files Browse the repository at this point in the history
* Add Makefile with format target

* Use Makefile in CI

* Trigger clang-format on Makefile changes
  • Loading branch information
bamx23 committed Jun 30, 2024
1 parent ae4d75c commit 8cc2170
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
- 'Samples/Common/Sources/CrashTriggers/**'
- '.github/workflows/clang-format.yml'
- '.clang-format-ignore'
- 'Makefile'

jobs:
formatting-check:
Expand All @@ -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()
Expand Down
33 changes: 33 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8cc2170

Please sign in to comment.