Check if environment variable is set in configure script?












0















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









share|improve this question




















  • 1





    I don't think the error you observe is related to the shell syntax you're using for testing whether ANDROID_NDK_ROOT is set. The error message itself leads me to suspect mismatched quotes, but I don't see that in the script excerpt. I can, however, reproduce the issue using very little more than the posted code.

    – John Bollinger
    Nov 16 '18 at 15:29


















0















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









share|improve this question




















  • 1





    I don't think the error you observe is related to the shell syntax you're using for testing whether ANDROID_NDK_ROOT is set. The error message itself leads me to suspect mismatched quotes, but I don't see that in the script excerpt. I can, however, reproduce the issue using very little more than the posted code.

    – John Bollinger
    Nov 16 '18 at 15:29
















0












0








0








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









share|improve this question
















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






environment-variables autotools autoconf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 17:47









Dan Albert

5,54411652




5,54411652










asked Nov 16 '18 at 3:54









jwwjww

54.1k41233513




54.1k41233513








  • 1





    I don't think the error you observe is related to the shell syntax you're using for testing whether ANDROID_NDK_ROOT is set. The error message itself leads me to suspect mismatched quotes, but I don't see that in the script excerpt. I can, however, reproduce the issue using very little more than the posted code.

    – John Bollinger
    Nov 16 '18 at 15:29
















  • 1





    I don't think the error you observe is related to the shell syntax you're using for testing whether ANDROID_NDK_ROOT is set. The error message itself leads me to suspect mismatched quotes, but I don't see that in the script excerpt. I can, however, reproduce the issue using very little more than the posted code.

    – John Bollinger
    Nov 16 '18 at 15:29










1




1





I don't think the error you observe is related to the shell syntax you're using for testing whether ANDROID_NDK_ROOT is set. The error message itself leads me to suspect mismatched quotes, but I don't see that in the script excerpt. I can, however, reproduce the issue using very little more than the posted code.

– John Bollinger
Nov 16 '18 at 15:29







I don't think the error you observe is related to the shell syntax you're using for testing whether ANDROID_NDK_ROOT is set. The error message itself leads me to suspect mismatched quotes, but I don't see that in the script excerpt. I can, however, reproduce the issue using very little more than the posted code.

– John Bollinger
Nov 16 '18 at 15:29














1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer
























  • Thanks John. My test machine is bogged down with building Clang. I need to wait a couple of hours to free up some test time. I'm also out of upvotes so I need to wait until tonight to upvote.

    – jww
    Nov 16 '18 at 19:05














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331197%2fcheck-if-environment-variable-is-set-in-configure-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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.






share|improve this answer
























  • Thanks John. My test machine is bogged down with building Clang. I need to wait a couple of hours to free up some test time. I'm also out of upvotes so I need to wait until tonight to upvote.

    – jww
    Nov 16 '18 at 19:05


















1














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.






share|improve this answer
























  • Thanks John. My test machine is bogged down with building Clang. I need to wait a couple of hours to free up some test time. I'm also out of upvotes so I need to wait until tonight to upvote.

    – jww
    Nov 16 '18 at 19:05
















1












1








1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 16:10









John BollingerJohn Bollinger

84.5k74279




84.5k74279













  • Thanks John. My test machine is bogged down with building Clang. I need to wait a couple of hours to free up some test time. I'm also out of upvotes so I need to wait until tonight to upvote.

    – jww
    Nov 16 '18 at 19:05





















  • Thanks John. My test machine is bogged down with building Clang. I need to wait a couple of hours to free up some test time. I'm also out of upvotes so I need to wait until tonight to upvote.

    – jww
    Nov 16 '18 at 19:05



















Thanks John. My test machine is bogged down with building Clang. I need to wait a couple of hours to free up some test time. I'm also out of upvotes so I need to wait until tonight to upvote.

– jww
Nov 16 '18 at 19:05







Thanks John. My test machine is bogged down with building Clang. I need to wait a couple of hours to free up some test time. I'm also out of upvotes so I need to wait until tonight to upvote.

– jww
Nov 16 '18 at 19:05






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331197%2fcheck-if-environment-variable-is-set-in-configure-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values