Skip to content

Commit 26e1666

Browse files
committed
Add make format target
This simplifies the formatting before creating PR, just 'make format'.
1 parent 966e5c1 commit 26e1666

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,30 @@ quake: artifact $(quake_deps)
488488
endif
489489
endif
490490

491+
CLANG_FMT := $(shell which clang-format 2>/dev/null)
492+
SH_FMT := $(shell which shfmt 2>/dev/null)
493+
BLACK := $(shell which black 2>/dev/null)
494+
SUBMODULES := $(shell git config --file .gitmodules --get-regexp path | awk '{ print $$2 }')
495+
SUBMODULES_PRUNE_PATHS := $(shell for subm in $(SUBMODULES); do echo -n "-path \"./$$subm\" -o "; done | sed 's/ -o $$//')
496+
format:
497+
ifeq ($(CLANG_FMT),)
498+
$(error clang-format not found. Install clang-format version 18 or above and try again)
499+
else
500+
# Skip formatting submodules and everything in $(OUT), apply the same rule for shfmt and black
501+
$(Q)$(shell $(CLANG_FMT) -i $(shell find . \( $(SUBMODULES_PRUNE_PATHS) -o -path \"./$(OUT)\" \) -prune -o -name "*.[ch]" -print))
502+
endif
503+
ifeq ($(SH_FMT),)
504+
$(error shfmt not found. Install shfmt and try again)
505+
else
506+
$(Q)$(shell $(SH_FMT) -w $(shell find . \( $(SUBMODULES_PRUNE_PATHS) -o -path \"./$(OUT)\" \) -prune -o -name "*.sh" -print))
507+
endif
508+
ifeq ($(BLACK),)
509+
$(error black not found. Install black version 25.1.0 or above and try again)
510+
else
511+
$(Q)$(shell $(BLACK) --quiet $(shell find . \( $(SUBMODULES_PRUNE_PATHS) -o -path \"./$(OUT)\" \) -prune -o \( -name "*.py" -o -name "*.pyi" \) -print))
512+
endif
513+
$(Q)$(call notice,All files are properly formatted.)
514+
491515
clean:
492516
$(VECHO) "Cleaning... "
493517
$(Q)$(RM) $(BIN) $(OBJS) $(DEV_OBJS) $(BUILD_DTB) $(BUILD_DTB2C) $(HIST_BIN) $(HIST_OBJS) $(deps) $(WEB_FILES) $(CACHE_OUT)

0 commit comments

Comments
 (0)