From a331e3a63981427724d2d9cfdcb4a456153299b5 Mon Sep 17 00:00:00 2001
From: Stephen Webb <swebb@blackberry.com>
Date: Mon, 18 Mar 2024 10:22:26 -0400
Subject: [PATCH] configure.ac: detect if -latomic is required

Some OS runtimes require libatomic be linked in separately to get
standard atomic operations to work. Try to detect that at configure
time.

Upstream-Status: Submitted [https://github.com/libunwind/libunwind/pull/732/]
Signed-off-by: Ross Burton <ross.burton@arm.com>
---
 configure.ac | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/configure.ac b/configure.ac
index eaa0ddc..d5f1d4a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -93,6 +93,18 @@ dnl Checks for library functions.
 AC_CHECK_FUNCS(dl_iterate_phdr dl_phdr_removals_counter dlmodinfo getunwind \
 		ttrace mincore pipe2 sigaltstack execvpe)
 
+AC_MSG_CHECKING([if -latomic is required])
+  AC_LINK_IFELSE([AC_LANG_PROGRAM([[
+                        #include <stdatomic.h>
+                        atomic_bool ab_ = 0; atomic_llong al_ = 0;
+                    ]],[[
+                        atomic_load(&ab_); atomic_load(&al_);
+                    ]])],
+                 [use_libatomic=no],
+                 [use_libatomic=yes])
+AC_MSG_RESULT([$use_libatomic])
+AS_IF([test "$use_libatomic" = "yes"],[LIBS="-latomic $LIBS"])
+
 AC_MSG_CHECKING([if building with AltiVec])
 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
 #ifndef __ALTIVEC__
