Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a smarter alternative to "watch make"?

Tags:

makefile

watch

I ran into this useful tip that if you're working on files a lot and you want them to build automatically you run:

watch make

And it re-runs make every couple seconds and things get built.

However ... it seems to swallow all the output all the time. I think it could be smarter - perhaps show a stream of output but suppress Nothing to be done for 'all' so that if nothing is built the output doesn't scroll.

A few shell script approaches come to mind using a loop and grep ... but perhaps something more elegant is out there? Has anyone seen something?

like image 337
Dobes Vandermeer Avatar asked Sep 24 '11 14:09

Dobes Vandermeer


3 Answers

Using classic gnu make and inotifywait, without interval-based polling:

watch:
    while true; do \
        make $(WATCHMAKE); \
        inotifywait -qre close_write .; \
    done

This way make is triggered on every file write in the current directory tree. You can specify the target by running

make watch WATCHMAKE=foo
like image 198
Otto Allmendinger Avatar answered Oct 18 '22 23:10

Otto Allmendinger


This one-liner should do it:

while true; do make --silent; sleep 1; done

It'll run make once every second, and it will only print output when it actually does something.

like image 38
wch Avatar answered Oct 18 '22 21:10

wch


Here is a one-liner:

while true; do make -q || make; sleep 0.5; done

Using make -q || make instead of just make will only run the build if there is something to be done and will not output any messages otherwise.

You can add this as a rule to your project's Makefile:

watch:
    while true; do $(MAKE) -q || $(MAKE); sleep 0.5; done

And then use make watch to invoke it.

This technique will prevent Make from filling a terminal with "make: Nothing to be done for TARGET" messages.

It also does not retain a bunch of open file descriptors like some file-watcher solutions, which can lead to ulimit errors.

like image 13
Chris McCormick Avatar answered Oct 18 '22 23:10

Chris McCormick