[PATCH] [PATCH v2] Allow building qemu tools on 32-bit hosts

Helge Deller posted 1 patch 1 week ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20260404061215.505913-1-deller@kernel.org
Maintainers: Paolo Bonzini <pbonzini@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>
There is a newer version of this series
include/qemu/atomic.h |  6 +++---
meson.build           | 16 ++++++++++++----
2 files changed, 15 insertions(+), 7 deletions(-)
[PATCH] [PATCH v2] Allow building qemu tools on 32-bit hosts
Posted by Helge Deller 1 week ago
From: Helge Deller <deller@gmx.de>

Qemu's tools like qemu-img are often needed on 32-bit platforms,
although the actual qemu emulators have been discontinued on 32-bit.

To allow building the tools on 32-bit this patch implements three small
changes:

a) The check in meson.build is changed to still error out if the user
tries to build qemu-system or qemu-user on a 32-bit platform, but allows
building tools (e.g. by "--enable-tools") alone.

b) The compile time check in atomic.h now checks against
sizeof(uint64_t) so that 32-bit environments can still build
successfully, while 128-bit atomic operations are prevented to sneak in.

c) Allow linking against libatomic as long as we don't build the
qemu-system and qemu-user binaries.

Sucessfully tested on the 32-bit big-endian powerpc architecture.

v2:
- keep comple time checks in atomic.h
- allow linking against libatomic when building tools

Signed-off-by: Helge Deller <deller@gmx.de>
Siggested-by: Richard Henderson <richard.henderson@linaro.org>
---
 include/qemu/atomic.h |  6 +++---
 meson.build           | 16 ++++++++++++----
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/include/qemu/atomic.h b/include/qemu/atomic.h
index dc9290084b..e4dd948863 100644
--- a/include/qemu/atomic.h
+++ b/include/qemu/atomic.h
@@ -58,11 +58,11 @@
 
 /*
  * Sanity check that the size of an atomic operation isn't "overly large".
- * Despite the fact that e.g. i686 has 64-bit atomic operations, we do not
+ * Despite the fact that e.g. x86-64 has 128-bit atomic operations, we do not
  * want to use them because we ought not need them, and this lets us do a
- * bit of sanity checking that other 32-bit hosts might build.
+ * bit of sanity checking that other 32- and 64-bit hosts might build.
  */
-#define ATOMIC_REG_SIZE  sizeof(void *)
+#define ATOMIC_REG_SIZE  sizeof(uint64_t)
 
 /* Weak atomic operations prevent the compiler moving other
  * loads/stores past the atomic operation load/store. However there is
diff --git a/meson.build b/meson.build
index daa58e46a3..8b0f00608d 100644
--- a/meson.build
+++ b/meson.build
@@ -323,8 +323,8 @@ endif
 # Compiler flags #
 ##################
 
-if cc.sizeof('void *') < 8
-  error('QEMU requires a 64-bit CPU host architecture')
+if (cc.sizeof('void *') < 8) and (have_system or have_user)
+  error('QEMU emulator requires a 64-bit CPU host architecture. Only tools may be built for 32-bit.')
 endif
 
 foreach lang : all_languages
@@ -423,8 +423,16 @@ endif
 #
 # Later checks assume we won't get atomic ops for int128 without
 # explicitly asking for -latomic, so we must disable GCC's new
-# automatic linking with the new -fno-link-libatomic flag
-qemu_isa_flags += cc.get_supported_arguments('-fno-link-libatomic')
+# automatic linking with the new -fno-link-libatomic flag.
+#
+# When not building the emulators, but qemu helper tools only
+# (e.g. on 32-bit hosts), libatomic is sometimes required and
+# thus acceptable.
+if (have_system or have_user)
+  qemu_isa_flags += cc.get_supported_arguments('-fno-link-libatomic')
+else
+  qemu_ldflags += cc.get_supported_link_arguments('-Wl,-latomic')
+endif
 
 qemu_common_flags = qemu_isa_flags + qemu_common_flags
 
-- 
2.53.0
Re: [PATCH] [PATCH v2] Allow building qemu tools on 32-bit hosts
Posted by Richard Henderson 6 days, 18 hours ago
On 4/4/26 17:12, Helge Deller wrote:
> +  qemu_ldflags += cc.get_supported_link_arguments('-Wl,-latomic')

You don't need "-Wl".

Otherwise,
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>


r~
Re: [PATCH] [PATCH v2] Allow building qemu tools on 32-bit hosts
Posted by Michael Tokarev 1 week ago
On 04.04.2026 09:12, Helge Deller wrote:
> From: Helge Deller <deller@gmx.de>
> 
> Qemu's tools like qemu-img are often needed on 32-bit platforms,
> although the actual qemu emulators have been discontinued on 32-bit.
> 
> To allow building the tools on 32-bit this patch implements three small
> changes:
> 
> a) The check in meson.build is changed to still error out if the user
> tries to build qemu-system or qemu-user on a 32-bit platform, but allows
> building tools (e.g. by "--enable-tools") alone.
> 
> b) The compile time check in atomic.h now checks against
> sizeof(uint64_t) so that 32-bit environments can still build
> successfully, while 128-bit atomic operations are prevented to sneak in.
> 
> c) Allow linking against libatomic as long as we don't build the
> qemu-system and qemu-user binaries.
> 
> Sucessfully tested on the 32-bit big-endian powerpc architecture.

On powerpc, util/meson.build tweak is also needed, - did you use it?

> v2:
> - keep comple time checks in atomic.h
> - allow linking against libatomic when building tools
> 
> Signed-off-by: Helge Deller <deller@gmx.de>
> Siggested-by: Richard Henderson <richard.henderson@linaro.org>
   ^^^ Suggested

Reviewed-by: Michael Tokarev <mjt@tls.msk.ru>

Yes this makes sense too, and it looks like a good long-term solution.

Thank you again!

/mjt
Re: [PATCH] [PATCH v2] Allow building qemu tools on 32-bit hosts
Posted by Helge Deller 1 week ago
On 4/4/26 09:07, Michael Tokarev wrote:
> On 04.04.2026 09:12, Helge Deller wrote:
>> From: Helge Deller <deller@gmx.de>
>>
>> Qemu's tools like qemu-img are often needed on 32-bit platforms,
>> although the actual qemu emulators have been discontinued on 32-bit.
>>
>> To allow building the tools on 32-bit this patch implements three small
>> changes:
>>
>> a) The check in meson.build is changed to still error out if the user
>> tries to build qemu-system or qemu-user on a 32-bit platform, but allows
>> building tools (e.g. by "--enable-tools") alone.
>>
>> b) The compile time check in atomic.h now checks against
>> sizeof(uint64_t) so that 32-bit environments can still build
>> successfully, while 128-bit atomic operations are prevented to sneak in.
>>
>> c) Allow linking against libatomic as long as we don't build the
>> qemu-system and qemu-user binaries.
>>
>> Sucessfully tested on the 32-bit big-endian powerpc architecture.
> 
> On powerpc, util/meson.build tweak is also needed, - did you use it?

Yes, it was needed.
I used all your patches you sent in the series, but replaced
my patch with the new v2 version.

Helge