diff mbox series

[meta-oe,12/29] rtorrent: Fix libatomic detection with optimized builds

Message ID 20261023065350.3228996-12-khem.raj@oss.qualcomm.com
State New
Headers show
Series [meta-oe,01/29] castxml: upgrade 0.7.0 -> 0.8.0 | expand

Commit Message

Khem Raj Oct. 23, 2026, 6:53 a.m. UTC
The configure check for -latomic links a program that loads from a
local std::atomic<uint64_t>, which clang folds away at -O2. On riscv32
it therefore reports that libatomic is not needed, and linking rtorrent
fails:

  ld.lld: error: undefined symbol: __atomic_load_8

Add a patch that makes the test atomic global and modifies it with
fetch_add() so the check cannot be optimized away.

AI-Generated: Uses Claude Code
Signed-off-by: Khem Raj <khem.raj@oss.qualcomm.com>
---
 ...e-atomic-test-program-from-being-opt.patch | 57 +++++++++++++++++++
 .../rtorrent/rtorrent_0.16.25.bb              |  4 +-
 2 files changed, 60 insertions(+), 1 deletion(-)
 create mode 100644 meta-oe/recipes-connectivity/rtorrent/rtorrent/0001-checks.m4-Keep-the-atomic-test-program-from-being-opt.patch
diff mbox series

Patch

diff --git a/meta-oe/recipes-connectivity/rtorrent/rtorrent/0001-checks.m4-Keep-the-atomic-test-program-from-being-opt.patch b/meta-oe/recipes-connectivity/rtorrent/rtorrent/0001-checks.m4-Keep-the-atomic-test-program-from-being-opt.patch
new file mode 100644
index 0000000000..5f18c63c2c
--- /dev/null
+++ b/meta-oe/recipes-connectivity/rtorrent/rtorrent/0001-checks.m4-Keep-the-atomic-test-program-from-being-opt.patch
@@ -0,0 +1,57 @@ 
+From: Khem Raj <khem.raj@oss.qualcomm.com>
+Date: Wed, 21 Oct 2026 13:00:00 -0700
+Subject: [PATCH] checks.m4: Keep the atomic test program from being optimized
+ away
+
+TORRENT_CHECK_ATOMIC decides whether -latomic is needed by linking a
+program that loads from a local std::atomic<uint64_t>. The atomic never
+escapes, so with -O2 in CXXFLAGS clang folds the load to a constant and
+no __atomic_* call is emitted. The check then reports that -latomic is
+not required even on targets that need it for 64-bit atomics, such as
+riscv32, and linking fails later:
+
+  ld.lld: error: undefined symbol: __atomic_load_8
+
+Make the atomic global and modify it with fetch_add() so the atomic
+operations are emitted at any optimization level.
+
+Upstream-Status: Submitted [https://github.com/rakshasa/rtorrent/pull/2072]
+Signed-off-by: Khem Raj <khem.raj@oss.qualcomm.com>
+---
+ scripts/checks.m4 | 13 +++++++++----
+ 1 file changed, 9 insertions(+), 4 deletions(-)
+
+--- a/scripts/checks.m4
++++ b/scripts/checks.m4
+@@ -99,13 +99,17 @@
+ ])
+
+
++# The atomic is global and modified with a read-modify-write operation,
++# otherwise the compiler can optimize the operations away and the program
++# links without libatomic even on targets that need it.
+ AC_DEFUN([TORRENT_CHECK_ATOMIC], [
+   AC_MSG_CHECKING([whether 64-bit atomic operations require -latomic])
+   AC_LANG_PUSH(C++)
+
+   AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <cstdint>
+-                                    #include <atomic>]],
+-                                    [[std::atomic<uint64_t> x(0);
++                                    #include <atomic>
++                                    std::atomic<uint64_t> x(0);]],
++                                    [[x.fetch_add(1);
+                                     return x.load();]])],
+     [
+       AC_MSG_RESULT([no])
+@@ -116,8 +120,9 @@
+       LIBS="$LIBS -latomic"
+
+       AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <cstdint>
+-                                        #include <atomic>]],
+-                                        [[std::atomic<uint64_t> x(0);
++                                        #include <atomic>
++                                        std::atomic<uint64_t> x(0);]],
++                                        [[x.fetch_add(1);
+                                         return x.load();]])],
+         [
+           AC_MSG_RESULT([yes])
diff --git a/meta-oe/recipes-connectivity/rtorrent/rtorrent_0.16.25.bb b/meta-oe/recipes-connectivity/rtorrent/rtorrent_0.16.25.bb
index 26cf52346c..b6057872a6 100644
--- a/meta-oe/recipes-connectivity/rtorrent/rtorrent_0.16.25.bb
+++ b/meta-oe/recipes-connectivity/rtorrent/rtorrent_0.16.25.bb
@@ -5,7 +5,9 @@  LIC_FILES_CHKSUM = "file://COPYING;md5=94d55d512a9ba36caa9b7df079bae19f"

 DEPENDS = "autoconf-archive libsigc++-2.0 curl cppunit libtool libtorrent ncurses"

-SRC_URI = "git://github.com/rakshasa/rtorrent;branch=master;protocol=https;tag=v${PV}"
+SRC_URI = "git://github.com/rakshasa/rtorrent;branch=master;protocol=https;tag=v${PV} \
+           file://0001-checks.m4-Keep-the-atomic-test-program-from-being-opt.patch \
+           "

 SRCREV = "15ccae27deb9aa1dc8e46ea0ed218a673ccc5732"