[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/bgpd/Makefile.am b/bgpd/Makefile.am
index 80e9328..1b17d38 100644
--- a/bgpd/Makefile.am
+++ b/bgpd/Makefile.am
@@ -4,6 +4,9 @@
 DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\"
 INSTALL_SDATA=@INSTALL@ -m 600
 
+AM_CFLAGS = $(PICFLAGS)
+AM_LDFLAGS = $(PILDFLAGS)
+
 noinst_LIBRARIES = libbgp.a
 sbin_PROGRAMS = bgpd
 
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 ------------------------------
diff --git a/guile/Makefile.am b/guile/Makefile.am
index ecdb8f3..5beb71c 100644
--- a/guile/Makefile.am
+++ b/guile/Makefile.am
@@ -3,6 +3,9 @@
 INCLUDES = @GUILE_CFLAGS@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib
 DEFS = @DEFS@ -I. -I$(srcdir)
 
+AM_CFLAGS = $(PICFLAGS)
+AM_LDFLAGS = $(PILDFLAGS)
+
 bin_PROGRAMS = zebra-guile
 zebra_guile_SOURCES = zebra-guile.c zebra-support.c guile-bgp.c
 noinst_HEADERS = zebra-guile.h
diff --git a/isisd/Makefile.am b/isisd/Makefile.am
index 859facd..9c30339 100644
--- a/isisd/Makefile.am
+++ b/isisd/Makefile.am
@@ -5,6 +5,10 @@
 DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\"
 INSTALL_SDATA=@INSTALL@ -m 600
 LIBS = @LIBS@ 
+
+AM_CFLAGS = $(PICFLAGS)
+AM_LDFLAGS = $(PILDFLAGS)
+
 noinst_LIBRARIES = libisis.a
 sbin_PROGRAMS = isisd 
 SUBDIRS = topology
diff --git a/ospf6d/Makefile.am b/ospf6d/Makefile.am
index ad82871..01bc6fe 100644
--- a/ospf6d/Makefile.am
+++ b/ospf6d/Makefile.am
@@ -4,6 +4,9 @@
 DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\"
 INSTALL_SDATA=@INSTALL@ -m 600
 
+AM_CFLAGS = $(PICFLAGS)
+AM_LDFLAGS = $(PILDFLAGS)
+
 noinst_LIBRARIES = libospf6.a
 sbin_PROGRAMS = ospf6d
 
diff --git a/ospfclient/Makefile.am b/ospfclient/Makefile.am
index ab2a378..b8aae84 100644
--- a/ospfclient/Makefile.am
+++ b/ospfclient/Makefile.am
@@ -21,4 +21,6 @@
 ospfclient_LDADD = libospfapiclient.la \
 	../ospfd/libospf.la ../lib/libzebra.la @LIBCAP@
 
+ospfclient_CFLAGS = $(AM_CFLAGS) $(PICFLAGS)
+ospfclient_LDFLAGS = $(AM_LDFLAGS) $(PILDFLAGS)
 
diff --git a/ripd/Makefile.am b/ripd/Makefile.am
index c36b58c..2fa2665 100644
--- a/ripd/Makefile.am
+++ b/ripd/Makefile.am
@@ -4,6 +4,9 @@
 DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\"
 INSTALL_SDATA=@INSTALL@ -m 600
 
+AM_CFLAGS = $(PICFLAGS)
+AM_LDFLAGS = $(PILDFLAGS)
+
 noinst_LIBRARIES = librip.a
 sbin_PROGRAMS = ripd
 
diff --git a/ripngd/Makefile.am b/ripngd/Makefile.am
index ad1e278..c6bd486 100644
--- a/ripngd/Makefile.am
+++ b/ripngd/Makefile.am
@@ -4,6 +4,9 @@
 DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\"
 INSTALL_SDATA=@INSTALL@ -m 600
 
+AM_CFLAGS = $(PICFLAGS)
+AM_LDFLAGS = $(PILDFLAGS)
+
 noinst_LIBRARIES = libripng.a
 sbin_PROGRAMS = ripngd
 
diff --git a/tests/Makefile.am b/tests/Makefile.am
index d00485f..c93fa08 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,6 +1,9 @@
 INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib
 DEFS = @DEFS@ $(LOCAL_OPTS) -DSYSCONFDIR=\"$(sysconfdir)/\"
 
+AM_CFLAGS = $(PICFLAGS)
+AM_LDFLAGS = $(PILDFLAGS)
+
 noinst_PROGRAMS = testsig testbuffer testmemory heavy heavywq heavythread \
 		aspathtest testprivs teststream testbgpcap ecommtest \
 		testbgpmpattr
diff --git a/vtysh/Makefile.am b/vtysh/Makefile.am
index dbd1d3d..4886a64 100644
--- a/vtysh/Makefile.am
+++ b/vtysh/Makefile.am
@@ -5,6 +5,9 @@
 
 LIBS = @LIBS@ @CURSES@ @LIBPAM@
 
+AM_CFLAGS = $(PICFLAGS)
+AM_LDFLAGS = $(PILDFLAGS)
+
 bin_PROGRAMS = vtysh
 
 vtysh_SOURCES = vtysh_main.c vtysh.c vtysh_user.c vtysh_config.c
diff --git a/watchquagga/Makefile.am b/watchquagga/Makefile.am
index e422535..a49f62e 100644
--- a/watchquagga/Makefile.am
+++ b/watchquagga/Makefile.am
@@ -3,6 +3,9 @@
 INCLUDES = @INCLUDES@ -I$(top_srcdir) -I$(top_srcdir)/lib
 DEFS = @DEFS@ -DSTATEDIR=\"$(localstatedir)/\"
 
+AM_CFLAGS = $(PICFLAGS)
+AM_LDFLAGS = $(PILDFLAGS)
+
 sbin_PROGRAMS = watchquagga
 
 watchquagga_SOURCES = watchquagga.c
diff --git a/zebra/Makefile.am b/zebra/Makefile.am
index 5d8db41..542f36f 100644
--- a/zebra/Makefile.am
+++ b/zebra/Makefile.am
@@ -19,6 +19,9 @@
 otherobj = $(ioctl_method) $(ipforward) $(if_method) $(if_proc) \
 	$(rt_method) $(rtread_method) $(kernel_method) $(other_method)
 
+AM_CFLAGS = $(PICFLAGS)
+AM_LDFLAGS = $(PILDFLAGS)
+
 sbin_PROGRAMS = zebra
 
 noinst_PROGRAMS = testzebra