Skip to content

Commit 0856fcd

Browse files
committed
Introduce version_{eq,lt,lte,gt,gte} utils and refactor
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 26e1666 commit 0856fcd

File tree

4 files changed

+58
-34
lines changed

4 files changed

+58
-34
lines changed

Makefile

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -488,27 +488,50 @@ 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)
491+
# If clang-format-x (x > 18) is used, change the 'clang-format-18' to 'clang-format-x' accordingly
492+
CLANG_FORMAT := $(shell which clang-format-18 2>/dev/null)
493+
494+
SHFMT := $(shell which shfmt 2>/dev/null)
495+
493496
BLACK := $(shell which black 2>/dev/null)
497+
BLACK_VERSION := $(shell $(BLACK) --version | head -n 1 | awk '{print $$2}')
498+
BLACK_MAJOR := $(shell echo $(BLACK_VERSION) | cut -f1 -d.)
499+
BLACK_MINOR := $(shell echo $(BLACK_VERSION) | cut -f2 -d.)
500+
BLACK_PATCH := $(shell echo $(BLACK_VERSION) | cut -f3 -d.)
501+
BLACK_ATLEAST_MAJOR := 25
502+
BLACK_ATLEAST_MINOR := 1
503+
BLACK_ATLEAST_PATCH := 0
504+
BLACK_FORMAT_WARNING := Python format check might fail at CI as you use version $(BLACK_VERSION). \
505+
You may switch black to version $(BLACK_ATLEAST_MAJOR).$(BLACK_ATLEAST_MINOR).$(BLACK_ATLEAST_PATCH)
506+
494507
SUBMODULES := $(shell git config --file .gitmodules --get-regexp path | awk '{ print $$2 }')
495508
SUBMODULES_PRUNE_PATHS := $(shell for subm in $(SUBMODULES); do echo -n "-path \"./$$subm\" -o "; done | sed 's/ -o $$//')
509+
496510
format:
497-
ifeq ($(CLANG_FMT),)
498-
$(error clang-format not found. Install clang-format version 18 or above and try again)
511+
ifeq ($(CLANG_FORMAT),)
512+
$(error clang-format-18 not found. Install clang-format version 18 or above and try again)
499513
else
500514
# 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))
515+
$(Q)$(shell $(CLANG_FORMAT) -i $(shell find . \( $(SUBMODULES_PRUNE_PATHS) -o -path \"./$(OUT)\" \) \
516+
-prune -o -name "*.[ch]" -print))
502517
endif
503-
ifeq ($(SH_FMT),)
518+
ifeq ($(SHFMT),)
504519
$(error shfmt not found. Install shfmt and try again)
505520
else
506-
$(Q)$(shell $(SH_FMT) -w $(shell find . \( $(SUBMODULES_PRUNE_PATHS) -o -path \"./$(OUT)\" \) -prune -o -name "*.sh" -print))
521+
$(Q)$(shell $(SHFMT) -w $(shell find . \( $(SUBMODULES_PRUNE_PATHS) -o -path \"./$(OUT)\" \) \
522+
-prune -o -name "*.sh" -print))
507523
endif
508524
ifeq ($(BLACK),)
509525
$(error black not found. Install black version 25.1.0 or above and try again)
510526
else
511-
$(Q)$(shell $(BLACK) --quiet $(shell find . \( $(SUBMODULES_PRUNE_PATHS) -o -path \"./$(OUT)\" \) -prune -o \( -name "*.py" -o -name "*.pyi" \) -print))
527+
ifeq ($(call version_lt,\
528+
$(BLACK_MAJOR),$(BLACK_MINOR),$(BLACK_PATCH),\
529+
$(BLACK_ATLEAST_MAJOR),$(BLACK_ATLEAST_MINOR),$(BLACK_ATLEAST_PATCH)), 1)
530+
$(warning $(BLACK_FORMAT_WARNING))
531+
else
532+
$(Q)$(shell $(BLACK) --quiet $(shell find . \( $(SUBMODULES_PRUNE_PATHS) -o -path \"./$(OUT)\" \) \
533+
-prune -o \( -name "*.py" -o -name "*.pyi" \) -print))
534+
endif
512535
endif
513536
$(Q)$(call notice,All files are properly formatted.)
514537

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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ 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
8889
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
8990
endif
@@ -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)