diff --git a/Makefile b/Makefile index b1953c0..a1190b7 100644 --- a/Makefile +++ b/Makefile @@ -32,4 +32,9 @@ re: $(MAKE) -C $$ex re; \ done -.PHONY: all clean fclean re \ No newline at end of file +format: + for ex in $(EX); do \ + $(MAKE) -C $$ex format; \ + done + +.PHONY: all clean fclean re format \ No newline at end of file diff --git a/common.mk b/common.mk index 59b4c71..77a2044 100644 --- a/common.mk +++ b/common.mk @@ -16,6 +16,8 @@ SRC = $(notdir $(wildcard src/*.cpp)) OBJ = $(SRC:.cpp=.o) CC = c++ CFLAGS = -Wall -Wextra -Werror -std=c++11 +FORMATTER = clang-format +FORMAT_FILES = $(wildcard src/*.[ch] src/*.[ch]pp inc/*.[ch] inc/*.[ch]pp) all: $(NAME) @@ -36,4 +38,17 @@ fclean: clean re: fclean all -.PHONY: all clean fclean re run +format: + @if command -v $(FORMATTER) >/dev/null 2>&1; then \ + if [ -n "$(FORMAT_FILES)" ]; then \ + $(FORMATTER) -i --style=file $(FORMAT_FILES); \ + printf "Formatted %s\n" "$(NAME)"; \ + else \ + printf "No files to format in %s\n" "$(NAME)"; \ + fi; \ + else \ + printf "clang-format not found. Please install clang-format.\n"; \ + exit 1; \ + fi + +.PHONY: all clean fclean re run format