Commit ab78ac76f4 for qemu.org

commit ab78ac76f44a1241e76fd32d248df62d83f1ea58
Author: Paolo Bonzini <pbonzini@redhat.com>
Date:   Tue Oct 27 15:51:39 2026 +0100

    meson: rewrite scripts/qemu-version.sh directly in meson.build

    Invoking git and creating a .h file does not need a shell script.
    Embed the logic directly in meson.build.

    Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

diff --git a/include/qemu/version.h b/include/qemu/version.h
index 38405495c0..dd0f937f30 100644
--- a/include/qemu/version.h
+++ b/include/qemu/version.h
@@ -7,6 +7,14 @@
 #ifndef QEMU_VERSION_H
 #define QEMU_VERSION_H

+/* qemu-version.h defines QEMU_PKGVERSION only if it is not empty.  */
 #include "qemu-version.h"

+#ifdef QEMU_PKGVERSION
+#define QEMU_FULL_VERSION QEMU_VERSION " (" QEMU_PKGVERSION ")"
+#else
+#define QEMU_PKGVERSION ""
+#define QEMU_FULL_VERSION QEMU_VERSION
+#endif
+
 #endif
diff --git a/meson.build b/meson.build
index 71f135f053..0258744d06 100644
--- a/meson.build
+++ b/meson.build
@@ -78,6 +78,7 @@ have_user = have_linux_user or have_bsd_user
 ############

 sh = find_program('sh')
+git = find_program('git', required: false)
 python = import('python').find_installation()

 cc = meson.get_compiler('c')
@@ -3551,15 +3552,25 @@ tracetool_depends = files(
   'scripts/tracetool/__init__.py',
 )

-qemu_version_cmd = [find_program('scripts/qemu-version.sh'),
-                    meson.current_source_dir(),
-                    get_option('pkgversion'), meson.project_version()]
-qemu_version = custom_target('qemu-version.h',
-                             output: 'qemu-version.h',
-                             command: qemu_version_cmd,
-                             capture: true,
-                             build_by_default: true,
-                             build_always_stale: true)
+qemu_pkgversion = get_option('pkgversion')
+git_describe_version = [git, 'describe', '--match', 'v*', '--dirty']
+if qemu_pkgversion == '' and git.found() and fs.exists('.git') and \
+    run_command(git_describe_version, check: false).returncode() == 0
+  # Refresh the version at build time, so that "make" after a commit updates it.
+  qemu_version = vcs_tag(input: 'scripts/qemu-version.h.in',
+                         output: 'qemu-version.h',
+                         command: git_describe_version,
+                         fallback: '')
+else
+  # Unlike vcs_tag(), which always defines QEMU_PKGVERSION, leave it out if
+  # empty.  include/qemu/version.h then omits the parentheses from
+  # QEMU_FULL_VERSION.
+  qemu_version_data = configuration_data()
+  if qemu_pkgversion != ''
+    qemu_version_data.set_quoted('QEMU_PKGVERSION', qemu_pkgversion)
+  endif
+  qemu_version = configure_file(output: 'qemu-version.h', configuration: qemu_version_data)
+endif
 genh += qemu_version

 hxdep = []
diff --git a/scripts/qemu-version.h.in b/scripts/qemu-version.h.in
new file mode 100644
index 0000000000..b62d4c2265
--- /dev/null
+++ b/scripts/qemu-version.h.in
@@ -0,0 +1 @@
+#define QEMU_PKGVERSION "@VCS_TAG@"
diff --git a/scripts/qemu-version.sh b/scripts/qemu-version.sh
deleted file mode 100755
index 3f6e7e6d41..0000000000
--- a/scripts/qemu-version.sh
+++ /dev/null
@@ -1,25 +0,0 @@
-#!/bin/sh
-
-set -eu
-
-dir="$1"
-pkgversion="$2"
-version="$3"
-
-if [ -z "$pkgversion" ]; then
-    cd "$dir"
-    if [ -e .git ]; then
-        pkgversion=$(git describe --match 'v*' --dirty) || :
-    fi
-fi
-
-if [ -n "$pkgversion" ]; then
-    fullversion="$version ($pkgversion)"
-else
-    fullversion="$version"
-fi
-
-cat <<EOF
-#define QEMU_PKGVERSION "$pkgversion"
-#define QEMU_FULL_VERSION "$fullversion"
-EOF