Any interesting uses of Makefiles to share? Any interesting uses of Makefiles to share? bash bash

Any interesting uses of Makefiles to share?


Make's parallelism is particularly handy for shell scripting. Say you want to get the 'uptime' of a whole set of hosts (or basically perform any slow operation). You could do it in a loop:

cat hosts | while read host; do echo "$host: $(ssh $host uptime)"; done

This works, but is slow. You can parallelise this by spawning subshells:

cat hosts | while read host; do (echo "$host: $(ssh $host uptime)")&; done

But now you have no control over how many threads you spawn, and CTRL-C won't cleanly interrupt all threads.

Here is the Make solution: save this to a file (eg. showuptimes) and mark as executable:

#!/usr/bin/make -fhosts:=$(shell cat)all: ${hosts}${hosts} %:        @echo "$@: `ssh $@ uptime`".PHONY: ${hosts} all

Now running cat hosts | ./showuptimes will print the uptimes one by one. cat hosts | ./showuptimes -j will run them all in parallel. The caller has direct control over the degree of parallelisation (-j), or can specify it indirectly by system load (-l).


Other than obvious uses in programming, I've seen Makefiles used to perform repetitive tasks on servers to manage system settings such as user/group creation, package upgrades/updates, and copying config files into place. I have also seen (and used) rake to do this in Ruby based environments.

The most complex example of this I have heard of is ISConf, a configuration management tool. I heard about it from Luke Kanies, the author of Puppet, on his blog.


Depending on the variety of 'make' , you can sail through dependencies of tasks by using executable makefiles vs shell scripts .. i.e. in a scenario where one init script needs to start 10 services ... one after the other (i.e service two needs service one to be up and alive prior to starting), its easily done via a makefile.

A lot of GNU/Linux packagers make use of this, i.e. debian/rules and others. So yeah, if done correctly, make is just as good at starting programs as it is as building them.

The problem is, not everyone has a build tool chain installed .. so shell scripts or batch files remain the portable (hence 'correct') approach.