Skip to content

Commit 11d8684

Browse files
committed
Add make format target and version_{eq,lt,lte,gt,gte} utils
Simplifies the formatting before creating PR, just 'make format'. Version checking is needed for tools, e.g., emcc, browser, and black(python formatting tool). Thus, introduce and refactor with version_{eq,lt,lte,gt,gte} utils for better readability and maintainability (no more 'Pyramid of Doom' checking for MAJOR, MINOR and PATCH).
1 parent 966e5c1 commit 11d8684

File tree

4 files changed

+75
-28
lines changed

4 files changed

+75
-28
lines changed

Makefile

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,52 @@ quake: artifact $(quake_deps)
488488
endif
489489
endif
490490

491+
CLANG_FORMAT := $(shell which clang-format-18 2>/dev/null)
492+
493+
SHFMT := $(shell which shfmt 2>/dev/null)
494+
495+
BLACK := $(shell which black 2>/dev/null)
496+
BLACK_VERSION := $(if $(strip $(BLACK)),$(shell $(BLACK) --version | head -n 1 | awk '{print $$2}'),)
497+
BLACK_MAJOR := $(shell echo $(BLACK_VERSION) | cut -f1 -d.)
498+
BLACK_MINOR := $(shell echo $(BLACK_VERSION) | cut -f2 -d.)
499+
BLACK_PATCH := $(shell echo $(BLACK_VERSION) | cut -f3 -d.)
500+
BLACK_ATLEAST_MAJOR := 25
501+
BLACK_ATLEAST_MINOR := 1
502+
BLACK_ATLEAST_PATCH := 0
503+
BLACK_FORMAT_WARNING := Python format check might fail at CI as you use version $(BLACK_VERSION). \
504+
You may switch black to version $(BLACK_ATLEAST_MAJOR).$(BLACK_ATLEAST_MINOR).$(BLACK_ATLEAST_PATCH)
505+
506+
SUBMODULES := $(shell git config --file .gitmodules --get-regexp path | awk '{ print $$2 }')
507+
SUBMODULES_PRUNE_PATHS := $(shell for subm in $(SUBMODULES); do echo -n "-path \"./$$subm\" -o "; done | sed 's/ -o $$//')
508+
509+
format:
510+
ifeq ($(CLANG_FORMAT),)
511+
$(error clang-format-18 not found. Install clang-format version 18 and try again)
512+
else
513+
# Skip formatting submodules and everything in $(OUT), apply the same rule for shfmt and black
514+
$(Q)$(CLANG_FORMAT) -i $(shell find . \( $(SUBMODULES_PRUNE_PATHS) -o -path \"./$(OUT)\" \) \
515+
-prune -o -name "*.[ch]" -print)
516+
endif
517+
ifeq ($(SHFMT),)
518+
$(error shfmt not found. Install shfmt and try again)
519+
else
520+
$(Q)$(SHFMT) -w $(shell find . \( $(SUBMODULES_PRUNE_PATHS) -o -path \"./$(OUT)\" \) \
521+
-prune -o -name "*.sh" -print)
522+
endif
523+
ifeq ($(BLACK),)
524+
$(error black not found. Install black version 25.1.0 or above and try again)
525+
else
526+
ifeq ($(call version_lt,\
527+
$(BLACK_MAJOR),$(BLACK_MINOR),$(BLACK_PATCH),\
528+
$(BLACK_ATLEAST_MAJOR),$(BLACK_ATLEAST_MINOR),$(BLACK_ATLEAST_PATCH)), 1)
529+
$(warning $(BLACK_FORMAT_WARNING))
530+
else
531+
$(Q)$(BLACK) --quiet $(shell find . \( $(SUBMODULES_PRUNE_PATHS) -o -path \"./$(OUT)\" \) \
532+
-prune -o \( -name "*.py" -o -name "*.pyi" \) -print)
533+
endif
534+
endif
535+
$(Q)$(call notice,All files are properly formatted.)
536+
491537
clean:
492538
$(VECHO) "Cleaning... "
493539
$(Q)$(RM) $(BIN) $(OBJS) $(DEV_OBJS) $(BUILD_DTB) $(BUILD_DTB2C) $(HIST_BIN) $(HIST_OBJS) $(deps) $(WEB_FILES) $(CACHE_OUT)
@@ -505,6 +551,6 @@ distclean: clean
505551
$(Q)$(call notice, [OK])
506552

507553
.PHONY: all config tool check check-hello misalign misalign-in-blk-emu mmu-test
508-
.PHONY: gdbstub-test doom quake clean distclean artifact
554+
.PHONY: gdbstub-test doom quake format clean distclean artifact
509555

510556
-include $(deps)

mk/common.mk

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ warn = $(PRINTF) "$(YELLOW)$(strip $1)$(NC)\n"
5353
# Used inside $(warning) or $(error)
5454
warnx = $(shell echo "$(YELLOW)$(strip $1)$(NC)\n")
5555

56+
# Version checking
57+
version_num = $(shell printf "%d%03d%03d" $(1) $(2) $(3))
58+
# Compare two versions with bc
59+
# Returns 1 if first == second else 0
60+
version_eq = $(shell echo "$$(($(call version_num,$(1),$(2),$(3)) == $(call version_num,$(4),$(5),$(6))))" | bc)
61+
# Returns 1 if first < second else 0
62+
version_lt = $(shell echo "$$(($(call version_num,$(1),$(2),$(3)) < $(call version_num,$(4),$(5),$(6))))" | bc)
63+
# Returns 1 if first <= second else 0
64+
version_lte = $(shell echo "$$(($(call version_num,$(1),$(2),$(3)) <= $(call version_num,$(4),$(5),$(6))))" | bc)
65+
# Returns 1 if first > second else 0
66+
version_gt = $(shell echo "$$(($(call version_num,$(1),$(2),$(3)) > $(call version_num,$(4),$(5),$(6))))" | bc)
67+
# Returns 1 if first >= second else 0
68+
version_gte = $(shell echo "$$(($(call version_num,$(1),$(2),$(3)) >= $(call version_num,$(4),$(5),$(6))))" | bc)
69+
5670
# File utilities
5771
SHA1SUM = sha1sum
5872
SHA1SUM := $(shell which $(SHA1SUM))

mk/toolchain.mk

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,21 @@ ifneq ($(shell $(CC) --version | head -n 1 | grep emcc),)
1414
SDL_MUSIC_PLAY_AT_EMCC_MINOR := 1
1515
SDL_MUSIC_PLAY_AT_EMCC_PATCH := 51
1616
SDL_MUSIC_CANNOT_PLAY_WARNING := Video games music might not be played. You may switch emcc to version $(SDL_MUSIC_PLAY_AT_EMCC_MAJOR).$(SDL_MUSIC_PLAY_AT_EMCC_MINOR).$(SDL_MUSIC_PLAY_AT_EMCC_PATCH)
17-
ifeq ($(shell echo $(EMCC_MAJOR)\==$(SDL_MUSIC_PLAY_AT_EMCC_MAJOR) | bc), 1)
18-
ifeq ($(shell echo $(EMCC_MINOR)\==$(SDL_MUSIC_PLAY_AT_EMCC_MINOR) | bc), 1)
19-
ifeq ($(shell echo $(EMCC_PATCH)\==$(SDL_MUSIC_PLAY_AT_EMCC_PATCH) | bc), 1)
20-
# do nothing
21-
else
22-
$(warning $(SDL_MUSIC_CANNOT_PLAY_WARNING))
23-
endif
24-
else
25-
$(warning $(SDL_MUSIC_CANNOT_PLAY_WARNING))
26-
endif
27-
else
28-
$(warning $(SDL_MUSIC_CANNOT_PLAY_WARNING))
17+
ifeq ($(call version_eq,\
18+
$(EMCC_MAJOR),$(EMCC_MINOR),$(EMCC_PATCH),\
19+
$(SDL_MUSIC_PLAY_AT_EMCC_MAJOR),$(SDL_MUSIC_PLAY_AT_EMCC_MINOR),$(SDL_MUSIC_PLAY_AT_EMCC_PATCH)), 0)
20+
$(warning $(SDL_MUSIC_CANNOT_PLAY_WARNING))
2921
endif
3022

3123
# see commit 165c1a3 of emscripten
3224
MIMALLOC_SUPPORT_SINCE_MAJOR := 3
3325
MIMALLOC_SUPPORT_SINCE_MINOR := 1
3426
MIMALLOC_SUPPORT_SINCE_PATCH := 50
3527
MIMALLOC_UNSUPPORTED_WARNING := mimalloc is supported after version $(MIMALLOC_SUPPORT_SINCE_MAJOR).$(MIMALLOC_SUPPORT_SINCE_MINOR).$(MIMALLOC_SUPPORT_SINCE_PATCH)
36-
ifeq ($(shell echo $(EMCC_MAJOR)\>=$(MIMALLOC_SUPPORT_SINCE_MAJOR) | bc), 1)
37-
ifeq ($(shell echo $(EMCC_MINOR)\>=$(MIMALLOC_SUPPORT_SINCE_MINOR) | bc), 1)
38-
ifeq ($(shell echo $(EMCC_PATCH)\>=$(MIMALLOC_SUPPORT_SINCE_PATCH) | bc), 1)
39-
CFLAGS_emcc += -sMALLOC=mimalloc
40-
else
41-
$(warning $(MIMALLOC_UNSUPPORTED_WARNING))
42-
endif
43-
else
44-
$(warning $(MIMALLOC_UNSUPPORTED_WARNING))
45-
endif
28+
ifeq ($(call version_gte,\
29+
$(EMCC_MAJOR),$(EMCC_MINOR),$(EMCC_PATCH),\
30+
$(MIMALLOC_SUPPORT_SINCE_MAJOR),$(MIMALLOC_SUPPORT_SINCE_MINOR),$(MIMALLOC_SUPPORT_SINCE_PATCH)), 1)
31+
CFLAGS_emcc += -sMALLOC=mimalloc
4632
else
4733
$(warning $(MIMALLOC_UNSUPPORTED_WARNING))
4834
endif

mk/wasm.mk

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ ifeq ($(UNAME_S),Darwin)
8383
SAFARI_MAJOR :=
8484
SAFARI_MINOR :=
8585
SAFARI_VERSION_CHECK_CMD :=
86-
SAFARI_SUPPORT_TCO_AT_MAJOR_MINOR := 18.2
86+
SAFARI_SUPPORT_TCO_AT_MAJOR := 18
87+
SAFARI_SUPPORT_TCO_AT_MINOR := 2
8788
SAFARI_SUPPORT_TCO_INFO := Safari supports TCO, you can use Safari to request the wasm
88-
SAFARI_NO_SUPPORT_TCO_WARNING := Safari not found or Safari must have at least version $(SAFARI_SUPPORT_TCO_AT_MAJOR_MINOR) to support TCO in wasm
89+
SAFARI_NO_SUPPORT_TCO_WARNING := Safari not found or Safari must have at least version $(SAFARI_SUPPORT_TCO_AT_MAJOR).$(SAFARI_SUPPORT_TCO_AT_MINOR) to support TCO in wasm
8990
endif
9091

9192
# FIXME: for Windows
@@ -101,14 +102,14 @@ CHROME_MAJOR := $(shell $(CHROME_MAJOR_VERSION_CHECK_CMD))
101102
FIREFOX_MAJOR := $(shell $(FIREFOX_MAJOR_VERSION_CHECK_CMD))
102103

103104
# Chrome
104-
ifeq ($(shell echo $(CHROME_MAJOR)\>=$(CHROME_SUPPORT_TCO_AT_MAJOR) | bc), 1)
105+
ifeq ($(call version_gte,$(CHROME_MAJOR),,,$(CHROME_SUPPORT_TCO_AT_MAJOR),,), 1)
105106
$(info $(call noticex, $(CHROME_SUPPORT_TCO_INFO)))
106107
else
107108
$(warning $(call warnx, $(CHROME_NO_SUPPORT_TCO_WARNING)))
108109
endif
109110

110111
# Firefox
111-
ifeq ($(shell echo $(FIREFOX_MAJOR)\>=$(FIREFOX_SUPPORT_TCO_AT_MAJOR) | bc), 1)
112+
ifeq ($(call version_gte,$(FIREFOX_MAJOR),,,$(FIREFOX_SUPPORT_TCO_AT_MAJOR),,), 1)
112113
$(info $(call noticex, $(FIREFOX_SUPPORT_TCO_INFO)))
113114
else
114115
$(warning $(call warnx, $(FIREFOX_NO_SUPPORT_TCO_WARNING)))
@@ -120,7 +121,7 @@ ifeq ($(UNAME_S),Darwin)
120121
SAFARI_VERSION := $(shell $(SAFARI_VERSION_CHECK_CMD))
121122
SAFARI_MAJOR := $(shell echo $(SAFARI_VERSION) | cut -f1 -d.)
122123
SAFARI_MINOR := $(shell echo $(SAFARI_VERSION) | cut -f2 -d.)
123-
ifeq ($(shell echo "$(SAFARI_MAJOR).$(SAFARI_MINOR)>=$(SAFARI_SUPPORT_TCO_AT_MAJOR_MINOR)" | bc), 1)
124+
ifeq ($(call version_gte,$(SAFARI_MAJOR),$(SAFARI_MINOR),,$(SAFARI_SUPPORT_TCO_AT_MAJOR),$(SAFARI_SUPPORT_TCO_AT_MINOR),), 1)
124125
$(info $(call noticex, $(SAFARI_SUPPORT_TCO_INFO)))
125126
else
126127
$(warning $(call warnx, $(SAFARI_NO_SUPPORT_TCO_WARNING)))

0 commit comments

Comments
 (0)