From 90fd5171f8d2ea48e98ed452b5f2161a17db1747 Mon Sep 17 00:00:00 2001 From: Ryusuke Konishi Date: Mon, 26 Jan 2026 01:44:10 +0900 Subject: [PATCH] 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 '. 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 --- configure.ac | 12 +++++++++++- include/util.h | 11 +++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 25ef8fa..6e49aaa 100644 --- a/configure.ac +++ b/configure.ac @@ -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 diff --git a/include/util.h b/include/util.h index 0edd421..c87c2e7 100644 --- a/include/util.h +++ b/include/util.h @@ -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; \