Thursday, June 14, 2012

Emacs, your makefile friend

After long hiatus I find myself against writing (editing) makefiles. Using Emacs compile command makes all the difference. Emacs runs make in another process, sending output to an independent buffer (window, tab, panel) default named *compilation*.

In the *compilation* buffer, Emacs colorizes make's output. This greatly aids comprehension. As icing, Emacs recognizes output patterns from dozens of popular tools, and highlights them as information, warning or error as appropriate.

Emacs simplifies navigation with hyperlinking. The TAB key in *compilation* steps through warnings and errors. Magic.

As a bonus, I looked up common patterns stored in the Emacs variable, compilation-error-regexp-alist. The info page for this variable includes a link to "compilation.txt", a complete list of sample output for each supported tool. It was trivial for me to find right at the top:

* GNU style

symbol: gnu

foo.c:8: message
../foo.c:8: W: message
/tmp/foo.c:8: warning: message

That last line was my ticket. I arrange to echo similar text when something needs attention in my shell command, but without failing the entire build. I take care to break the output into separate shell echo commands so Emacs does not match prematurely when make displays executed commands. I could have prefixed my shell line with "@" (ampersand) to suppress make from printing it, but coding standards here discourage that.

a-file:
        if $(some_bad_condition_set_in_makefile) ; then \
        echo -n $@ ; \
        echo :1: warning: $@ is foobar. >&2 ; fi

I emulate the output of one of the tools Emacs groks, and I get the same magical colorizing and hyperlinking with my output:

making in /some/path
if true ; then \
 echo -n a-file ; \
 echo :1: Warning: a-file is foobar. >&2 ; fi
a-file:1: warning: a-file is foobar.

In *compilation* the TAB key jumps me to the start of a-file when it exists.

Post a Comment