Coloring Make Output

Considering how verbose make output is, it's really easy to miss warnings or errors. What we need is a bit of color. However, make being such an old program, doesn't support any ANSI coloring. However, since both errors and warnings have standardized formats, it's relativelly easy to use grep to color them.

For example:

Terminal
make | grep --color=always -e '^Makefile:[0-9]\+:.*' -e '^'

This will color all lines following the Makefile:number: format in default color. Extending this for catching errors is similar as there is just an extra match of ".Stop":

Terminal
make | grep --color=always -e '^Makefile:[0-9]\+:.* Stop\.$' -e '^'

But what if we want to have warnings in yellow and errors in red? Well, then we get to use GREP_COLOR variable and pass grep twice:

Terminal
make | GREP_COLOR='01;31' grep --color=always -e '^Makefile:[0-9]\+:.* Stop\.$' -e '^' \
| GREP_COLOR='01;33' grep --color=always -e '^Makefile:[0-9]\+:.*' -e '^'

And yes, the order of greps is important as we first want to capture errors. Matched lines are to be colored red (01;31) and prefixed with ANSI escape sequence thus preventing the second grep matching. Lines matching the second grep will get similar treatment, just in yellow (01;33).

Instead of remembering this, we can create a new amake function that will do the coloring:

.bash_aliases
function amake() {
make "$@" 2>&1 | \
GREP_COLOR='01;31' grep --color=always -e '^Makefile:[0-9]\+:.* Stop\.$' -e '^' | \
GREP_COLOR='01;33' grep --color=always -e '^Makefile:[0-9]\+:.*' -e '^'
}

So, now when we call function, we can see issues a bit more quickly:

Terminal
amake

Leave a Reply

Your email address will not be published. Required fields are marked *