The issue has been raised in the past that our build system links each
authorNeil Conway
Thu, 5 May 2005 11:50:18 +0000 (11:50 +0000)
committerNeil Conway
Thu, 5 May 2005 11:50:18 +0000 (11:50 +0000)
executable against the maximal set of libraries it might need. So for
example, if one executable requires `libreadline', all executables are
linked against it.

The easiest fix is to make use of GNU ld's --as-needed flag, which
ignores linker arguments that are not actually needed by the specified
object files. The attached patch modifies configure to check for this
flag (when using GNU ld), and if ld supports it, adds the flag to
LDFLAGS (we need to do the check since only relatively recent versions
of GNU ld support this capability). Currently only GNU ld is supported;
I'm not aware of any other linkers that support this functionality.

configure
configure.in

index b77ec52aa8c63d1e3cefed912480eac34396ba43..ba97253d478f5fe5eaf8b835805320ec540f0daf 100755 (executable)
--- a/configure
+++ b/configure
@@ -3622,11 +3622,6 @@ rm -f conftest*
 CPPFLAGS="$CPPFLAGS $INCLUDES"
 LDFLAGS="$LDFLAGS $LIBDIRS"
 
-{ echo "$as_me:$LINENO: using CPPFLAGS=$CPPFLAGS" >&5
-echo "$as_me: using CPPFLAGS=$CPPFLAGS" >&6;}
-{ echo "$as_me:$LINENO: using LDFLAGS=$LDFLAGS" >&5
-echo "$as_me: using LDFLAGS=$LDFLAGS" >&6;}
-
 
 
 for ac_prog in gawk mawk nawk awk
@@ -3856,6 +3851,7 @@ with_gnu_ld=$ac_cv_prog_gnu_ld
 
 
 
+
 case $host_os in sysv5*)
   echo "$as_me:$LINENO: checking whether ld -R works" >&5
 echo $ECHO_N "checking whether ld -R works... $ECHO_C" >&6
@@ -3909,6 +3905,68 @@ echo "${ECHO_T}$pgac_cv_prog_ld_R" >&6
   ld_R_works=$pgac_cv_prog_ld_R
 
 esac
+
+# To simplify the build system, we specify the maximal set of
+# libraries to link against when building any executable. The linker
+# on some platforms optionally allows unused link arguments to be
+# elided from the resulting executable, so enable that capability if
+# it exists.
+# XXX: currently we only support GNU ld; do any other linkers support
+# an equivalent feature?
+if test "$with_gnu_ld"; then
+  echo "$as_me:$LINENO: checking whether ld --as-needed works" >&5
+echo $ECHO_N "checking whether ld --as-needed works... $ECHO_C" >&6
+if test "${pgac_cv_prog_ld_as_needed+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+
+    pgac_save_LDFLAGS=$LDFLAGS; LDFLAGS="$LDFLAGS -Wl,--as-needed"
+    cat >conftest.$ac_ext <<_ACEOF
+#line $LINENO "configure"
+#include "confdefs.h"
+
+#ifdef F77_DUMMY_MAIN
+#  ifdef __cplusplus
+     extern "C"
+#  endif
+   int F77_DUMMY_MAIN() { return 1; }
+#endif
+int
+main ()
+{
+
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext conftest$ac_exeext
+if { (eval echo "$as_me:$LINENO: \"$ac_link\"") >&5
+  (eval $ac_link) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+         { ac_try='test -s conftest$ac_exeext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  pgac_cv_prog_ld_as_needed=yes
+else
+  echo "$as_me: failed program was:" >&5
+cat conftest.$ac_ext >&5
+pgac_cv_prog_ld_as_needed=no
+fi
+rm -f conftest.$ac_objext conftest$ac_exeext conftest.$ac_ext
+   if test x"$pgac_cv_prog_ld_as_needed" = x"no"; then
+     LDFLAGS=$pgac_save_LDFLAGS
+   fi
+
+fi
+echo "$as_me:$LINENO: result: $pgac_cv_prog_ld_as_needed" >&5
+echo "${ECHO_T}$pgac_cv_prog_ld_as_needed" >&6
+fi
+
 if test -n "$ac_tool_prefix"; then
   # Extract the first word of "${ac_tool_prefix}ranlib", so it can be a program name with args.
 set dummy ${ac_tool_prefix}ranlib; ac_word=$2
@@ -4167,6 +4225,11 @@ echo "${ECHO_T}no" >&6
 
 
 
+{ echo "$as_me:$LINENO: using CPPFLAGS=$CPPFLAGS" >&5
+echo "$as_me: using CPPFLAGS=$CPPFLAGS" >&6;}
+{ echo "$as_me:$LINENO: using LDFLAGS=$LDFLAGS" >&5
+echo "$as_me: using LDFLAGS=$LDFLAGS" >&6;}
+
 for ac_prog in 'bison -y'
 do
   # Extract the first word of "$ac_prog", so it can be a program name with args.
index d8da7cd4ffb5d123048f5ed52a2a5576e8c103d5..39ed00a1abf59fa7f6c140f384dfe7ef87ebe602 100644 (file)
@@ -1,5 +1,5 @@
 dnl Process this file with autoconf to produce a configure script.
-dnl $PostgreSQL: pgsql/configure.in,v 1.407 2005/03/25 00:34:19 tgl Exp $
+dnl $PostgreSQL: pgsql/configure.in,v 1.408 2005/05/05 11:50:18 neilc Exp $
 dnl
 dnl Developers, please strive to achieve this order:
 dnl
@@ -539,9 +539,6 @@ AC_SUBST(ELF_SYS)
 CPPFLAGS="$CPPFLAGS $INCLUDES"
 LDFLAGS="$LDFLAGS $LIBDIRS"
 
-AC_MSG_NOTICE([using CPPFLAGS=$CPPFLAGS])
-AC_MSG_NOTICE([using LDFLAGS=$LDFLAGS])
-
 AC_ARG_VAR(LDFLAGS_SL)
 
 AC_PROG_AWK
@@ -550,6 +547,7 @@ AC_PROG_LN_S
 PGAC_PROG_LD
 AC_SUBST(LD)
 AC_SUBST(with_gnu_ld)
+
 case $host_os in sysv5*)
   AC_CACHE_CHECK([whether ld -R works], [pgac_cv_prog_ld_R],
   [
@@ -560,11 +558,35 @@ case $host_os in sysv5*)
   ld_R_works=$pgac_cv_prog_ld_R
   AC_SUBST(ld_R_works)
 esac
+
+# To simplify the build system, we specify the maximal set of
+# libraries to link against when building any executable. The linker
+# on some platforms optionally allows unused link arguments to be
+# elided from the resulting executable, so enable that capability if
+# it exists.
+# XXX: currently we only support GNU ld; do any other linkers support
+# an equivalent feature?
+if test "$with_gnu_ld"; then
+  AC_CACHE_CHECK([whether ld --as-needed works], [pgac_cv_prog_ld_as_needed],
+  [
+    pgac_save_LDFLAGS=$LDFLAGS; LDFLAGS="$LDFLAGS -Wl,--as-needed"
+    AC_TRY_LINK([], [],
+                [pgac_cv_prog_ld_as_needed=yes],
+                [pgac_cv_prog_ld_as_needed=no])
+   if test x"$pgac_cv_prog_ld_as_needed" = x"no"; then
+     LDFLAGS=$pgac_save_LDFLAGS
+   fi
+  ])
+fi
+
 AC_PROG_RANLIB
 AC_CHECK_PROGS(LORDER, lorder)
 AC_PATH_PROG(TAR, tar)
 PGAC_CHECK_STRIP
 
+AC_MSG_NOTICE([using CPPFLAGS=$CPPFLAGS])
+AC_MSG_NOTICE([using LDFLAGS=$LDFLAGS])
+
 AC_CHECK_PROGS(YACC, ['bison -y'])
 
 if test "$YACC"; then