Search code examples
autotoolsm4

how to use variables for strings in autoconf:configure.ac


How to use variables for messages inside configure.ac

if test "$foo" = "yes"; then
    AC_MSG_ERROR([this string is being used in WARN as well as ERROR])
else
    AC_MSG_WARN([this string is being used in WARN as well as ERROR])
fi

It would make sense to define the string "this string is being used in WARN as well as ERROR" in a variable and then use that variable in both AC_MSG_WARN and AC_MSG_ERROR. What is the best way to do that ?

In addition to that, does m4 has any macro which can replace this entire if else by taking string and $foo as argument ?


Solution

  • This should work:

    msg="this string is being used in WARN as well as ERROR"
    if test "$foo" = "yes"; then
        AC_MSG_ERROR([$msg])
    else
        AC_MSG_WARN([$msg])
    fi
    

    In addition to that, does m4 has any macro which can replace this entire if else by taking string and $foo as argument ?

    If you write one, it will. :-). The if-else isn't in m4, it's in the output of m4, the configure shell script. Something like:

    AC_DEFUN([AX_TEST_FOO], [
        pushdef([MSG],$1)
        pushdef([FOO],$2)
        AS_IF([test $FOO = yes], [AC_MSG_ERROR([$MSG])], [AC_MSG_WARN([$MSG])])
        popdef([FOO])
        popdef([MSG])
    ])
    

    called like:

    AX_TEST_FOO(["this string is being used in WARN as well as ERROR"], [$foo])
    

    should be close. I didn't try it.