Skip to content

Commit 0c8d339

Browse files
pks-tgitster
authored andcommitted
Makefile: propagate Git version via generated header
We set up a couple of preprocessor macros when compiling Git that propagate the version that Git was built from to `git version` et al. The way this is set up makes it harder than necessary to reuse the infrastructure across the different build systems. Refactor this such that we generate a "version-def.h" header via `GIT-VERSION-GEN` instead. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4838dea commit 0c8d339

File tree

6 files changed

+35
-11
lines changed

6 files changed

+35
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@
195195
/config-list.h
196196
/command-list.h
197197
/hook-list.h
198+
/version-def.h
198199
*.tar.gz
199200
*.dsc
200201
*.deb

GIT-VERSION-GEN

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ else
5252
fi
5353

5454
GIT_VERSION=$(expr "$VN" : v*'\(.*\)')
55+
GIT_BUILT_FROM_COMMIT=$(git -C "$SOURCE_DIR" rev-parse -q --verify HEAD 2>/dev/null)
56+
if test -z "$GIT_USER_AGENT"
57+
then
58+
GIT_USER_AGENT="git/$GIT_VERSION"
59+
fi
5560

5661
read GIT_MAJOR_VERSION GIT_MINOR_VERSION GIT_MICRO_VERSION trailing <<EOF
5762
$(echo "$GIT_VERSION" 0 0 0 | tr '.a-zA-Z-' ' ')
@@ -61,6 +66,8 @@ sed -e "s|@GIT_VERSION@|$GIT_VERSION|" \
6166
-e "s|@GIT_MAJOR_VERSION@|$GIT_MAJOR_VERSION|" \
6267
-e "s|@GIT_MINOR_VERSION@|$GIT_MINOR_VERSION|" \
6368
-e "s|@GIT_MICRO_VERSION@|$GIT_MICRO_VERSION|" \
69+
-e "s|@GIT_BUILT_FROM_COMMIT@|$GIT_BUILT_FROM_COMMIT|" \
70+
-e "s|@GIT_USER_AGENT@|$GIT_USER_AGENT|" \
6471
"$INPUT" >"$OUTPUT"+
6572

6673
if ! test -f "$OUTPUT" || ! cmp "$OUTPUT"+ "$OUTPUT" >/dev/null

Makefile

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,13 +2508,11 @@ PAGER_ENV_CQ_SQ = $(subst ','\'',$(PAGER_ENV_CQ))
25082508
pager.sp pager.s pager.o: EXTRA_CPPFLAGS = \
25092509
-DPAGER_ENV='$(PAGER_ENV_CQ_SQ)'
25102510

2511-
version.sp version.s version.o: GIT-VERSION-FILE GIT-USER-AGENT
2512-
version.sp version.s version.o: EXTRA_CPPFLAGS = \
2513-
'-DGIT_VERSION="$(GIT_VERSION)"' \
2514-
'-DGIT_USER_AGENT=$(GIT_USER_AGENT_CQ_SQ)' \
2515-
'-DGIT_BUILT_FROM_COMMIT="$(shell \
2516-
GIT_CEILING_DIRECTORIES="$(CURDIR)/.." \
2517-
git rev-parse -q --verify HEAD 2>/dev/null)"'
2511+
version-def.h: version-def.h.in GIT-VERSION-GEN GIT-VERSION-FILE GIT-USER-AGENT
2512+
$(QUIET_GEN)GIT_USER_AGENT="$(GIT_USER_AGENT)" $(SHELL_PATH) ./GIT-VERSION-GEN "$(shell pwd)" $< $@+
2513+
@if cmp $@+ $@ >/dev/null 2>&1; then $(RM) $@+; else mv $@+ $@; fi
2514+
2515+
version.sp version.s version.o: version-def.h
25182516

25192517
$(BUILT_INS): git$X
25202518
$(QUIET_BUILT_IN)$(RM) $@ && \
@@ -3728,6 +3726,7 @@ clean: profile-clean coverage-clean cocciclean
37283726
$(RM) $(FUZZ_PROGRAMS)
37293727
$(RM) $(SP_OBJ)
37303728
$(RM) $(HCC)
3729+
$(RM) version-def.h
37313730
$(RM) -r bin-wrappers $(dep_dirs) $(compdb_dir) compile_commands.json
37323731
$(RM) -r po/build/
37333732
$(RM) *.pyc *.pyo */*.pyc */*.pyo $(GENERATED_H) $(ETAGS_TARGET) tags cscope*

contrib/buildsystems/CMakeLists.txt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,7 @@ add_compile_definitions(PAGER_ENV="LESS=FRX LV=-c"
229229
GIT_HTML_PATH="share/doc/git-doc"
230230
DEFAULT_HELP_FORMAT="html"
231231
DEFAULT_GIT_TEMPLATE_DIR="share/git-core/templates"
232-
GIT_VERSION="${PROJECT_VERSION}.GIT"
233-
GIT_USER_AGENT="git/${PROJECT_VERSION}.GIT"
234-
BINDIR="bin"
235-
GIT_BUILT_FROM_COMMIT="")
232+
BINDIR="bin")
236233

237234
if(WIN32)
238235
set(FALLBACK_RUNTIME_PREFIX /mingw64)
@@ -668,6 +665,17 @@ parse_makefile_for_sources(libgit_SOURCES "LIB_OBJS")
668665

669666
list(TRANSFORM libgit_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
670667
list(TRANSFORM compat_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
668+
669+
add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/version-def.h"
670+
COMMAND "${SH_EXE}" "${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN"
671+
"${CMAKE_SOURCE_DIR}"
672+
"${CMAKE_SOURCE_DIR}/version-def.h.in"
673+
"${CMAKE_BINARY_DIR}/version-def.h"
674+
DEPENDS "${SH_EXE}" "${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN"
675+
"${CMAKE_SOURCE_DIR}/version-def.h.in"
676+
VERBATIM)
677+
list(APPEND libgit_SOURCES "${CMAKE_BINARY_DIR}/version-def.h")
678+
671679
add_library(libgit ${libgit_SOURCES} ${compat_SOURCES})
672680

673681
#libxdiff

version-def.h.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef VERSION_DEF_H
2+
#define VERSION_DEF_H
3+
4+
#define GIT_VERSION "@GIT_VERSION@"
5+
#define GIT_BUILT_FROM_COMMIT "@GIT_BUILT_FROM_COMMIT@"
6+
#define GIT_USER_AGENT "@GIT_USER_AGENT@"
7+
8+
#endif /* VERSION_DEF_H */

version.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "git-compat-util.h"
22
#include "version.h"
3+
#include "version-def.h"
34
#include "strbuf.h"
45

56
const char git_version_string[] = GIT_VERSION;

0 commit comments

Comments
 (0)