From ae944d88cd4a3b9fad3b7c43c85bbc383ed053dc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
Date: Thu, 7 Mar 2013 14:51:02 +0100
Subject: [PATCH] Setup and use CC_FOR_BUILD.

---
 ChangeLog      |  12 ++++++
 Makefile.in    |  44 +++++++++++--------
 aclocal.m4     | 112 +++++++++++++++++++++++++++++++++++++++++++++++++
 config.make.in |   3 ++
 configure.ac   |   4 ++
 5 files changed, 157 insertions(+), 18 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 6cf092a1..afa886e2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
 2013-03-07  Niels Möller  <nisse@lysator.liu.se>
 
+	* Makefile.in (aesdata, desdata, twofishdata, shadata, gcmdata)
+	(eccdata): Arrange for compiling these programs for running on the
+	build system, also when cross compiling everything else.
+
+	* config.make.in (CC_FOR_BUILD, EXEEXT_FOR_BUILD): New variables.
+
+	* configure.ac: Use GMP_PROG_CC_FOR_BUILD and
+	GMP_PROG_EXEEXT_FOR_BUILD.
+
+	* aclocal.m4 (GMP_PROG_CC_FOR_BUILD, GMP_PROG_CC_FOR_BUILD_WORKS)
+	(GMP_PROG_EXEEXT_FOR_BUILD): New macros, based on GMP's.
+
 	* aesdata.c: Deleted includes of config.h and nettle-types.h. Use
 	unsigned char and unsigned long instead of stdint.h types.
 
diff --git a/Makefile.in b/Makefile.in
index ad48a5ce..1ec807be 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -221,24 +221,32 @@ $(LIBHOGWEED_FORLINK): $(hogweed_PURE_OBJS) $(LIBNETTLE_FORLINK)
 	$(COMPILE) $(SHLIBCFLAGS) -c $< -o $@ \
 	&& $(DEP_PROCESS)
 
-# For Solaris and BSD make, we have to use an explicit rule for each executable
-aesdata$(EXEEXT): aesdata.$(OBJEXT)
-	$(LINK) aesdata.$(OBJEXT) $(LIBS) -o aesdata$(EXEEXT)
-
-desdata$(EXEEXT): desdata.$(OBJEXT)
-	$(LINK) desdata.$(OBJEXT) $(LIBS) -o desdata$(EXEEXT)
-
-twofishdata$(EXEEXT): twofishdata.$(OBJEXT)
-	$(LINK) twofishdata.$(OBJEXT) $(LIBS) -o twofishdata$(EXEEXT)
-
-shadata$(EXEEXT): shadata.$(OBJEXT)
-	$(LINK) shadata.$(OBJEXT) $(LIBS) -lm -o shadata$(EXEEXT)
-
-gcmdata$(EXEEXT): gcmdata.$(OBJEXT)
-	$(LINK) gcmdata.$(OBJEXT) $(LIBS) -o gcmdata$(EXEEXT)
-
-eccdata$(EXEEXT): eccdata.$(OBJEXT)
-	$(LINK) eccdata.$(OBJEXT) $(LIBS) -o eccdata$(EXEEXT)
+# For Solaris and BSD make, we have to use an explicit rule for each
+# executable. Avoid object file targets to make it easy to run the
+# right compiler.
+aesdata$(EXEEXT_FOR_BUILD): aesdata.c
+	$(CC_FOR_BUILD) `test -f aesdata.c || echo '$(srcdir)/'`aesdata.c \
+	-o aesdata$(EXEEXT_FOR_BUILD)
+
+desdata$(EXEEXT_FOR_BUILD): desdata.c
+	$(CC_FOR_BUILD) `test -f desdata.c || echo '$(srcdir)/'`desdata.c \
+	-o desdata$(EXEEXT_FOR_BUILD)
+
+twofishdata$(EXEEXT_FOR_BUILD): twofishdata.c
+	$(CC_FOR_BUILD) `test -f twofishdata.c || echo '$(srcdir)/'`twofishdata.c \
+	-o twofishdata$(EXEEXT_FOR_BUILD)
+
+shadata$(EXEEXT_FOR_BUILD): shadata.c
+	$(CC_FOR_BUILD) `test -f shadata.c || echo '$(srcdir)/'`shadata.c -lm \
+	-o shadata$(EXEEXT_FOR_BUILD)
+
+gcmdata$(EXEEXT_FOR_BUILD): gcmdata.c
+	$(CC_FOR_BUILD) `test -f gcmdata.c || echo '$(srcdir)/'`gcmdata.c \
+	-o gcmdata$(EXEEXT_FOR_BUILD)
+
+eccdata$(EXEEXT_FOR_BUILD): eccdata.c
+	$(CC_FOR_BUILD) `test -f eccdata.c || echo '$(srcdir)/'`eccdata.c -lgmp \
+	-o eccdata$(EXEEXT_FOR_BUILD)
 
 # desCore rules
 # It seems using $(srcdir)/ doesn't work with GNU make 3.79.1
diff --git a/aclocal.m4 b/aclocal.m4
index a94c20d3..ae6b204a 100644
--- a/aclocal.m4
+++ b/aclocal.m4
@@ -496,6 +496,118 @@ fi
 rm -f conftest*
 ])
 
+dnl  GMP_PROG_CC_FOR_BUILD
+dnl  ---------------------
+dnl  Establish CC_FOR_BUILD, a C compiler for the build system.
+dnl
+dnl  If CC_FOR_BUILD is set then it's expected to work, likewise the old
+dnl  style HOST_CC, otherwise some likely candidates are tried, the same as
+dnl  configfsf.guess.
+
+AC_DEFUN([GMP_PROG_CC_FOR_BUILD],
+[AC_REQUIRE([AC_PROG_CC])
+if test -n "$CC_FOR_BUILD"; then
+  GMP_PROG_CC_FOR_BUILD_WORKS($CC_FOR_BUILD,,
+    [AC_MSG_ERROR([Specified CC_FOR_BUILD doesn't seem to work])])
+elif test -n "$HOST_CC"; then
+  GMP_PROG_CC_FOR_BUILD_WORKS($HOST_CC,
+    [CC_FOR_BUILD=$HOST_CC],
+    [AC_MSG_ERROR([Specified HOST_CC doesn't seem to work])])
+elif test $cross_compiling = no ; then
+  CC_FOR_BUILD="$CC"
+else
+  for i in cc gcc c89 c99; do
+    GMP_PROG_CC_FOR_BUILD_WORKS($i,
+      [CC_FOR_BUILD=$i
+       break])
+  done
+  if test -z "$CC_FOR_BUILD"; then
+    AC_MSG_ERROR([Cannot find a build system compiler])
+  fi
+fi
+
+AC_ARG_VAR(CC_FOR_BUILD,[build system C compiler])
+AC_SUBST(CC_FOR_BUILD)
+])
+
+
+dnl  GMP_PROG_CC_FOR_BUILD_WORKS(cc/cflags[,[action-if-good][,action-if-bad]])
+dnl  -------------------------------------------------------------------------
+dnl  See if the given cc/cflags works on the build system.
+dnl
+dnl  It seems easiest to just use the default compiler output, rather than
+dnl  figuring out the .exe or whatever at this stage.
+
+AC_DEFUN([GMP_PROG_CC_FOR_BUILD_WORKS],
+[AC_MSG_CHECKING([build system compiler $1])
+# remove anything that might look like compiler output to our "||" expression
+rm -f conftest* a.out b.out a.exe a_out.exe
+cat >conftest.c <<EOF
+int
+main ()
+{
+  exit(0);
+}
+EOF
+gmp_compile="$1 conftest.c"
+cc_for_build_works=no
+if AC_TRY_EVAL(gmp_compile); then
+  if (./a.out || ./b.out || ./a.exe || ./a_out.exe || ./conftest) >&AC_FD_CC 2>&1; then
+    cc_for_build_works=yes
+  fi
+fi
+rm -f conftest* a.out b.out a.exe a_out.exe
+AC_MSG_RESULT($cc_for_build_works)
+if test "$cc_for_build_works" = yes; then
+  ifelse([$2],,:,[$2])
+else
+  ifelse([$3],,:,[$3])
+fi
+])
+
+dnl  GMP_PROG_EXEEXT_FOR_BUILD
+dnl  -------------------------
+dnl  Determine EXEEXT_FOR_BUILD, the build system executable suffix.
+dnl
+dnl  The idea is to find what "-o conftest$foo" will make it possible to run
+dnl  the program with ./conftest.  On Unix-like systems this is of course
+dnl  nothing, for DOS it's ".exe", or for a strange RISC OS foreign file
+dnl  system cross compile it can be ",ff8" apparently.  Not sure if the
+dnl  latter actually applies to a build-system executable, maybe it doesn't,
+dnl  but it won't hurt to try.
+
+AC_DEFUN([GMP_PROG_EXEEXT_FOR_BUILD],
+[AC_REQUIRE([GMP_PROG_CC_FOR_BUILD])
+AC_CACHE_CHECK([for build system executable suffix],
+               gmp_cv_prog_exeext_for_build,
+[if test $cross_compiling = no ; then
+  gmp_cv_prog_exeext_for_build="$EXEEXT"
+else
+  cat >conftest.c <<EOF
+int
+main ()
+{
+  exit (0);
+}
+EOF
+  for i in .exe ,ff8 ""; do
+    gmp_compile="$CC_FOR_BUILD conftest.c -o conftest$i"
+    if AC_TRY_EVAL(gmp_compile); then
+      if (./conftest) 2>&AC_FD_CC; then
+        gmp_cv_prog_exeext_for_build=$i
+        break
+      fi
+    fi
+  done
+  rm -f conftest*
+  if test "${gmp_cv_prog_exeext_for_build+set}" != set; then
+    AC_MSG_ERROR([Cannot determine executable suffix])
+  fi
+fi
+])
+AC_SUBST(EXEEXT_FOR_BUILD,$gmp_cv_prog_exeext_for_build)
+])
+
 dnl @synopsis AX_CREATE_STDINT_H [( HEADER-TO-GENERATE [, HEADERS-TO-CHECK])]
 dnl
 dnl the "ISO C9X: 7.18 Integer types <stdint.h>" section requires the
diff --git a/config.make.in b/config.make.in
index a1ebf0dc..ac3393de 100644
--- a/config.make.in
+++ b/config.make.in
@@ -17,6 +17,9 @@ NM = @NM@
 OBJEXT = @OBJEXT@
 EXEEXT = @EXEEXT@
 
+CC_FOR_BUILD = @CC_FOR_BUILD@
+EXEEXT_FOR_BUILD = @EXEEXT_FOR_BUILD@
+
 DEP_FLAGS = @DEP_FLAGS@
 DEP_PROCESS = @DEP_PROCESS@
 
diff --git a/configure.ac b/configure.ac
index c0413781..d082d8b0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -118,6 +118,10 @@ AC_PROG_MKDIR_P
 
 AC_PROG_LN_S
 
+# Compiler tests for the build system
+GMP_PROG_CC_FOR_BUILD
+GMP_PROG_EXEEXT_FOR_BUILD
+
 LSH_DEPENDENCY_TRACKING
 
 if test x$enable_dependency_tracking = xyes ; then
-- 
GitLab