Re: [PATCH RFC v6 08/19] Makefile: refactor GIT-VERSION-GEN to be reusable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Patrick

On 12/11/2024 17:02, Patrick Steinhardt wrote:
Our "GIT-VERSION-GEN" script always writes the "GIT-VERSION-FILE" into
the current directory, where the expectation is that it should exist in
the source directory. But other build systems that support out-of-tree
builds may not want to do that to keep the source directory pristine,
even though CMake currently doesn't care.

Refactor the script such that it doesn't write output to a file anymore
but so that it instead writes the version to stdout. This makes it
easier to compute the same version as our Makefile would without having
to write the "GIT-VERSION-FILE".

This patch moves the logic that only updates GIT-VERSION-FILE if the
version has changed from the script into the Makefile. As we really want
the CMake and meson builds to set the string at build time which
probably means extending GIT-VERSION-GEN to write a header file that
defines GIT_VERSION etc. I'm not sure this is a good direction. In the
long run I think we'd be better off with something like the patch below.

Best Wishes

Phillip

---- >8 ----
From: Phillip Wood <phillip.wood@xxxxxxxxxxxxx>
Subject: [PATCH] WIP CMake: update GIT_VERSION at runtime

The CMake build defines GIT_VERSION when it is configured and does not
update it when HEAD changes. Fix this by modifying GIT-VERSION-GEN so
that in addition to creating GIT_VERSION-FILE, it creates a header file
containing the version strings that is included in version.c.

TODO:
  - update CMakeLists.txt to set the version information for git.res
    at build time.
  - Check for changes to GIT_USER_AGENT when the version has not
    changed.
  - Maybe remove GIT-VERISON-FILE infavor of parsing version-def.h so
    that there is a single source of truth for the version.

Signed-off-by: Phillip Wood <phillip.wood@xxxxxxxxxxxxx>
---
 GIT-VERSION-GEN                     | 17 +++++++++++++----
 Makefile                            | 15 +++++++--------
 contrib/buildsystems/CMakeLists.txt | 26 +++++++++++++++++++-------
 version.c                           |  1 +
 4 files changed, 40 insertions(+), 19 deletions(-)

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index 78e8631f677..25c033fa892 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -1,6 +1,7 @@
 #!/bin/sh
-GVF=GIT-VERSION-FILE
+GVF="$1"
+VERSION_HEADER="$2"
 DEF_VER=v2.47.GIT
LF='
@@ -30,11 +31,19 @@ VN=$(expr "$VN" : v*'\(.*\)')
if test -r $GVF
 then
-	VC=$(sed -e 's/^GIT_VERSION = //' <$GVF)
+	VC=$(sed -e 's/^GIT_VERSION = //' "$GVF")
 else
 	VC=unset
 fi
-test "$VN" = "$VC" || {
+test "$VN" = "$VC" && test -f "$VERSION_HEADER" || {
 	echo >&2 "GIT_VERSION = $VN"
-	echo "GIT_VERSION = $VN" >$GVF
+	echo "GIT_VERSION = $VN" >"$GVF"
+	USER_AGENT="${GIT_USER_AGENT:-git/$VN}"
+	HEAD="$(GIT_CEILING_DIRECTORIES=.. \
+		git rev-parse -q --verify HEAD 2>/dev/null)"
+	cat <<-EOF >"$VERSION_HEADER"
+	#define GIT_VERSION "$VN"
+	#define GIT_BUILT_FROM_COMMIT "$HEAD"
+	#define GIT_USER_AGENT "$USER_AGENT"
+	EOF
 }
diff --git a/Makefile b/Makefile
index 2afad000762..7cd42e2bed7 100644
--- a/Makefile
+++ b/Makefile
@@ -592,7 +592,11 @@ include shared.mak
 #        Disable -pedantic compilation.
GIT-VERSION-FILE: FORCE
-	@$(SHELL_PATH) ./GIT-VERSION-GEN
+	@$(SHELL_PATH) ./GIT-VERSION-GEN GIT-VERSION-FILE version-def.h
+
+version-def.h: GIT-VERSION-FILE
+	@touch $@
+
 -include GIT-VERSION-FILE
# Set our default configuration.
@@ -919,6 +923,7 @@ REFTABLE_LIB = reftable/libreftable.a
 GENERATED_H += command-list.h
 GENERATED_H += config-list.h
 GENERATED_H += hook-list.h
+GENERATED_H += version-def.h
 GENERATED_H += $(UNIT_TEST_DIR)/clar-decls.h
 GENERATED_H += $(UNIT_TEST_DIR)/clar.suite
@@ -2505,13 +2510,7 @@ PAGER_ENV_CQ_SQ = $(subst ','\'',$(PAGER_ENV_CQ))
 pager.sp pager.s pager.o: EXTRA_CPPFLAGS = \
 	-DPAGER_ENV='$(PAGER_ENV_CQ_SQ)'
-version.sp version.s version.o: GIT-VERSION-FILE GIT-USER-AGENT
-version.sp version.s version.o: EXTRA_CPPFLAGS = \
-	'-DGIT_VERSION="$(GIT_VERSION)"' \
-	'-DGIT_USER_AGENT=$(GIT_USER_AGENT_CQ_SQ)' \
-	'-DGIT_BUILT_FROM_COMMIT="$(shell \
-		GIT_CEILING_DIRECTORIES="$(CURDIR)/.." \
-		git rev-parse -q --verify HEAD 2>/dev/null)"'
+version.sp version.s version.o: version-def.h GIT-USER-AGENT
$(BUILT_INS): git$X
 	$(QUIET_BUILT_IN)$(RM) $@ && \
diff --git a/contrib/buildsystems/CMakeLists.txt b/contrib/buildsystems/CMakeLists.txt
index 2e22e87d188..e64045c4dbd 100644
--- a/contrib/buildsystems/CMakeLists.txt
+++ b/contrib/buildsystems/CMakeLists.txt
@@ -86,12 +86,15 @@ endif()
 #Create GIT-VERSION-FILE using GIT-VERSION-GEN
 if(NOT EXISTS ${CMAKE_SOURCE_DIR}/GIT-VERSION-FILE)
 	message("Generating GIT-VERSION-FILE")
-	execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN
-		WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
+	execute_process(COMMAND ${SH_EXE}
+				"${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN"
+				"${CMAKE_BINARY_DIR}/GIT-VERSION-FILE"
+				"${CMAKE_BINARY_DIR}/version-def.h"
+			WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
 endif()
#Parse GIT-VERSION-FILE to get the version
-file(STRINGS ${CMAKE_SOURCE_DIR}/GIT-VERSION-FILE git_version REGEX "GIT_VERSION = (.*)")
+file(STRINGS "${CMAKE_BINARY_DIR}/GIT-VERSION-FILE" git_version REGEX "^GIT_VERSION = (.*)")
 string(REPLACE "GIT_VERSION = " "" git_version ${git_version})
 string(FIND ${git_version} "GIT" location)
 if(location EQUAL -1)
@@ -101,6 +104,16 @@ else()
 	string(APPEND git_version ".0") #for building from a snapshot
 endif()
+add_custom_target(version-def
+	COMMAND ${SH_EXE}
+		./GIT-VERSION-GEN
+		"${CMAKE_BINARY_DIR}/GIT-VERSION-FILE"
+		"${CMAKE_BINARY_DIR}/version-def.h"
+	WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
+	BYPRODUCTS "${CMAKE_BINARY_DIR}/GIT-VERSION-FILE"
+		-  "${CMAKE_BINARY_DIR}/version-def.h"
+	VERBATIM)
+
 project(git
 	VERSION ${git_version}
 	LANGUAGES C)
@@ -240,10 +253,7 @@ add_compile_definitions(PAGER_ENV="LESS=FRX LV=-c"
 			GIT_HTML_PATH="share/doc/git-doc"
 			DEFAULT_HELP_FORMAT="html"
 			DEFAULT_GIT_TEMPLATE_DIR="share/git-core/templates"
-			GIT_VERSION="${PROJECT_VERSION}.GIT"
-			GIT_USER_AGENT="git/${PROJECT_VERSION}.GIT"
-			BINDIR="bin"
-			GIT_BUILT_FROM_COMMIT="")
+			BINDIR="bin")
if(WIN32)
 	set(FALLBACK_RUNTIME_PREFIX /mingw64)
@@ -680,6 +690,8 @@ parse_makefile_for_sources(libgit_SOURCES "LIB_OBJS")
 list(TRANSFORM libgit_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
 list(TRANSFORM compat_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
 add_library(libgit ${libgit_SOURCES} ${compat_SOURCES})
+target_include_directories(libgit PRIVATE "${CMAKE_BINARY_DIR}")
+add_dependencies(libgit version-def)
#libxdiff
 parse_makefile_for_sources(libxdiff_SOURCES "XDIFF_OBJS")
diff --git a/version.c b/version.c
index 41b718c29e1..7adc4d51ff2 100644
--- a/version.c
+++ b/version.c
@@ -1,5 +1,6 @@
 #include "git-compat-util.h"
 #include "version.h"
+#include "version-def.h"
 #include "strbuf.h"
const char git_version_string[] = GIT_VERSION;
--
2.47.0.354.g52c9d0440b0.dirty









[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux