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>
maint
Patrick Steinhardt 2024-12-06 14:24:40 +01:00 committed by Junio C Hamano
parent 0c8d339514
commit 9bb10d27e7
5 changed files with 30 additions and 17 deletions

1
.gitignore vendored
View File

@ -199,6 +199,7 @@
*.tar.gz *.tar.gz
*.dsc *.dsc
*.deb *.deb
/git.rc
/git.spec /git.spec
*.exe *.exe
*.[aos] *.[aos]

View File

@ -58,14 +58,18 @@ then
GIT_USER_AGENT="git/$GIT_VERSION" GIT_USER_AGENT="git/$GIT_VERSION"
fi fi


read GIT_MAJOR_VERSION GIT_MINOR_VERSION GIT_MICRO_VERSION trailing <<EOF # While released Git versions only have three numbers, development builds also
$(echo "$GIT_VERSION" 0 0 0 | tr '.a-zA-Z-' ' ') # have a fourth number that corresponds to the number of patches since the last
# release.
read GIT_MAJOR_VERSION GIT_MINOR_VERSION GIT_MICRO_VERSION GIT_PATCH_LEVEL trailing <<EOF
$(echo "$GIT_VERSION" 0 0 0 0 | tr '.a-zA-Z-' ' ')
EOF EOF


sed -e "s|@GIT_VERSION@|$GIT_VERSION|" \ sed -e "s|@GIT_VERSION@|$GIT_VERSION|" \
-e "s|@GIT_MAJOR_VERSION@|$GIT_MAJOR_VERSION|" \ -e "s|@GIT_MAJOR_VERSION@|$GIT_MAJOR_VERSION|" \
-e "s|@GIT_MINOR_VERSION@|$GIT_MINOR_VERSION|" \ -e "s|@GIT_MINOR_VERSION@|$GIT_MINOR_VERSION|" \
-e "s|@GIT_MICRO_VERSION@|$GIT_MICRO_VERSION|" \ -e "s|@GIT_MICRO_VERSION@|$GIT_MICRO_VERSION|" \
-e "s|@GIT_PATCH_LEVEL@|$GIT_PATCH_LEVEL|" \
-e "s|@GIT_BUILT_FROM_COMMIT@|$GIT_BUILT_FROM_COMMIT|" \ -e "s|@GIT_BUILT_FROM_COMMIT@|$GIT_BUILT_FROM_COMMIT|" \
-e "s|@GIT_USER_AGENT@|$GIT_USER_AGENT|" \ -e "s|@GIT_USER_AGENT@|$GIT_USER_AGENT|" \
"$INPUT" >"$OUTPUT"+ "$INPUT" >"$OUTPUT"+

View File

@ -2568,11 +2568,12 @@ $(SCRIPT_LIB) : % : %.sh GIT-SCRIPT-DEFINES
$(QUIET_GEN)$(cmd_munge_script) && \ $(QUIET_GEN)$(cmd_munge_script) && \
mv $@+ $@ mv $@+ $@


git.res: git.rc GIT-VERSION-FILE GIT-PREFIX git.rc: git.rc.in GIT-VERSION-GEN GIT-VERSION-FILE
$(QUIET_RC)$(RC) \ $(QUIET_GEN)$(SHELL_PATH) ./GIT-VERSION-GEN "$(shell pwd)" $< $@+
$(join -DMAJOR= -DMINOR= -DMICRO= -DPATCHLEVEL=, $(wordlist 1, 4, \ @if cmp $@+ $@ >/dev/null 2>&1; then $(RM) $@+; else mv $@+ $@; fi
$(shell echo $(GIT_VERSION) 0 0 0 0 | tr '.a-zA-Z-' ' '))) \
-DGIT_VERSION="\\\"$(GIT_VERSION)\\\"" -i $< -o $@ git.res: git.rc GIT-PREFIX
$(QUIET_RC)$(RC) -i $< -o $@


# This makes sure we depend on the NO_PERL setting itself. # This makes sure we depend on the NO_PERL setting itself.
$(SCRIPT_PERL_GEN): GIT-BUILD-OPTIONS $(SCRIPT_PERL_GEN): GIT-BUILD-OPTIONS
@ -3717,7 +3718,7 @@ clean: profile-clean coverage-clean cocciclean
$(RM) -r .build $(UNIT_TEST_BIN) $(RM) -r .build $(UNIT_TEST_BIN)
$(RM) GIT-TEST-SUITES $(RM) GIT-TEST-SUITES
$(RM) po/git.pot po/git-core.pot $(RM) po/git.pot po/git-core.pot
$(RM) git.res $(RM) git.rc git.res
$(RM) $(OBJECTS) $(RM) $(OBJECTS)
$(RM) headless-git.o $(RM) headless-git.o
$(RM) $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(RM) $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB)

View File

@ -691,18 +691,25 @@ list(TRANSFORM reftable_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
add_library(reftable STATIC ${reftable_SOURCES}) add_library(reftable STATIC ${reftable_SOURCES})


if(WIN32) if(WIN32)
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.rc
COMMAND "${SH_EXE}" "${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN"
"${CMAKE_SOURCE_DIR}"
"${CMAKE_SOURCE_DIR}/git.rc.in"
"${CMAKE_BINARY_DIR}/git.rc"
DEPENDS "${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN"
"${CMAKE_SOURCE_DIR}/git.rc.in"
VERBATIM)

if(NOT MSVC)#use windres when compiling with gcc and clang if(NOT MSVC)#use windres when compiling with gcc and clang
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
COMMAND ${WINDRES_EXE} -O coff -DMAJOR=${PROJECT_VERSION_MAJOR} -DMINOR=${PROJECT_VERSION_MINOR} COMMAND ${WINDRES_EXE} -O coff -i ${CMAKE_BINARY_DIR}/git.rc -o ${CMAKE_BINARY_DIR}/git.res
-DMICRO=${PROJECT_VERSION_PATCH} -DPATCHLEVEL=0 -DGIT_VERSION="\\\"${PROJECT_VERSION}.GIT\\\"" DEPENDS "${CMAKE_BINARY_DIR}/git.rc"
-i ${CMAKE_SOURCE_DIR}/git.rc -o ${CMAKE_BINARY_DIR}/git.res
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
VERBATIM) VERBATIM)
else()#MSVC use rc else()#MSVC use rc
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
COMMAND ${CMAKE_RC_COMPILER} /d MAJOR=${PROJECT_VERSION_MAJOR} /d MINOR=${PROJECT_VERSION_MINOR} COMMAND ${CMAKE_RC_COMPILER} /fo ${CMAKE_BINARY_DIR}/git.res ${CMAKE_BINARY_DIR}/git.rc
/d MICRO=${PROJECT_VERSION_PATCH} /d PATCHLEVEL=0 /d GIT_VERSION="${PROJECT_VERSION}.GIT" DEPENDS "${CMAKE_BINARY_DIR}/git.rc"
/fo ${CMAKE_BINARY_DIR}/git.res ${CMAKE_SOURCE_DIR}/git.rc
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
VERBATIM) VERBATIM)
endif() endif()

View File

@ -1,6 +1,6 @@
1 VERSIONINFO 1 VERSIONINFO
FILEVERSION MAJOR,MINOR,MICRO,PATCHLEVEL FILEVERSION @GIT_MAJOR_VERSION@,@GIT_MINOR_VERSION@,@GIT_MICRO_VERSION@,@GIT_PATCH_LEVEL@
PRODUCTVERSION MAJOR,MINOR,MICRO,PATCHLEVEL PRODUCTVERSION @GIT_MAJOR_VERSION@,@GIT_MINOR_VERSION@,@GIT_MICRO_VERSION@,@GIT_PATCH_LEVEL@
BEGIN BEGIN
BLOCK "StringFileInfo" BLOCK "StringFileInfo"
BEGIN BEGIN
@ -11,7 +11,7 @@ BEGIN
VALUE "InternalName", "git\0" VALUE "InternalName", "git\0"
VALUE "OriginalFilename", "git.exe\0" VALUE "OriginalFilename", "git.exe\0"
VALUE "ProductName", "Git\0" VALUE "ProductName", "Git\0"
VALUE "ProductVersion", GIT_VERSION "\0" VALUE "ProductVersion", "@GIT_VERSION@\0"
END END
END END