diff --git a/.gitignore b/.gitignore index 2fc80d487..2a3d8299a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ **/*.o **/*.s src/.depend +.build_sha.txt +.build_date.txt # Built binary src/stockfish* diff --git a/src/Makefile b/src/Makefile index 4eb64ffef..c4ea80207 100644 --- a/src/Makefile +++ b/src/Makefile @@ -833,19 +833,28 @@ ifeq ($(pext),yes) endif endif -### 3.8.1 Try to include git commit sha for versioning -GIT_SHA := $(shell git rev-parse HEAD 2>/dev/null | cut -c 1-8) -ifneq ($(GIT_SHA), ) - CXXFLAGS += -DGIT_SHA=$(GIT_SHA) +### 3.8.1 Try to include git info for versioning and avoid recompiles if nothing changes +BUILD_SHA_FILE := .build_sha.txt +BUILD_DATE_FILE := .build_date.txt +GIT_SHA := $(shell git rev-parse HEAD 2>/dev/null | cut -c 1-8 || true) +GIT_DATE := $(shell git show -s --date=format:%Y%m%d --format=%cd HEAD 2>/dev/null || true) +COMPILER_DATE := $(shell date +%Y%m%d 2>/dev/null) + +BUILD_DATE := $(if $(GIT_DATE),$(GIT_DATE),$(COMPILER_DATE)) + +define cache_file_contents +$(shell \ + if [ ! -f "$(1)" ] || [ "$$(cat "$(1)" 2>/dev/null)" != "$(2)" ]; then \ + printf '%s\n' "$(2)" > "$(1)"; \ + fi) +endef + +ifneq ($(filter $(MAKECMDGOALS),help strip install clean net objclean profileclean format config-sanity),$(MAKECMDGOALS)) +_ := $(call cache_file_contents,$(BUILD_SHA_FILE),$(GIT_SHA)) +_ := $(call cache_file_contents,$(BUILD_DATE_FILE),$(BUILD_DATE)) endif -### 3.8.2 Try to include git commit date for versioning -GIT_DATE := $(shell git show -s --date=format:'%Y%m%d' --format=%cd HEAD 2>/dev/null) -ifneq ($(GIT_DATE), ) - CXXFLAGS += -DGIT_DATE=$(GIT_DATE) -endif - -### 3.8.3 Try to include architecture +### 3.8.2 Try to include architecture ifneq ($(ARCH), ) CXXFLAGS += -DARCH=$(ARCH) endif @@ -1035,7 +1044,7 @@ clean: objclean profileclean # clean binaries and objects objclean: - @rm -f stockfish stockfish.exe *.o ./syzygy/*.o ./nnue/*.o ./nnue/features/*.o + @rm -f stockfish stockfish.exe *.o ./syzygy/*.o ./nnue/*.o ./nnue/features/*.o $(BUILD_SHA_FILE) $(BUILD_DATE_FILE) # clean auxiliary profiling files profileclean: @@ -1131,9 +1140,19 @@ config-sanity: net $(EXE): $(OBJS) +$(CXX) -o $@ $(OBJS) $(LDFLAGS) -# Force recompilation to ensure version info is up-to-date -misc.o: FORCE -FORCE: +%.o: %.cpp + $(strip $(CXX) $(CPPFLAGS) $(CXXFLAGS)) -c -o $@ $< + +# Cache git metadata when available, otherwise cache the compiler date. +misc.o: misc.cpp $(BUILD_SHA_FILE) $(BUILD_DATE_FILE) + @sha="$$(cat $(BUILD_SHA_FILE))"; \ + set -- $(CXX) $(CPPFLAGS) $(CXXFLAGS); \ + test -n "$$sha" && set -- "$$@" -DGIT_SHA=$$sha; \ + test -n "$(GIT_DATE)" && set -- "$$@" -DGIT_DATE=$(GIT_DATE); \ + set -- "$$@" -c $< -o $@; \ + printf '%s ' "$$@"; \ + printf '\n'; \ + "$$@" clang-profile-make: $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \