1

We are trying to test for -pthread availability and set the flag in both CXXFLAGS and LDFLAGS. We don't want to use ax_pthread because it uses the wrong compiler and sets the wrong flags for a C++ project. And according to Significance of -pthread flag when compiling, -pthread is most portable, so we want to use it for both CXXFLAGS and LDFLAGS.

The scripting we added to configure.ac is:

AC_ARG_ENABLE(tls,
   AS_HELP_STRING([--enable-tls], [enable thread storage (default is yes)]),
   ac_enable_tls=$enableval,
   ac_enable_tls=yes)
AM_CONDITIONAL(HAS_PTHREADS, test $ac_enable_tls = yes)

if test "$ac_enable_tls" = "yes"; then
   SAVED_CXXFLAGS="$CXXFLAGS"
   CXXFLAGS="-pthread"
   AC_MSG_CHECKING([for pthread support])
   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([])],
      [AC_MSG_RESULT([-pthread]) && AC_SUBST([AM_CXXFLAGS], ["-pthread"]) && AC_SUBST([AM_LDFLAGS], ["-pthread"])],
      [AC_MSG_FAILURE(["--enable-tls=yes but pthreads are not available"])]
   )
   CXXFLAGS="$SAVED_CXXFLAGS"
fi

It results in:

./configure: line 16173: syntax error near unexpected token `&&'
./configure: line 16173: ` && AM_LDFLAGS="-pthread"'

autoreconf --warnings=all produces no warnings related to the test.

I'm guessing the trouble is trying to do three things in AC_COMPILE_IFELSE and [action-if-true]. The Autoconf AC_COMPILE_IFELSE docs don't tell us how to handle the situation and does not provide examples.

We want to perform three actions in [action-if-true]:

  • Print the message pthread
  • AM_CXXFLAGS += -pthread
  • AM_LDFLAGS += -pthread

How do I perform multiple actions in [action-if-true]?

jww
  • 97,681
  • 90
  • 411
  • 885

1 Answers1

2

It is not clear to me why you use the && shell operator; it will not work if the preceding m4 macro emits a newline. You should consider using ; or simply multiple lines.

It's also quite common in configure scripts just to set a shell variable in the AC_COMPILE_IFELSE actions, and check that afterwards. This avoids deeply nested shell code, and issues related to m4 quoting/macro expansion. Here is an example from glibc:

  AC_COMPILE_IFELSE([AC_LANG_SOURCE([[#include <sys/sdt.h>                      
void foo (int i, void *p)
{
  asm ("" STAP_PROBE_ASM (foo, bar, STAP_PROBE_ASM_TEMPLATE (2)) ""
       :: STAP_PROBE_ASM_OPERANDS (2, i, p));
}]])], [libc_cv_sdt=yes], [libc_cv_sdt=no])
Florian Weimer
  • 32,022
  • 3
  • 48
  • 92
  • Thanks Florian. *"It is not clear to me why you use the && shell operator..."* - That's standard shell stuff - [`&&` and `||`](https://unix.stackexchange.com/q/24684/56041). I'm using it because the [Autoconf documentation](https://www.gnu.org/software/autoconf/manual/autoconf-2.66/html_node/Running-the-Compiler.html) does not tell me what to do. It does not surprise me it is wrong :) – jww Nov 05 '17 at 14:20
  • *"It's also quite common in configure scripts just to set a shell variable..."* - Do you have an example of this? I thought about it too, but I was not sure how to set a local variable that was not exported. The things I tried failed to configure, like `set foo=1`. – jww Nov 05 '17 at 14:36