new file mode 100644
@@ -0,0 +1,36 @@
+/*
+ * Utility function around QEMU release version
+ *
+ * Copyright (c) 2023 Linaro Ltd
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef QEMU_UTIL_VERSION_H
+#define QEMU_UTIL_VERSION_H
+
+/**
+ * qemu_version_delta - Return delta between two release versions ('A' and 'B').
+ * @version_major_a: Version 'A' major number
+ * @version_minor_a: Version 'A' minor number
+ * @version_major_b: Version 'B' major number
+ * @version_minor_b: Version 'B' minor number
+ *
+ * Returns a negative number is returned if 'A' is older than 'B', or positive
+ * if 'A' is newer than 'B'. The number represents the number of minor versions.
+ */
+int qemu_version_delta(unsigned version_major_a, unsigned version_minor_a,
+ unsigned version_major_b, unsigned version_minor_b);
+
+/**
+ * qemu_version_delta_current - Return delta with current QEMU release version.
+ * @version_major: The major version
+ * @version_minor: The minor version
+ *
+ * Returns the number of minor versions between the current released
+ * version and the requested $major.$minor. A negative number is returned
+ * for older versions and positive for newer.
+ */
+int qemu_version_delta_current(unsigned version_major, unsigned version_minor);
+
+#endif
@@ -58,6 +58,7 @@ util_ss.add(files('yank.c'))
util_ss.add(files('int128.c'))
util_ss.add(files('memalign.c'))
util_ss.add(files('interval-tree.c'))
+util_ss.add(files('qemu-version.c'))
if have_user
util_ss.add(files('selfmap.c'))
new file mode 100644
@@ -0,0 +1,37 @@
+/*
+ * Utility function around QEMU release version
+ *
+ * Copyright (c) 2023 Linaro Ltd
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/qemu-version.h"
+#include "config-host.h"
+
+#define QEMU_FIRST_MAJOR_VERSION_SUPPORTED 4
+#define QEMU_MINOR_VERSIONS_PER_MAJOR 3
+
+int qemu_version_delta(unsigned version_major_a, unsigned version_minor_a,
+ unsigned version_major_b, unsigned version_minor_b)
+{
+ int delta;
+
+ assert(version_major_a >= QEMU_FIRST_MAJOR_VERSION_SUPPORTED);
+ assert(version_major_b >= QEMU_FIRST_MAJOR_VERSION_SUPPORTED);
+ assert(version_minor_a < QEMU_MINOR_VERSIONS_PER_MAJOR);
+ assert(version_minor_b < QEMU_MINOR_VERSIONS_PER_MAJOR);
+
+ delta = version_major_b - version_major_a;
+ delta *= QEMU_MINOR_VERSIONS_PER_MAJOR;
+ delta += version_minor_b - version_minor_a;
+
+ return delta;
+}
+
+int qemu_version_delta_current(unsigned version_major, unsigned version_minor)
+{
+ return qemu_version_delta(QEMU_VERSION_MAJOR, QEMU_VERSION_MINOR,
+ version_major, version_minor);
+}
Add qemu_version_delta() to compare 2 QEMU versions, and qemu_version_delta_current() to compare with the current QEMU version. Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> --- include/qemu/qemu-version.h | 36 ++++++++++++++++++++++++++++++++++++ util/meson.build | 1 + util/qemu-version.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 include/qemu/qemu-version.h create mode 100644 util/qemu-version.c