build: add support for embedding git revision

Add a mechanism to embed the git revision into the compiled binaries.
This allows users and developers to identify the exact source commit
used for the build, which aids in debugging and tracking versions.

The git revision is obtained using 'git describe' and stored in the
.comment ELF section using the new NILFS_UTILS_GITID() macro defined
in util.h.  This section is not loaded into memory at runtime.  The
embedded revision can be checked using 'readelf -p .comment <binary>'.

This feature is opt-in to ensure deterministic builds for package
maintainers.  It is enabled only if the environment variable
NILFS_UTILS_USE_GITID is set to 1, true, on, or yes when generating
the configure script (e.g. by running autoconf or autogen.sh).

Also, bump the minimum Autoconf version to 2.64 to utilize m4_esyscmd_s.

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
This commit is contained in:
Ryusuke Konishi 2026-01-26 01:44:10 +09:00
parent 80274ede69
commit 90fd5171f8
2 changed files with 22 additions and 1 deletions

View File

@ -5,13 +5,23 @@ dnl Copyright (C) 2007-2012 Nippon Telegraph and Telephone Corporation.
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.60)
AC_PREREQ(2.64)
AC_INIT([NILFS utils],[2.4.0-dev],[linux-nilfs@vger.kernel.org])
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([config.h.in])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIR([m4])
dnl GIT ID embedding logic
m4_define([nilfs_utils_git_rev],
m4_esyscmd_s([case "$NILFS_UTILS_USE_GITID" in 1|[tT]*|[oO]n|[yY]*) \
git describe --tags --always --dirty 2>/dev/null ;; esac]))
dnl Emit to config.h if not empty
m4_if(nilfs_utils_git_rev, [], [],
[AC_DEFINE_UNQUOTED([NILFS_UTILS_GIT_REVISION], ["nilfs_utils_git_rev"],
[Git revision])])
# Checks for programs.
AC_PROG_CC
AM_PROG_CC_C_O

View File

@ -22,6 +22,17 @@
/* Force a compilation error if the condition is true */
#define BUILD_BUG_ON(condition) ((void)sizeof(struct { int: -!!(condition); }))
/* Macro to emit a git revision string into binaries */
#if defined(NILFS_UTILS_GIT_REVISION) && defined(__GNUC__)
#define NILFS_UTILS_GITID() \
__asm__( \
".section .comment\n\t" \
".string \"nilfs-utils git: " NILFS_UTILS_GIT_REVISION "\"\n\t" \
".previous")
#else
#define NILFS_UTILS_GITID() /* muted */
#endif
#define typecheck(type, x) \
({ \
type __dummy; \