Move accel-$cpu_type registration into accel-cpu-target.c. System
targets whose variants share CPU_RESOLVING_TYPE (arm, riscv, sh4,
and xtensa) build that file once per TARGET_BASE_ARCH. User-mode
and every other system target compile it once per target.

test-multi-target checks that each target's accel parent exists
after QOM init.

Signed-off-by: Yonggang Luo <[email protected]>
---
 accel/accel-common.c           | 15 ---------------
 accel/accel-cpu-target.c       | 27 +++++++++++++++++++++++++++
 meson.build                    | 32 ++++++++++++++++++++++++++++++++
 tests/unit/test-multi-target.c | 10 ++++++++++
 4 files changed, 69 insertions(+), 15 deletions(-)
 create mode 100644 accel/accel-cpu-target.c

diff --git a/accel/accel-common.c b/accel/accel-common.c
index 00a400243f0..e7abec4a7b1 100644
--- a/accel/accel-common.c
+++ b/accel/accel-common.c
@@ -129,18 +129,3 @@ static const TypeInfo accel_types[] = {
 };
 
 DEFINE_TYPES(accel_types)
-
-static void register_accel_target_type(void)
-{
-    g_autofree char *name = g_strconcat("accel-", target_cpu_type(), NULL);
-    const TypeInfo accel_cpu_type = {
-        .name = name,
-        .parent = TYPE_OBJECT,
-        .abstract = true,
-        .class_size = sizeof(AccelCPUClass),
-    };
-
-    type_register_static(&accel_cpu_type);
-}
-
-type_init(register_accel_target_type);
diff --git a/accel/accel-cpu-target.c b/accel/accel-cpu-target.c
new file mode 100644
index 00000000000..ef4da5a2b61
--- /dev/null
+++ b/accel/accel-cpu-target.c
@@ -0,0 +1,27 @@
+/*
+ * Accelerator CPU parent registration.
+ *
+ * Compiled once per target. System targets for arm, riscv, sh4, and
+ * xtensa share one CPU_RESOLVING_TYPE, so those bases compile it once.
+ * The constructor is static, so more than one copy can be linked.
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/module.h"
+#include "accel/accel-cpu-target.h"
+
+static void register_accel_target_type(void)
+{
+    const TypeInfo accel_cpu_type = {
+        .name = TYPE_ACCEL_CPU,
+        .parent = TYPE_OBJECT,
+        .abstract = true,
+        .class_size = sizeof(AccelCPUClass),
+    };
+
+    type_register_static(&accel_cpu_type);
+}
+
+type_init(register_accel_target_type);
diff --git a/meson.build b/meson.build
index 7ec3fb90233..2b605b373fa 100644
--- a/meson.build
+++ b/meson.build
@@ -3243,6 +3243,11 @@ config_devices_h = {}
 config_target_h = {}
 config_target_mak = {}
 config_base_arch_mak = {}
+# System targets of these bases share one CPU_RESOLVING_TYPE.
+# i386/x86_64, ppc/ppc64, mips/mips64, and sparc/sparc64 do not.
+# Once they share one name too, drop this workaround.
+config_base_arch_cpu_type = ['arm', 'riscv', 'sh4', 'xtensa']
+config_base_arch_target = {}
 config_target_info = {}
 
 disassemblers = {
@@ -3443,6 +3448,10 @@ foreach target : target_dirs
   target_base_arch = config_target['TARGET_BASE_ARCH']
   config_base_arch = config_base_arch_mak.get(target_base_arch, {}) + 
config_target
   config_base_arch_mak += {target_base_arch: config_base_arch}
+  if (target.endswith('-softmmu') and
+      target_base_arch in config_base_arch_cpu_type)
+    config_base_arch_target += {target_base_arch: target}
+  endif
 endforeach
 target_dirs = actual_target_dirs
 
@@ -4150,6 +4159,7 @@ common_all = static_library('common',
 target_common_arch_libs = {}
 target_common_system_arch_libs = {}
 target_stubs_arch_libs = {}
+target_cpu_libs = {}
 foreach target_base_arch, config_base_arch : config_base_arch_mak
   target_inc = [include_directories('target' / target_base_arch)]
   inc = [common_user_inc + target_inc]
@@ -4219,6 +4229,23 @@ foreach target_base_arch, config_base_arch : 
config_base_arch_mak
                          c_args: target_system_c_args)
     target_stubs_arch_libs += {target_base_arch: lib}
   endif
+
+  if target_base_arch in config_base_arch_target
+    accel_target = config_base_arch_target[target_base_arch]
+    target_cpu_libs += {
+      target_base_arch: static_library(
+        'accel-cpu-type-' + target_base_arch,
+        files('accel/accel-cpu-target.c'),
+        config_target_h[accel_target],
+        genh,
+        build_by_default: false,
+        include_directories: inc,
+        c_args: ['-DCOMPILING_PER_TARGET',
+                 '-DCONFIG_TARGET="@[email protected]"'.format(accel_target),
+                 '-DTARGET_ARCH_PREFIX=target_' + target_base_arch.to_lower()],
+        dependencies: common_deps + system_deps)
+    }
+  endif
 endforeach
 
 if have_rust
@@ -4390,6 +4417,11 @@ foreach target : target_dirs
   target_common = common_ss.apply(config_target, strict: false)
   objects = [common_all.extract_objects(target_common.sources())]
   arch_deps += target_common.dependencies()
+  if target_type == 'system' and target_base_arch in target_cpu_libs
+    objects += 
target_cpu_libs[target_base_arch].extract_all_objects(recursive: false)
+  else
+    arch_srcs += files('accel/accel-cpu-target.c')
+  endif
   if target_base_arch in target_common_arch_libs
     src = target_common_arch[target_base_arch].apply(config_target, strict: 
false)
     lib = target_common_arch_libs[target_base_arch]
diff --git a/tests/unit/test-multi-target.c b/tests/unit/test-multi-target.c
index c7559827a08..0f0aff0d7d2 100644
--- a/tests/unit/test-multi-target.c
+++ b/tests/unit/test-multi-target.c
@@ -30,6 +30,13 @@
 /* stub for system/main.c */
 int (*qemu_main)(void);
 
+static void test_accel_cpu_parent(const TargetInfo *ti)
+{
+    g_autofree char *name = g_strconcat("accel-", ti->cpu_type, NULL);
+
+    g_assert_nonnull(object_class_by_name(name));
+}
+
 static void test_qom_types_are_unique(const void *data)
 {
     const TargetInfo *ti = data;
@@ -49,6 +56,9 @@ static void test_qom_types_are_unique(const void *data)
     module_call_init(MODULE_INIT_QOM);
     /* trigger type_initialize for all types */
     object_class_get_list("", false);
+
+    /* Accel parent lookup depends on the QOM types above being unique. */
+    test_accel_cpu_parent(ti);
 }
 
 int main(int argc, char **argv)
-- 
2.55.0.windows.3


Reply via email to