Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Makefile with formatter helper #521

Merged
merged 3 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/clang-format.yml
bamx23 marked this conversation as resolved.
Show resolved Hide resolved
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