Skip to content

Instantly share code, notes, and snippets.

@newhouseb
Created September 29, 2011 09:35
Show Gist options
  • Save newhouseb/1250398 to your computer and use it in GitHub Desktop.
Save newhouseb/1250398 to your computer and use it in GitHub Desktop.
makewatch

A bash function that makes a makefile every time you save a file that make depends on.

First install inotify-tools if you haven't already:

apt-get install inotify-tools

Then put this in your .bashrc (or just run it in your current session).

function makewatch() { while true; do make $1; strace -e trace=stat make -q 2>&1 | sed 's/^[^"]*"//' | grep " = 0" | sed 's/".*//' | xargs inotifywait -e create -e modify; done }

Then just run

makewatch

or

makewatch target

And voila, make will run anytime you change a dependency.

The basic idea behind this (if the bash doesn't make sense) is we first run make, then run make -q which just tells you if make needs to be rerun but doesn't actually make anything and strace it to figure out which files it looked at to come so such a conclusion. Then we prune these filenames out of strace and tell inotify to block until one of them is modified at which point we restart the whole process (i.e. go back to the beginning of the loop).

Note: It probably makes more sense to parse make -p than make -q, especially since OSX doesn't have strace (only dtrace which requires root). A cross platform way to do this SHOULD be using watchdog (the python package) w/ the following command:

watchmedo shell-command --command='make -pqR | egrep "^[^#:. ]+\\.[^#: ]+:" | sed "s#^#'"$PWD"'/#g" | grep "${watch_src_path}:"; exit' -R -w

But watchdog is buggy and doesn't properly detect subdirectory events. So this slightly more aggressive maker will have to do for now if you want cross platform.

function makewatch() { make $1; watchmedo shell-command -w -c "make $1" -i '*/.*' -R; }

@maxdeliso
Copy link

makes no effort to format or make the script easy to parse by humans
makes remarks like "if the bash doesn't make sense"
trolling.

@maxdeliso
Copy link

useful tool though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment