diff mbox series

[RFC,3/4] util: Introduce helpers to compare QEMU versions

Message ID 20230109225419.22621-4-philmd@linaro.org
State New
Headers show
Series qom: Introduce object_class_property_deprecate() | expand

Commit Message

Philippe Mathieu-Daudé Jan. 9, 2023, 10:54 p.m. UTC
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
diff mbox series

Patch

diff --git a/include/qemu/qemu-version.h b/include/qemu/qemu-version.h
new file mode 100644
index 0000000000..c9274bfaf0
--- /dev/null
+++ b/include/qemu/qemu-version.h
@@ -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
diff --git a/util/meson.build b/util/meson.build
index d8d109ff84..655debeec1 100644
--- a/util/meson.build
+++ b/util/meson.build
@@ -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'))
diff --git a/util/qemu-version.c b/util/qemu-version.c
new file mode 100644
index 0000000000..d409a6e574
--- /dev/null
+++ b/util/qemu-version.c
@@ -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);
+}