Is there a smarter alternative to “watch make”?

前端 未结 11 592
渐次进展
渐次进展 2021-02-01 15:28

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 s

相关标签:
11条回答
  • 2021-02-01 15:57

    You could change your make file to output a growl (OS X) or notify-send (Linux) notification. For me in Ubuntu, that would show a notification bubble in the upper-right corner of my screen.

    Then you'd only notice the build when it fails.

    You'd probably want to set watch to only cycle as fast as those notifications can display (so they don't pile up).

    0 讨论(0)
  • 2021-02-01 16:02

    This shell script uses make itself to detect changes with the -q flag, and then does a full rebuild if and only if there are changes.

    #!/bin/sh
    
    while true;
    do
      if ! make -q "$@";
      then
        echo "#-> Starting build: `date`"
        make "$@";
        echo "#-> Build complete."
      fi
      sleep 0.5;
    done
    
    • It does not have any dependencies apart from make.

    • You can pass normal make arguments (such as -C mydir) to it as they are passed on to the make command.

    • As requested in the question it is silent if there is nothing to build but does not swallow output when there is.

    • You can keep this script handy as e.g. ~/bin/watch-make to use across multiple projects.

    0 讨论(0)
  • 2021-02-01 16:04

    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.

    0 讨论(0)
  • 2021-02-01 16:04

    @Dobes Vandermeer -- I have a script named "mkall" that runs make in every subdirectory. I could assign that script as a cron job to run every five minutes, or one minute, or thirty seconds. Then, to see the output, I'd redirect gcc results (in each individual makefile) to a log in each subdirectory.

    Could something like that work for you?

    It could be pretty elaborate so as to avoid makes that do nothing. For example, the script could save the modify time of each source file and do the make when that guy changes.

    0 讨论(0)
  • 2021-02-01 16:04

    You could try using something like inotify-tools. It will let you watch a directory and run a command when a file is changed or saved or any of the other events that inotify can watch for. A simple script that does a watch for save and kicks off a make when a file is saved would probably be useful.

    0 讨论(0)
  • 2021-02-01 16:05

    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.

    0 讨论(0)
提交回复
热议问题