[linux-elitists] Fun* with inotify and make

Don Marti dmarti at zgp.org
Mon Jul 29 05:47:19 PDT 2013

begin Teh Entar-Nick quotation of Mon, Jul 29, 2013 at 11:04:52AM +0000:
> Don Marti:
> > Arr, if only we had a program that would parse
> > Makefiles...
> > 
> > _make_prereqs() {
> >     make -dnr $* | tr ' ' '\n' | \
> >         grep ".*'.$" | grep -o '\w.*\b'
> > }
> Good one!  You'll want an inotify to refresh this when the Makefile
> itself changes, of course.

Yes, I added Makefile right after I sent the last mail.

_make_prereqs() {
    echo "Makefile"
    make -dnr $* | tr ' ' '\n' | \
        grep ".*'.$" | grep -o '\w.*\b'

> Only problem I see is that you're pulling out all files, including
> *target* files.  That could get annoying when e.g. I pull up a .html
> file in vim to try something out, and when I save it my hack gets
> reverted automatically.

I try to always do that kind of modifications on a
copy of the target file so that I don't overwrite it
myself by mistake.

Also, somewhat improved version of the main loop.
Exit status of inotifywait is 0 if the watched-for
event happens, 1 if a different event happens, so this
should only run make if a file has an "interesting"

    while true; do
        inotifywait -e close_write -e moved_to `make_prereqs $*` && \
            make $*

> Also, it seems to ignore explicit prerequisites!  If I put a
> metadata.xml to CC-license my epub files, it never shows up in that
> output:
>     %.epub: %.md %_cover.jpeg metadata.xml
>             pandoc -S --epub-metadata=metadata.xml --epub-cover-image=$*_cover.jpeg -o $@ $<
> Alas:
>     $ make -dnr all | grep -c xml
>     0
>     $ make -dnr foo.epub | grep -c xml
>     7
> Poking around in -p and -B don't seem to help much here.

Strange.  I tried adding a "metadata.xml" prerequisite and got...

    $ make_prereqs all
    index.html index.md Makefile metadata.xml templates/default.html 

    $ make -dnr index.html | grep xml
      Considering target file `metadata.xml'.
       Looking for an implicit rule for `metadata.xml'.
       No implicit rule found for `metadata.xml'.
       Finished prerequisites of target file `metadata.xml'.
      No need to remake target `metadata.xml'.
     Prerequisite `metadata.xml' is newer than target `index.html'.

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