Subject: bug#11089: LINK determination

From: Stefano Lattarini <[email-masked]>
Date: Tue, 27 Mar 2012 15:27:31 +0200

[Adding automake-patches]

Reference: <http://debbugs.gnu.org/11089>

> On Sun, Mar 25, 2012 at 11:20 AM, Stefano Lattarini wrote:
>
> I'd describe this as an automake limitation; not sure if it's worth
> fixing (since yours is not a common use case, and the issue you're
> facing has a simple-enough workaround, as you've already found out);
> but it would be worth documenting IMHO.
>
On a second thought, I've been unable to find a way to document Jason's
workaround that doesn't sound cumbersome and clumsy; so maybe it's better
to just drop this documentation change.

Still, having a test case verifying that such a workaround actually works
is still worthwhile and easy to do.  This is what the attached patch does.
I will push it this evening or tomorrow if there is no objection.

Regards,
  Stefano

>From 6805810d314e2b4ecb8ff14e04779d8f0ad2b4f8 Mon Sep 17 00:00:00 2001
Message-Id: <6805810d3[email-masked]>
From: Stefano Lattarini <[email-masked]>
Date: Mon, 26 Mar 2012 15:26:46 +0200
Subject: [PATCH] tests: workaround for automatic linker determination and
 conditionals

See automake bug#11089.

Automake is not very smart in automatically determining the command
to be used to link a program whose source files' languages are
conditionally defined.  For example, an input like:

  if HAVE_CXX
  foo_SOURCES = more.c++
  else
  foo_SOURCES = less.c
  endif

will cause the build rules for 'foo' to *unconditionally* use the
C++ compiler for linking, even when the 'HAVE_CXX' conditional
evaluates to false (which might mean that no C++ compiler is
available).

This behaviour is not really correct, but it's easy enough to work
around, and it's only relevant for fringe use cases (at best).  So
let's just test that the workaround really works.

* tests/link_cond.test: New test.
* tests/list-of-tests.mk: Add it.
* THANKS: Update.

Signed-off-by: Stefano Lattarini <[email-masked]>
---
 THANKS                 |    1 +
 tests/link_cond.test   |   90 ++++++++++++++++++++++++++++++++++++++++++++++++
 tests/list-of-tests.mk |    1 +
 3 files changed, 92 insertions(+), 0 deletions(-)
 create mode 100755 tests/link_cond.test

diff --git a/THANKS b/THANKS
index 455cc62..702c97b 100644
--- a/THANKS
+++ b/THANKS
@@ -149,6 +149,7 @@ James Youngman		[email-masked]
 Jan Engelhardt		[email-masked]
 Janos Farkas		[email-masked]
 Jared Davis		[email-masked]
+Jason DeVinney		[email-masked]
 Jason Duell		[email-masked]
 Jason Molenda		[email-masked]
 Javier Jardón		[email-masked]
diff --git a/tests/link_cond.test b/tests/link_cond.test
new file mode 100755
index 0000000..0d61865
--- /dev/null
+++ b/tests/link_cond.test
@@ -0,0 +1,90 @@
+#! /bin/sh
+# Copyright (C) 2012 Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test that automatic determination of the linker works well with
+# conditional use of languages in a single program.
+# This currently doesn't truly work, but we have an easy workaround
+# at least, that is tested here.
+# See automake bug#11089.
+
+required='cc c++'
+. ./defs || Exit 1
+
+set -e
+
+cat >> configure.in << 'END'
+AC_PROG_CC
+AC_PROG_CXX
+AM_CONDITIONAL([HAVE_CXX], [test $have_cxx = yes])
+AC_OUTPUT
+END
+
+cat > Makefile.am << 'END'
+bin_PROGRAMS = foo
+if HAVE_CXX
+foo_SOURCES = more.c++
+else
+foo_SOURCES = less.c
+endif
+## FIXME: ideally, this workaround shouldn't be needed.
+if HAVE_CXX
+foo_LINK = $(CXXLINK)
+else
+foo_LINK = $(LINK)
+endif
+END
+
+$ACLOCAL
+$AUTOMAKE
+$AUTOCONF
+
+rm -f *.c++
+cat > less.c <<'END'
+/* Valid C but deliberately invalid C++ */
+main ()
+{
+  int new = 0;
+  return new;
+}
+END
+
+./configure have_cxx=no
+CXX=false $MAKE -e
+
+# Sanity check.
+rm -f foo foo.exe
+CC=false $MAKE -e && Exit 99
+
+$MAKE distclean
+
+rm -f *.c
+cat > more.c++ <<'END'
+/* Valid C++ but deliberately invalid C */
+using namespace std;
+int main (void)
+{
+  return 0;
+}
+END
+
+./configure have_cxx=yes
+CC=false $MAKE -e
+
+# Sanity check.
+rm -f foo foo.exe
+CXX=false $MAKE -e && Exit 99
+
+:
diff --git a/tests/list-of-tests.mk b/tests/list-of-tests.mk
index 819cd06..2548174 100644
--- a/tests/list-of-tests.mk
+++ b/tests/list-of-tests.mk
@@ -553,6 +553,7 @@ libtoo11.test \
 license.test \
 license2.test \
 link_c_cxx.test \
+link_cond.test \
 link_dist.test \
 link_f90_only.test \
 link_fc.test \
-- 
1.7.9