Search code examples
environment-variablesautotoolsautoconf

Check if environment variable is set in configure script?


I'm trying to test if ANDROID_NDK_ROOT is set in an Autoconf script. The relevant stanza is shown below. According to How can I check an environment variable? on the Autoconf mailing list, I can use:

if test "${var+set}" = set; then
  echo "variable \$var is set to: $var"
fi 

The variable is not set, but my AC_MSG_ERROR is not being executed.

$ printenv | grep ANDROID_NDK_ROOT
$

Instead, the test is producing the following error:

./configure: line 20616: syntax error near unexpected token `('
./configure: line 20616: `  $as_echo_n "(cached) " >&6'

(There's another reply in the thread but it seems to be just a comment and does not answer the question).

How do I test if an environmental variable is set in Autoconf?


Here is the stanza I am trying to execute in configure.ac:

# if test "$IS_ANDROID_OS" != "0"; then
if true; then

   if test "${ANDROID_NDK_ROOT+set}" != set; then
     AC_MSG_ERROR([ANDROID_NDK_ROOT is not set. Please set ANDROID_NDK_ROOT])
   fi 

   THIS_FILE="$ANDROID_NDK_ROOT/sources/android/cpufeatures/cpu-features.h"
   AC_CHECK_FILE([$THIS_FILE],
      [cp "$THIS_FILE" "$ac_srcdir"],
      AC_MSG_RESULT([cpu-features.h does not exist in ANDROID_NDK_ROOT, skipping])
   )

   THIS_FILE="$ANDROID_NDK_ROOT/sources/android/cpufeatures/cpu-features.c"
   AC_CHECK_FILE([$THIS_FILE],
      [cp "$THIS_FILE" "$ac_srcdir"],
      AC_MSG_RESULT([cpu-features.c does not exist in ANDROID_NDK_ROOT, skipping])
   )
fi

Here is the chunk of configure from cat -n:

 20610
 20611     THIS_FILE="$ANDROID_NDK_ROOT/sources/android/cpufeatures/cpu-features.c"
 20612     as_ac_File=`$as_echo "ac_cv_file_$THIS_FILE" | $as_tr_sh`
 20613  { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $THIS_FILE" >&5
 20614  $as_echo_n "checking for $THIS_FILE... " >&6; }
 20615  if eval \${$as_ac_File+:} false; then :
 20616    $as_echo_n "(cached) " >&6
 20617  else
 20618    test "$cross_compiling" = yes &&
 20619    as_fn_error $? "cannot check for file existence when cross compiling""$LINENO" 5
 20620  if test -r "$THIS_FILE"; then
 20621    eval "$as_ac_File=yes"
 20622  else
 20623    eval "$as_ac_File=no"
 20624  fi

Solution

  • There's nothing wrong with your shell syntax for testing whether a variable is set, and it works fine with Autoconf.

    The problem appears to arise from failing to quote the third arguments to the AC_CHECK_FILE() macros. You should always quote (with square brackets) each argument to each macro, especially when that argument is or contains a macro call itself. I can reproduce a syntax error in configure by wrapping the example code you provided between an AC_INIT and an AC_OUTPUT, but it goes away with proper quoting. Specifically, here:

       THIS_FILE="$ANDROID_NDK_ROOT/sources/android/cpufeatures/cpu-features.h"
       AC_CHECK_FILE([$THIS_FILE],
          [cp "$THIS_FILE" "$ac_srcdir"],
          [AC_MSG_RESULT([cpu-features.h does not exist in ANDROID_NDK_ROOT, skipping])]
       )
    
       THIS_FILE="$ANDROID_NDK_ROOT/sources/android/cpufeatures/cpu-features.c"
       AC_CHECK_FILE([$THIS_FILE],
          [cp "$THIS_FILE" "$ac_srcdir"],
          [AC_MSG_RESULT([cpu-features.c does not exist in ANDROID_NDK_ROOT, skipping])]
       )
    

    Failing to quote the argument results in it being expanded too many times, and the resulting output indeed is not syntactically valid shell code.