[linux-elitists] Fun* with inotify and make

Don Marti dmarti at zgp.org
Fri Jul 26 17:44:26 PDT 2013


Shell function, "makewatch" to re-run make when
the relevant files change.  Surprisingly useful
for refreshing the HTML version of a page when the
Markdown source changes, especially if you're into the
Auto Reload extension.  I also plan to use this for
"make test".

Andrew Cowie has something similar...
  http://blogs.operationaldynamics.com/andrew/software/haskell/rebuilding-via-inotify
...but I like mine better because it doesn't have to
special-case the irrelevant files--it actually looks
for what "make" needs.

I think I can get rid of the "cut" in the helper
function if I learn more about grep.

make_prereqs() {
    # finds all prerequisites mentioned in a Makefile
    # that are actual files.
    for f in `egrep '^\S.*:\S*' Makefile | \
        cut -d ':' -f 2 | tr -s ' ' '\n' | \
        sort -u`; do 
    [ -e $f ] && echo -n "$f ";
    done
    echo
}

makewatch() {
    # re-run make, with the supplied arguments, when a
    # Makefile prerequisite changes.  Example:
    #   makewatch test
    # Fun to use with Auto Reload and Pandoc
    #   https://addons.mozilla.org/en-US/firefox/addon/auto-reload/?src=api
    #   http://johnmacfarlane.net/pandoc/
    if [ ! -e Makefile ]; then
        echo "No Makefile in this directory." >&2
        return
    fi
    make $*
    while true; do
        inotifywait -e close_write -e moved_to `make_prereqs`
        make $*
    done
}

* actual fun achived depends on Makefile content.

-- 
Don Marti                      +1-510-332-1587 (mobile)
http://zgp.org/~dmarti/        Alameda, California, USA
dmarti at zgp.org


More information about the linux-elitists mailing list