Add a 'targets' field to MachineInfo which is populated with
the list of targets a machine is declard to be compatible
with.

To avoid reporting redundant information for the traditional
per-target emulator binaries, this new field is only emitted
if no target is currently selected, or the 'all-targets'
flag is explicitly set.

Signed-off-by: Daniel P. Berrangé <[email protected]>
---
 hw/core/machine-qmp-cmds.c | 18 ++++++++++++++++++
 qapi/machine.json          | 12 +++++++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/hw/core/machine-qmp-cmds.c b/hw/core/machine-qmp-cmds.c
index 3f06ce8c75f..39624e91139 100644
--- a/hw/core/machine-qmp-cmds.c
+++ b/hw/core/machine-qmp-cmds.c
@@ -35,6 +35,7 @@
 #include "hw/s390x/storage-keys.h"
 #include "qom/object_interfaces.h"
 #include "qemu/target-info-def.h"
+#include "qemu/base-arch-defs.h"
 
 /*
  * QMP query for enabled and present accelerators
@@ -155,11 +156,16 @@ qmp_query_cpu_model_expansion(CpuModelExpansionType type,
 }
 
 MachineInfoList *qmp_query_machines(bool has_compat_props, bool compat_props,
+                                    bool has_all_targets, bool all_targets,
                                     Error **errp)
 {
     GSList *el, *machines;
     MachineInfoList *mach_list = NULL;
 
+    if (target_mask_selected() == QEMU_ARCH_ALL) {
+        all_targets = true;
+    }
+
     machines = object_class_get_list(TYPE_MACHINE, false);
     for (el = machines; el; el = el->next) {
         ObjectClass *oc = el->data;
@@ -167,6 +173,7 @@ MachineInfoList *qmp_query_machines(bool has_compat_props, 
bool compat_props,
         const char *default_cpu_type = machine_class_default_cpu_type(mc);
         MachineInfo *info;
         AliasedClass *alc = ALIASED_CLASS(oc);
+        TargetSpecificClass *tsc = TARGET_SPECIFIC_CLASS(oc);
 
         /*
          * If we've selected a specific target, then filter
@@ -202,6 +209,17 @@ MachineInfoList *qmp_query_machines(bool has_compat_props, 
bool compat_props,
             info->alias = g_strndup(alc->aliases[0], len);
         }
 
+        if (tsc->targets && all_targets) {
+            SysEmuTargetList **targets = &info->targets;
+            size_t i;
+            info->has_targets = true;
+            for (i = 0; i < SYS_EMU_TARGET__MAX; i++) {
+                if (tsc->targets & QEMU_ARCH_BIT(i)) {
+                    QAPI_LIST_APPEND(targets, i);
+                }
+            }
+        }
+
         info->name = g_strdup(mc->name);
         info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
         info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
diff --git a/qapi/machine.json b/qapi/machine.json
index 40fda705462..c3b3cce7e48 100644
--- a/qapi/machine.json
+++ b/qapi/machine.json
@@ -165,6 +165,11 @@
 #
 # @aliases: a list of aliases for the machine type name (since 12.0)
 #
+# @targets: a list of compatible targets for the machine (since 12.0)
+#      This is only reported if no target is currently selected
+#      for the binary, or if the `query-machines` argument
+#      @all-targets is true.
+#
 # @is-default: whether the machine is default
 #
 # @cpu-max: maximum number of CPUs supported by the machine type
@@ -207,6 +212,7 @@
   'data': { 'name': 'str',
             '*alias': { 'type': 'str', 'features': ['deprecated'] },
             '*aliases': ['str'],
+            '*targets': ['SysEmuTarget'],
             '*is-default': 'bool', 'cpu-max': 'int',
             'hotpluggable-cpus': 'bool',  'numa-mem-supported': 'bool',
             'deprecated': 'bool', '*default-cpu-type': 'str',
@@ -222,6 +228,9 @@
 # @compat-props: if true, also return compatibility properties.
 #     (default: false) (since 9.1)
 #
+# @all-targets: if true, report all available targets even
+#     when a target is selected (default: false) (since 12.0)
+#
 # Features:
 #
 # @unstable: Argument @compat-props is experimental.
@@ -253,7 +262,8 @@
 ##
 { 'command': 'query-machines',
   'data': { '*compat-props': { 'type': 'bool',
-                               'features': [ 'unstable' ] } },
+                               'features': [ 'unstable' ] },
+            '*all-targets': 'bool' },
   'returns': ['MachineInfo'] }
 
 ##
-- 
2.56.1


Reply via email to