Makefile: generate "git.rc" via GIT-VERSION-GEN

The "git.rc" is used on Windows to embed information like the project name and version into the resulting executables. As such we need to inject the version information, which we do by using preprocessor defines. The logic to do so is non-trivial and needs to be kept in sync with the different build systems. Refactor the logic so that we generate "git.rc" via `GIT-VERSION-GEN`. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Dec 6, 2024 at 14:24 UTC 9bb10d27e7072cec919d38f0b987c70e598039ad
5 files changed +30 -17
.gitignore
+1
@@ -199,6 +199,7 @@
199 *.tar.gz
200 *.dsc
201 *.deb
202 +/git.rc
203 /git.spec
204 *.exe
205 *.[aos]
GIT-VERSION-GEN
+6 -2
@@ -58,14 +58,18 @@ then
58 GIT_USER_AGENT="git/$GIT_VERSION"
59 fi
60
61 -read GIT_MAJOR_VERSION GIT_MINOR_VERSION GIT_MICRO_VERSION trailing <<EOF
62 -$(echo "$GIT_VERSION" 0 0 0 | tr '.a-zA-Z-' ' ')
61 +# While released Git versions only have three numbers, development builds also
62 +# have a fourth number that corresponds to the number of patches since the last
63 +# release.
64 +read GIT_MAJOR_VERSION GIT_MINOR_VERSION GIT_MICRO_VERSION GIT_PATCH_LEVEL trailing <<EOF
65 +$(echo "$GIT_VERSION" 0 0 0 0 | tr '.a-zA-Z-' ' ')
66 EOF
67
68 sed -e "s|@GIT_VERSION@|$GIT_VERSION|" \
69 -e "s|@GIT_MAJOR_VERSION@|$GIT_MAJOR_VERSION|" \
70 -e "s|@GIT_MINOR_VERSION@|$GIT_MINOR_VERSION|" \
71 -e "s|@GIT_MICRO_VERSION@|$GIT_MICRO_VERSION|" \
72 + -e "s|@GIT_PATCH_LEVEL@|$GIT_PATCH_LEVEL|" \
73 -e "s|@GIT_BUILT_FROM_COMMIT@|$GIT_BUILT_FROM_COMMIT|" \
74 -e "s|@GIT_USER_AGENT@|$GIT_USER_AGENT|" \
75 "$INPUT" >"$OUTPUT"+
Makefile
+7 -6
@@ -2568,11 +2568,12 @@ $(SCRIPT_LIB) : % : %.sh GIT-SCRIPT-DEFINES
2568 $(QUIET_GEN)$(cmd_munge_script) && \
2569 mv $@+ $@
2570
2571 -git.res: git.rc GIT-VERSION-FILE GIT-PREFIX
2572 - $(QUIET_RC)$(RC) \
2573 - $(join -DMAJOR= -DMINOR= -DMICRO= -DPATCHLEVEL=, $(wordlist 1, 4, \
2574 - $(shell echo $(GIT_VERSION) 0 0 0 0 | tr '.a-zA-Z-' ' '))) \
2575 - -DGIT_VERSION="\\\"$(GIT_VERSION)\\\"" -i $< -o $@
2571 +git.rc: git.rc.in GIT-VERSION-GEN GIT-VERSION-FILE
2572 + $(QUIET_GEN)$(SHELL_PATH) ./GIT-VERSION-GEN "$(shell pwd)" $< $@+
2573 + @if cmp $@+ $@ >/dev/null 2>&1; then $(RM) $@+; else mv $@+ $@; fi
2574 +
2575 +git.res: git.rc GIT-PREFIX
2576 + $(QUIET_RC)$(RC) -i $< -o $@
2577
2578 # This makes sure we depend on the NO_PERL setting itself.
2579 $(SCRIPT_PERL_GEN): GIT-BUILD-OPTIONS
@@ -3717,7 +3718,7 @@ clean: profile-clean coverage-clean cocciclean
3718 $(RM) -r .build $(UNIT_TEST_BIN)
3719 $(RM) GIT-TEST-SUITES
3720 $(RM) po/git.pot po/git-core.pot
3720 - $(RM) git.res
3721 + $(RM) git.rc git.res
3722 $(RM) $(OBJECTS)
3723 $(RM) headless-git.o
3724 $(RM) $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB)
contrib/buildsystems/CMakeLists.txt
+13 -6
@@ -691,18 +691,25 @@ list(TRANSFORM reftable_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
691 add_library(reftable STATIC ${reftable_SOURCES})
692
693 if(WIN32)
694 + add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.rc
695 + COMMAND "${SH_EXE}" "${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN"
696 + "${CMAKE_SOURCE_DIR}"
697 + "${CMAKE_SOURCE_DIR}/git.rc.in"
698 + "${CMAKE_BINARY_DIR}/git.rc"
699 + DEPENDS "${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN"
700 + "${CMAKE_SOURCE_DIR}/git.rc.in"
701 + VERBATIM)
702 +
703 if(NOT MSVC)#use windres when compiling with gcc and clang
704 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
696 - COMMAND ${WINDRES_EXE} -O coff -DMAJOR=${PROJECT_VERSION_MAJOR} -DMINOR=${PROJECT_VERSION_MINOR}
697 - -DMICRO=${PROJECT_VERSION_PATCH} -DPATCHLEVEL=0 -DGIT_VERSION="\\\"${PROJECT_VERSION}.GIT\\\""
698 - -i ${CMAKE_SOURCE_DIR}/git.rc -o ${CMAKE_BINARY_DIR}/git.res
705 + COMMAND ${WINDRES_EXE} -O coff -i ${CMAKE_BINARY_DIR}/git.rc -o ${CMAKE_BINARY_DIR}/git.res
706 + DEPENDS "${CMAKE_BINARY_DIR}/git.rc"
707 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
708 VERBATIM)
709 else()#MSVC use rc
710 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
703 - COMMAND ${CMAKE_RC_COMPILER} /d MAJOR=${PROJECT_VERSION_MAJOR} /d MINOR=${PROJECT_VERSION_MINOR}
704 - /d MICRO=${PROJECT_VERSION_PATCH} /d PATCHLEVEL=0 /d GIT_VERSION="${PROJECT_VERSION}.GIT"
705 - /fo ${CMAKE_BINARY_DIR}/git.res ${CMAKE_SOURCE_DIR}/git.rc
711 + COMMAND ${CMAKE_RC_COMPILER} /fo ${CMAKE_BINARY_DIR}/git.res ${CMAKE_BINARY_DIR}/git.rc
712 + DEPENDS "${CMAKE_BINARY_DIR}/git.rc"
713 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
714 VERBATIM)
715 endif()
git.rc.in renamed
+3 -3
@@ -1,6 +1,6 @@
1 1 VERSIONINFO
2 -FILEVERSION MAJOR,MINOR,MICRO,PATCHLEVEL
3 -PRODUCTVERSION MAJOR,MINOR,MICRO,PATCHLEVEL
2 +FILEVERSION @GIT_MAJOR_VERSION@,@GIT_MINOR_VERSION@,@GIT_MICRO_VERSION@,@GIT_PATCH_LEVEL@
3 +PRODUCTVERSION @GIT_MAJOR_VERSION@,@GIT_MINOR_VERSION@,@GIT_MICRO_VERSION@,@GIT_PATCH_LEVEL@
4 BEGIN
5 BLOCK "StringFileInfo"
6 BEGIN
@@ -11,7 +11,7 @@ BEGIN
11 VALUE "InternalName", "git\0"
12 VALUE "OriginalFilename", "git.exe\0"
13 VALUE "ProductName", "Git\0"
14 - VALUE "ProductVersion", GIT_VERSION "\0"
14 + VALUE "ProductVersion", "@GIT_VERSION@\0"
15 END
16 END
17