Skip to content
Open
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
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ However, participation requires adherence to fundamental ground rules:
For instance, opt for "initialize" over "initialise" and "color" rather than "colour".

Software requirement:
* [clang-format](https://clang.llvm.org/docs/ClangFormat.html) version 18 or later.
* [clang-format](https://clang.llvm.org/docs/ClangFormat.html) version 18.
* [shfmt](https://github.com/mvdan/sh).
* [black](https://github.com/psf/black) version 25.1.0.

To maintain a uniform style across languages, run:
* `clang-format -i *.{c,h}` to apply the project’s C/C++ formatting rules from the up-to-date .clang-format file.
* `shfmt -w $(find . -type f -name "*.sh")` to clean and standardize all shell scripts.
* `black .` to enforce a consistent, idiomatic layout for Python code.
* `make format` to automatically run all three formatters.

## Coding Style for Shell Script

Expand Down
48 changes: 47 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,52 @@ quake: artifact $(quake_deps)
endif
endif

CLANG_FORMAT := $(shell which clang-format-18 2>/dev/null)

SHFMT := $(shell which shfmt 2>/dev/null)

BLACK := $(shell which black 2>/dev/null)
BLACK_VERSION := $(if $(strip $(BLACK)),$(shell $(BLACK) --version | head -n 1 | awk '{print $$2}'),)
BLACK_MAJOR := $(shell echo $(BLACK_VERSION) | cut -f1 -d.)
BLACK_MINOR := $(shell echo $(BLACK_VERSION) | cut -f2 -d.)
BLACK_PATCH := $(shell echo $(BLACK_VERSION) | cut -f3 -d.)
BLACK_ATLEAST_MAJOR := 25
BLACK_ATLEAST_MINOR := 1
BLACK_ATLEAST_PATCH := 0
BLACK_FORMAT_WARNING := Python format check might fail at CI as you use version $(BLACK_VERSION). \
You may switch black to version $(BLACK_ATLEAST_MAJOR).$(BLACK_ATLEAST_MINOR).$(BLACK_ATLEAST_PATCH)

SUBMODULES := $(shell git config --file .gitmodules --get-regexp path | awk '{ print $$2 }')
SUBMODULES_PRUNE_PATHS := $(shell for subm in $(SUBMODULES); do echo -n "-path \"./$$subm\" -o "; done | sed 's/ -o $$//')

format:
ifeq ($(CLANG_FORMAT),)
$(error clang-format-18 not found. Install clang-format version 18 and try again)
else
# Skip formatting submodules and everything in $(OUT), apply the same rule for shfmt and black
$(Q)$(CLANG_FORMAT) -i $(shell find . \( $(SUBMODULES_PRUNE_PATHS) -o -path \"./$(OUT)\" \) \
-prune -o -name "*.[ch]" -print)
endif
ifeq ($(SHFMT),)
$(error shfmt not found. Install shfmt and try again)
else
$(Q)$(SHFMT) -w $(shell find . \( $(SUBMODULES_PRUNE_PATHS) -o -path \"./$(OUT)\" \) \
-prune -o -name "*.sh" -print)
endif
ifeq ($(BLACK),)
$(error black not found. Install black version 25.1.0 or above and try again)
else
ifeq ($(call version_lt,\
$(BLACK_MAJOR),$(BLACK_MINOR),$(BLACK_PATCH),\
$(BLACK_ATLEAST_MAJOR),$(BLACK_ATLEAST_MINOR),$(BLACK_ATLEAST_PATCH)), 1)
$(warning $(BLACK_FORMAT_WARNING))
else
$(Q)$(BLACK) --quiet $(shell find . \( $(SUBMODULES_PRUNE_PATHS) -o -path \"./$(OUT)\" \) \
-prune -o \( -name "*.py" -o -name "*.pyi" \) -print)
endif
endif
$(Q)$(call notice,All files are properly formatted.)

clean:
$(VECHO) "Cleaning... "
$(Q)$(RM) $(BIN) $(OBJS) $(DEV_OBJS) $(BUILD_DTB) $(BUILD_DTB2C) $(HIST_BIN) $(HIST_OBJS) $(deps) $(WEB_FILES) $(CACHE_OUT)
Expand All @@ -505,6 +551,6 @@ distclean: clean
$(Q)$(call notice, [OK])

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

-include $(deps)
14 changes: 14 additions & 0 deletions mk/common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ warn = $(PRINTF) "$(YELLOW)$(strip $1)$(NC)\n"
# Used inside $(warning) or $(error)
warnx = $(shell echo "$(YELLOW)$(strip $1)$(NC)\n")

# Version checking
version_num = $(shell printf "%d%03d%03d" $(1) $(2) $(3))
# Compare two versions with bc
# Returns 1 if first == second else 0
version_eq = $(shell echo "$$(($(call version_num,$(1),$(2),$(3)) == $(call version_num,$(4),$(5),$(6))))" | bc)
# Returns 1 if first < second else 0
version_lt = $(shell echo "$$(($(call version_num,$(1),$(2),$(3)) < $(call version_num,$(4),$(5),$(6))))" | bc)
# Returns 1 if first <= second else 0
version_lte = $(shell echo "$$(($(call version_num,$(1),$(2),$(3)) <= $(call version_num,$(4),$(5),$(6))))" | bc)
# Returns 1 if first > second else 0
version_gt = $(shell echo "$$(($(call version_num,$(1),$(2),$(3)) > $(call version_num,$(4),$(5),$(6))))" | bc)
# Returns 1 if first >= second else 0
version_gte = $(shell echo "$$(($(call version_num,$(1),$(2),$(3)) >= $(call version_num,$(4),$(5),$(6))))" | bc)

# File utilities
SHA1SUM = sha1sum
SHA1SUM := $(shell which $(SHA1SUM))
Expand Down
30 changes: 8 additions & 22 deletions mk/toolchain.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,21 @@ ifneq ($(shell $(CC) --version | head -n 1 | grep emcc),)
SDL_MUSIC_PLAY_AT_EMCC_MINOR := 1
SDL_MUSIC_PLAY_AT_EMCC_PATCH := 51
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)
ifeq ($(shell echo $(EMCC_MAJOR)\==$(SDL_MUSIC_PLAY_AT_EMCC_MAJOR) | bc), 1)
ifeq ($(shell echo $(EMCC_MINOR)\==$(SDL_MUSIC_PLAY_AT_EMCC_MINOR) | bc), 1)
ifeq ($(shell echo $(EMCC_PATCH)\==$(SDL_MUSIC_PLAY_AT_EMCC_PATCH) | bc), 1)
# do nothing
else
$(warning $(SDL_MUSIC_CANNOT_PLAY_WARNING))
endif
else
$(warning $(SDL_MUSIC_CANNOT_PLAY_WARNING))
endif
else
$(warning $(SDL_MUSIC_CANNOT_PLAY_WARNING))
ifeq ($(call version_eq,\
$(EMCC_MAJOR),$(EMCC_MINOR),$(EMCC_PATCH),\
$(SDL_MUSIC_PLAY_AT_EMCC_MAJOR),$(SDL_MUSIC_PLAY_AT_EMCC_MINOR),$(SDL_MUSIC_PLAY_AT_EMCC_PATCH)), 0)
$(warning $(SDL_MUSIC_CANNOT_PLAY_WARNING))
endif

# see commit 165c1a3 of emscripten
MIMALLOC_SUPPORT_SINCE_MAJOR := 3
MIMALLOC_SUPPORT_SINCE_MINOR := 1
MIMALLOC_SUPPORT_SINCE_PATCH := 50
MIMALLOC_UNSUPPORTED_WARNING := mimalloc is supported after version $(MIMALLOC_SUPPORT_SINCE_MAJOR).$(MIMALLOC_SUPPORT_SINCE_MINOR).$(MIMALLOC_SUPPORT_SINCE_PATCH)
ifeq ($(shell echo $(EMCC_MAJOR)\>=$(MIMALLOC_SUPPORT_SINCE_MAJOR) | bc), 1)
ifeq ($(shell echo $(EMCC_MINOR)\>=$(MIMALLOC_SUPPORT_SINCE_MINOR) | bc), 1)
ifeq ($(shell echo $(EMCC_PATCH)\>=$(MIMALLOC_SUPPORT_SINCE_PATCH) | bc), 1)
CFLAGS_emcc += -sMALLOC=mimalloc
else
$(warning $(MIMALLOC_UNSUPPORTED_WARNING))
endif
else
$(warning $(MIMALLOC_UNSUPPORTED_WARNING))
endif
ifeq ($(call version_gte,\
$(EMCC_MAJOR),$(EMCC_MINOR),$(EMCC_PATCH),\
$(MIMALLOC_SUPPORT_SINCE_MAJOR),$(MIMALLOC_SUPPORT_SINCE_MINOR),$(MIMALLOC_SUPPORT_SINCE_PATCH)), 1)
CFLAGS_emcc += -sMALLOC=mimalloc
else
$(warning $(MIMALLOC_UNSUPPORTED_WARNING))
endif
Expand Down
11 changes: 6 additions & 5 deletions mk/wasm.mk
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ ifeq ($(UNAME_S),Darwin)
SAFARI_MAJOR :=
SAFARI_MINOR :=
SAFARI_VERSION_CHECK_CMD :=
SAFARI_SUPPORT_TCO_AT_MAJOR_MINOR := 18.2
SAFARI_SUPPORT_TCO_AT_MAJOR := 18
SAFARI_SUPPORT_TCO_AT_MINOR := 2
SAFARI_SUPPORT_TCO_INFO := Safari supports TCO, you can use Safari to request the wasm
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
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
endif

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

# Chrome
ifeq ($(shell echo $(CHROME_MAJOR)\>=$(CHROME_SUPPORT_TCO_AT_MAJOR) | bc), 1)
ifeq ($(call version_gte,$(CHROME_MAJOR),,,$(CHROME_SUPPORT_TCO_AT_MAJOR),,), 1)
$(info $(call noticex, $(CHROME_SUPPORT_TCO_INFO)))
else
$(warning $(call warnx, $(CHROME_NO_SUPPORT_TCO_WARNING)))
endif

# Firefox
ifeq ($(shell echo $(FIREFOX_MAJOR)\>=$(FIREFOX_SUPPORT_TCO_AT_MAJOR) | bc), 1)
ifeq ($(call version_gte,$(FIREFOX_MAJOR),,,$(FIREFOX_SUPPORT_TCO_AT_MAJOR),,), 1)
$(info $(call noticex, $(FIREFOX_SUPPORT_TCO_INFO)))
else
$(warning $(call warnx, $(FIREFOX_NO_SUPPORT_TCO_WARNING)))
Expand All @@ -120,7 +121,7 @@ ifeq ($(UNAME_S),Darwin)
SAFARI_VERSION := $(shell $(SAFARI_VERSION_CHECK_CMD))
SAFARI_MAJOR := $(shell echo $(SAFARI_VERSION) | cut -f1 -d.)
SAFARI_MINOR := $(shell echo $(SAFARI_VERSION) | cut -f2 -d.)
ifeq ($(shell echo "$(SAFARI_MAJOR).$(SAFARI_MINOR)>=$(SAFARI_SUPPORT_TCO_AT_MAJOR_MINOR)" | bc), 1)
ifeq ($(call version_gte,$(SAFARI_MAJOR),$(SAFARI_MINOR),,$(SAFARI_SUPPORT_TCO_AT_MAJOR),$(SAFARI_SUPPORT_TCO_AT_MINOR),), 1)
$(info $(call noticex, $(SAFARI_SUPPORT_TCO_INFO)))
else
$(warning $(call warnx, $(SAFARI_NO_SUPPORT_TCO_WARNING)))
Expand Down
Loading