[build] Test for GNU-style PIE support in toolchain and enable

2008-08-13 Paul P Komkoff Jr <i@stingr.net>

	* configure.ac: add a configure flag and autoconf macro, which will
	  determine if your toolchain supports PIE.
	* */Makefile.am: add corresponding CFLAGS and LDFLAGS into
	  appropriate places.

Signed-off-by: Paul Jakma <paul@quagga.net>
diff --git a/configure.ac b/configure.ac
index 746b5ce..5c6c0d7 100755
--- a/configure.ac
+++ b/configure.ac
@@ -60,9 +60,10 @@
 dnl --------------------
 dnl Check CC and friends
 dnl --------------------
+AC_LANG([C])
 AC_PROG_CC
 AC_PROG_CPP
-
+AM_PROG_CC_C_O
 AC_PROG_EGREP
 
 dnl autoconf 2.59 appears not to support AC_PROG_SED
@@ -1395,6 +1396,31 @@
 CONFDATE=`date '+%Y%m%d'`
 AC_SUBST(CONFDATE)
 
+dnl Conditionally enable PIE support for GNU toolchains.
+enable_pie=yes
+
+AC_ARG_ENABLE(pie, AS_HELP_STRING([--disable-pie], [Do not build tools as a Position Independent Executables]))
+if test "$enable_pie" = "yes"; then
+  AC_CACHE_CHECK([whether $CC accepts PIE flags], [ap_cv_cc_pie], [
+    save_CFLAGS=$CFLAGS
+    save_LDFLAGS=$LDFLAGS
+    CFLAGS="$CFLAGS -fPIE"
+    LDFLAGS="$LDFLAGS -pie"
+    AC_LINK_IFELSE([AC_LANG_SOURCE([[static int foo[30000]; int main () { return 0; }]])],
+      [ap_cv_cc_pie=yes], [ap_cv_cc_pie=no]
+    )
+    CFLAGS=$save_CFLAGS
+    LDFLAGS=$save_LDFLAGS
+  ])
+  if test "$ap_cv_cc_pie" = "yes"; then
+    PICFLAGS="-fPIE"
+    PILDFLAGS="-pie"
+  fi
+fi
+
+AC_SUBST(PICFLAGS)
+AC_SUBST(PILDFLAGS)
+
 dnl ------------------------------
 dnl set paths for state directory
 dnl ------------------------------