Search code examples
autotoolsautoconf

Colorize output from configure script


I'm looking for a way to colorize the output from ./configure so that all instances of yes at the end of a check show in one color and no in another. I think there may be a way of creating the echo message using AS_IF to test the check result and then set the output using the usual bash color commands. I'd obviously prefer to not have to spend time doing this if it's already been done.


Solution

  • Lucky for you, Autoconf is basically one giant C-preprocessor-abuse, so its internals are all exposed and it's easy to do something hacky that, fair warning, might not carry over from one Autoconf version to the next.

    The definition of AC_MSG_RESULT (which prints most of those yes/no messages) is found in /usr/share/autoconf/autoconf/general.m4, and is quite simply defined as printing a given message to the log file as well as to the terminal:

    m4_define([AC_MSG_RESULT],
    [{ _AS_ECHO_LOG([result: $1])
    _AS_ECHO([$1]); }dnl
    ])
    

    Since the Autoconf internals called in that macro are exposed to your configure.ac, you can just override AC_MSG_RESULT with your own macro that calls them. Here's one that worked for me, printing "yes" in green, "no" in red, and all other results in blue. Include it before any other macro calls in your configure.ac:

    m4_pushdef([AC_MSG_RESULT], [
        { result="$1"
        _AS_ECHO_LOG([result: $result])
        AS_CASE(["x$result"],
            [xnone\ needed], _AS_ECHO([$(tput setaf 4)$result$(tput sgr0)]),
            [xyes*], _AS_ECHO([$(tput setaf 2)$result$(tput sgr0)]),
            [xno*], _AS_ECHO([$(tput setaf 1)$result$(tput sgr0)]),
            _AS_ECHO([$(tput setaf 4)$result$(tput sgr0)])); }dnl
    ])
    

    But seriously, don't do this.