diff --git a/Makefile b/Makefile
index 38751928c4..ed4032a373 100644
--- a/Makefile
+++ b/Makefile
@@ -50,7 +50,7 @@ endif
include $(SRC_PATH)/rules.mak
-GENERATED_HEADERS = config-host.h qemu-options.def
+GENERATED_HEADERS = qemu-version.h config-host.h qemu-options.def
GENERATED_HEADERS += qmp-commands.h qapi-types.h qapi-visit.h qapi-event.h
GENERATED_SOURCES += qmp-marshal.c qapi-types.c qapi-visit.c qapi-event.c
GENERATED_HEADERS += qmp-introspect.h
@@ -167,6 +167,26 @@ endif
all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all modules
+qemu-version.h: FORCE
+ $(call quiet-command, \
+ (cd $(SRC_PATH); \
+ printf '#define QEMU_PKGVERSION '; \
+ if test -n "$(PKGVERSION)"; then \
+ printf '"$(PKGVERSION)"\n'; \
+ else \
+ if test -d .git; then \
+ printf '" ('; \
+ git describe --match 'v*' 2>/dev/null | tr -d '\n'; \
+ if ! git diff-index --quiet HEAD &>/dev/null; then \
+ printf -- '-dirty'; \
+ fi; \
+ printf ')"\n'; \
+ else \
+ printf '""\n'; \
+ fi; \
+ fi) > $@.tmp)
+ $(call quiet-command, cmp --quiet $@ $@.tmp || mv $@.tmp $@)
+
config-host.h: config-host.h-timestamp
config-host.h-timestamp: config-host.mak
qemu-options.def: $(SRC_PATH)/qemu-options.hx $(SRC_PATH)/scripts/hxtool
diff --git a/linux-user/main.c b/linux-user/main.c
index b2bc6ab2f7..8a11d0219e 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -17,6 +17,7 @@
* along with this program; if not, see .
*/
#include "qemu/osdep.h"
+#include "qemu-version.h"
#include
#include
#include
diff --git a/qemu-img.c b/qemu-img.c
index 4b56ad36aa..32e307c075 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -22,6 +22,7 @@
* THE SOFTWARE.
*/
#include "qemu/osdep.h"
+#include "qemu-version.h"
#include "qapi/error.h"
#include "qapi-visit.h"
#include "qapi/qmp-output-visitor.h"
diff --git a/qmp.c b/qmp.c
index 3165f8726b..7df6543871 100644
--- a/qmp.c
+++ b/qmp.c
@@ -14,6 +14,7 @@
*/
#include "qemu/osdep.h"
+#include "qemu-version.h"
#include "qemu/cutils.h"
#include "monitor/monitor.h"
#include "sysemu/sysemu.h"
diff --git a/scripts/create_config b/scripts/create_config
index b2d2ebb452..d47057b9d0 100755
--- a/scripts/create_config
+++ b/scripts/create_config
@@ -9,10 +9,6 @@ case $line in
version=${line#*=}
echo "#define QEMU_VERSION \"$version\""
;;
- PKGVERSION=*) # configuration
- pkgversion=${line#*=}
- echo "#define QEMU_PKGVERSION \"$pkgversion\""
- ;;
qemu_*dir=*) # qemu-specific directory configuration
name=${line%=*}
value=${line#*=}
diff --git a/vl.c b/vl.c
index 2f74fe8fb6..9539f92694 100644
--- a/vl.c
+++ b/vl.c
@@ -22,6 +22,7 @@
* THE SOFTWARE.
*/
#include "qemu/osdep.h"
+#include "qemu-version.h"
#include "qemu/cutils.h"
#include "qemu/help_option.h"