libgomp: Add, if existing, -latomic to libgomp.spec --as-needed

libgomp/ChangeLog:

	* acinclude.m4 (LIBGOMP_CHECK_LIBATOMIC): New; set
	@LIBATOMICSPEC@ is the target libatomic is built.
	* configure.ac: Call LIBGOMP_CHECK_LIBATOMIC.
	* libgomp.spec.in: Add @LIBATOMICSPEC@.
	* Makefile.in: Regenerate.
	* configure: Regenerate.
	* testsuite/Makefile.in: Regenerate.

 libgomp/Makefile.in           |   1 +
 libgomp/acinclude.m4          |  63 ++++++++++++++++++++++++++
 libgomp/configure             | 100 +++++++++++++++++++++++++++++++++++++++++-
 libgomp/configure.ac          |   2 +
 libgomp/libgomp.spec.in       |   2 +-
 libgomp/testsuite/Makefile.in |   1 +
 6 files changed, 166 insertions(+), 3 deletions(-)

diff --git a/libgomp/Makefile.in b/libgomp/Makefile.in
index 00d5e2919ee..a8ec69f1822 100644
--- a/libgomp/Makefile.in
+++ b/libgomp/Makefile.in
@@ -395,6 +395,7 @@ INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@
 INTPTR_T_KIND = @INTPTR_T_KIND@
 LD = @LD@
 LDFLAGS = @LDFLAGS@
+LIBATOMICSPEC = @LIBATOMICSPEC@
 LIBOBJS = @LIBOBJS@
 LIBS = @LIBS@
 LIBTOOL = @LIBTOOL@
diff --git a/libgomp/acinclude.m4 b/libgomp/acinclude.m4
index dbf54d06db9..3d7e5e08c3a 100644
--- a/libgomp/acinclude.m4
+++ b/libgomp/acinclude.m4
@@ -365,3 +365,66 @@ if test $enable_symvers != no ; then
     esac
 fi
 ])
+
+dnl Check whether libatomic exists
+AC_DEFUN([LIBGOMP_CHECK_LIBATOMIC], [
+  LIBATOMICSPEC=
+  libgomp_libatomic=no
+
+  if echo " ${TARGET_CONFIGDIRS} " | grep " libatomic " > /dev/null 2>&1 ; then
+    libgomp_libatomic=yes;
+  fi
+
+  AC_MSG_CHECKING([for target-libatomic support])
+  AC_MSG_RESULT([$libgomp_libatomic])
+
+  if test "x$libgomp_libatomic" = xyes; then
+    dnl Check whether -Wl,--as-needed resp. -Wl,-zignore is supported
+    dnl
+    dnl Turn warnings into error to avoid testsuite breakage.  So enable
+    dnl AC_LANG_WERROR, but there's currently (autoconf 2.64) no way to turn
+    dnl it off again.  As a workaround, save and restore werror flag like
+    dnl AC_PATH_XTRA.
+    dnl Cf. http://gcc.gnu.org/ml/gcc-patches/2010-05/msg01889.html
+    ac_xsave_[]_AC_LANG_ABBREV[]_werror_flag=$ac_[]_AC_LANG_ABBREV[]_werror_flag
+    AC_CACHE_CHECK([whether --as-needed/-z ignore works],
+      [libgomp_cv_have_as_needed],
+      [
+      # Test for native Solaris options first.
+      # No whitespace after -z to pass it through -Wl.
+      libgomp_cv_as_needed_option="-zignore"
+      libgomp_cv_no_as_needed_option="-zrecord"
+      save_LDFLAGS="$LDFLAGS"
+      LDFLAGS="$LDFLAGS -Wl,$libgomp_cv_as_needed_option -lm -Wl,$libompr_cv_no_as_needed_option"
+      libgomp_cv_have_as_needed=no
+      AC_LANG_WERROR
+      AC_LINK_IFELSE([AC_LANG_PROGRAM([])],
+		     [libgomp_cv_have_as_needed=yes],
+		     [libgomp_cv_have_as_needed=no])
+      LDFLAGS="$save_LDFLAGS"
+      if test "x$libgomp_cv_have_as_needed" = xno; then
+	libgomp_cv_as_needed_option="--as-needed"
+	libgomp_cv_no_as_needed_option="--no-as-needed"
+	save_LDFLAGS="$LDFLAGS"
+	LDFLAGS="$LDFLAGS -Wl,$libgomp_cv_as_needed_option -lm -Wl,$libgomp_cv_no_as_needed_option"
+	libgomp_cv_have_as_needed=no
+	AC_LANG_WERROR
+	AC_LINK_IFELSE([AC_LANG_PROGRAM([])],
+		       [libgomp_cv_have_as_needed=yes],
+		       [libgomp_cv_have_as_needed=no])
+	LDFLAGS="$save_LDFLAGS"
+      fi
+      ac_[]_AC_LANG_ABBREV[]_werror_flag=$ac_xsave_[]_AC_LANG_ABBREV[]_werror_flag
+    ])
+
+    dnl For static libgfortran linkage, depend on libquadmath only if needed.
+    if test "x$libgomp_cv_have_as_needed" = xyes; then
+      LIBATOMICSPEC="$libgomp_cv_as_needed_option -latomic $libgomp_cv_no_as_needed_option"
+    else
+      LIBATOMICSPEC="-latomic"
+    fi
+  fi
+
+  dnl For the spec file
+  AC_SUBST(LIBATOMICSPEC)
+])
diff --git a/libgomp/configure b/libgomp/configure
index e48371d5093..a4d93974084 100755
--- a/libgomp/configure
+++ b/libgomp/configure
@@ -630,6 +630,8 @@ ac_includes_default="\
 
 ac_header_list=
 ac_func_list=
+ac_c_werror_flag=
+ac_c_werror_flag=
 ac_subst_vars='am__EXEEXT_FALSE
 am__EXEEXT_TRUE
 LTLIBOBJS
@@ -653,6 +655,7 @@ USE_FORTRAN_FALSE
 USE_FORTRAN_TRUE
 link_gomp
 tmake_file
+LIBATOMICSPEC
 XLDFLAGS
 XCFLAGS
 config_path
@@ -11429,7 +11432,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11432 "configure"
+#line 11435 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -11535,7 +11538,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11538 "configure"
+#line 11541 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -16756,6 +16759,99 @@ FCFLAGS="$FCFLAGS $CET_FLAGS"
 
 
 
+
+  LIBATOMICSPEC=
+  libgomp_libatomic=no
+
+  if echo " ${TARGET_CONFIGDIRS} " | grep " libatomic " > /dev/null 2>&1 ; then
+    libgomp_libatomic=yes;
+  fi
+
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking for target-libatomic support" >&5
+$as_echo_n "checking for target-libatomic support... " >&6; }
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $libgomp_libatomic" >&5
+$as_echo "$libgomp_libatomic" >&6; }
+
+  if test "x$libgomp_libatomic" = xyes; then
+                                ac_xsave_c_werror_flag=$ac_c_werror_flag
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether --as-needed/-z ignore works" >&5
+$as_echo_n "checking whether --as-needed/-z ignore works... " >&6; }
+if ${libgomp_cv_have_as_needed+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+
+      # Test for native Solaris options first.
+      # No whitespace after -z to pass it through -Wl.
+      libgomp_cv_as_needed_option="-zignore"
+      libgomp_cv_no_as_needed_option="-zrecord"
+      save_LDFLAGS="$LDFLAGS"
+      LDFLAGS="$LDFLAGS -Wl,$libgomp_cv_as_needed_option -lm -Wl,$libompr_cv_no_as_needed_option"
+      libgomp_cv_have_as_needed=no
+
+ac_c_werror_flag=yes
+      cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+int
+main ()
+{
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+  libgomp_cv_have_as_needed=yes
+else
+  libgomp_cv_have_as_needed=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+      LDFLAGS="$save_LDFLAGS"
+      if test "x$libgomp_cv_have_as_needed" = xno; then
+	libgomp_cv_as_needed_option="--as-needed"
+	libgomp_cv_no_as_needed_option="--no-as-needed"
+	save_LDFLAGS="$LDFLAGS"
+	LDFLAGS="$LDFLAGS -Wl,$libgomp_cv_as_needed_option -lm -Wl,$libgomp_cv_no_as_needed_option"
+	libgomp_cv_have_as_needed=no
+
+ac_c_werror_flag=yes
+	cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+int
+main ()
+{
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+  libgomp_cv_have_as_needed=yes
+else
+  libgomp_cv_have_as_needed=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+	LDFLAGS="$save_LDFLAGS"
+      fi
+      ac_c_werror_flag=$ac_xsave_c_werror_flag
+
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libgomp_cv_have_as_needed" >&5
+$as_echo "$libgomp_cv_have_as_needed" >&6; }
+
+        if test "x$libgomp_cv_have_as_needed" = xyes; then
+      LIBATOMICSPEC="$libgomp_cv_as_needed_option -latomic $libgomp_cv_no_as_needed_option"
+    else
+      LIBATOMICSPEC="-latomic"
+    fi
+  fi
+
+
+
+
 # Conditionalize the makefile for this target machine.
 tmake_file_=
 for f in ${tmake_file}
diff --git a/libgomp/configure.ac b/libgomp/configure.ac
index e4f30cbb082..2ee2c1a16d8 100644
--- a/libgomp/configure.ac
+++ b/libgomp/configure.ac
@@ -348,6 +348,8 @@ AC_SUBST(config_path)
 AC_SUBST(XCFLAGS)
 AC_SUBST(XLDFLAGS)
 
+LIBGOMP_CHECK_LIBATOMIC
+
 # Conditionalize the makefile for this target machine.
 tmake_file_=
 for f in ${tmake_file}
diff --git a/libgomp/libgomp.spec.in b/libgomp/libgomp.spec.in
index 5651603f487..c42d8b28848 100644
--- a/libgomp/libgomp.spec.in
+++ b/libgomp/libgomp.spec.in
@@ -1,3 +1,3 @@
 # This spec file is read by gcc when linking.  It is used to specify the
 # standard libraries we need in order to link with libgomp.
-*link_gomp: @link_gomp@
+*link_gomp: @link_gomp@ @LIBATOMICSPEC@
diff --git a/libgomp/testsuite/Makefile.in b/libgomp/testsuite/Makefile.in
index e48c3f2f9b0..cf2ce55d126 100644
--- a/libgomp/testsuite/Makefile.in
+++ b/libgomp/testsuite/Makefile.in
@@ -173,6 +173,7 @@ INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@
 INTPTR_T_KIND = @INTPTR_T_KIND@
 LD = @LD@
 LDFLAGS = @LDFLAGS@
+LIBATOMICSPEC = @LIBATOMICSPEC@
 LIBOBJS = @LIBOBJS@
 LIBS = @LIBS@
 LIBTOOL = @LIBTOOL@
