BUILD: makefile: simplify detection of libatomic

We've had libatomic enabled on arm and aarch64 for some Raspberry PI
while usually it's not needed, but it was a bit arbitrary and in
issue #1455 it was reported that RISCV requires it for single-byte
atomics.

This changes the approach to detect the explicit requirement of
external functions for the builtins, as reported with *_LOCK_FREE=1.
If any of the atomics requires libatomic, it will be used. Older
compilers do not report any such atomic as they use sync_* instead
and will not match it nor include libatomic (which usually is not
present there).

On x86, the rules depend on -march. i386 uses LOCK_FREE=1 for all of
them. i486 uses it only for the 8-byte CAS and i586 doesn't require
it at all. For this reason, the build flags are used during the test.

This was tested with armv7, aarch64, mips, riscv, i
diff --git a/Makefile b/Makefile
index 50a21b3..f5dfed2 100644
--- a/Makefile
+++ b/Makefile
@@ -54,6 +54,7 @@
 #   USE_THREAD_DUMP      : use the more advanced thread state dump system. Automatic.
 #   USE_OT               : enable the OpenTracing filter
 #   USE_MEMORY_PROFILING : enable the memory profiler. Linux-glibc only.
+#   USE_LIBATOMIC        : force to link with/without libatomic. Automatic.
 #
 # Options can be forced by specifying "USE_xxx=1" or can be disabled by using
 # "USE_xxx=" (empty string). The list of enabled and disabled options for a
@@ -334,9 +335,6 @@
 USE_SLZ    = default
 endif
 
-# Always enable threads support by default and let the Makefile detect if
-# HAProxy can be compiled with threads or not.
-
 # generic system target has nothing specific
 ifeq ($(TARGET),generic)
   set_target_defaults = $(call default_opts,USE_POLL USE_TPROXY)
@@ -355,9 +353,6 @@
     USE_CPU_AFFINITY USE_THREAD USE_EPOLL USE_LINUX_TPROXY                    \
     USE_ACCEPT4 USE_LINUX_SPLICE USE_PRCTL USE_THREAD_DUMP USE_NS USE_TFO     \
     USE_GETADDRINFO USE_BACKTRACE)
-ifneq ($(shell echo __arm__/__aarch64__ | $(CC) -E -xc - | grep '^[^\#]'),__arm__/__aarch64__)
-  TARGET_LDFLAGS=-latomic
-endif
 endif
 
 # For linux >= 2.6.28, glibc without new features
@@ -375,9 +370,6 @@
     USE_CPU_AFFINITY USE_THREAD USE_EPOLL USE_LINUX_TPROXY                    \
     USE_ACCEPT4 USE_LINUX_SPLICE USE_PRCTL USE_THREAD_DUMP USE_NS USE_TFO     \
     USE_GETADDRINFO)
-ifneq ($(shell echo __arm__/__aarch64__ | $(CC) -E -xc - | grep '^[^\#]'),__arm__/__aarch64__)
-  TARGET_LDFLAGS=-latomic
-endif
 endif
 
 # Solaris 10 and above
@@ -460,6 +452,18 @@
 # set the default settings according to the target above
 $(set_target_defaults)
 
+# Some architectures require to link with libatomic for atomics of certain
+# sizes. These ones are reported as value 1 in the *_LOCK_FREE macros. Value
+# 2 indicates that the builtin is native thus doesn't require libatomic. Hence
+# any occurrence of 1 indicates libatomic is necessary. It's better to avoid
+# linking with it by default as it's not always available nor deployed
+# (especially on archs which do not need it).
+ifneq ($(USE_THREAD),)
+ifneq ($(shell $(CC) $(CFLAGS) -dM -E -xc - </dev/null 2>/dev/null | grep -c 'LOCK_FREE.*1'),0)
+  USE_LIBATOMIC=1
+endif
+endif
+
 #### Determine version, sub-version and release date.
 # If GIT is found, and IGNOREGIT is not set, VERSION, SUBVERS and VERDATE are
 # extracted from the last commit. Otherwise, use the contents of the files
@@ -794,6 +798,10 @@
 include addons/ot/Makefile
 endif
 
+ifneq ($(USE_LIBATOMIC),)
+  TARGET_LDFLAGS += -latomic
+endif
+
 #### Global link options
 # These options are added at the end of the "ld" command line. Use LDFLAGS to
 # add options at the beginning of the "ld" command line if needed.