"make" is not only useful for building your programming project, but it seems to be under-used in other areas.
For example, many shell scripts can be rewritten as Makefiles to allow independent parts to run in parallel (with "make -jXX") to keep all your CPU cores busy, with the explicitly declared dependencies as an added benefit in case you'd ever consider reordering some tasks with side effects in your shell script.
Do you have any interesting stories with unusual uses of make / Makefiles to share? Do you use some other utility as a cheap job scheduler?
Makefile sets a set of rules to determine which parts of a program need to be recompile, and issues command to recompile them. Makefile is a way of automating software building procedure and other complex tasks with dependencies. Makefile contains: dependency rules, macros and suffix(or implicit) rules.
make was first developed in 1976, and yet it's still widely used, predominantly in application development where you need to compile binaries. While makefiles are quite uncommon in Web development in general, at SSENSE we have found an unconventional use-case for it.
Normally you should call your makefile either `makefile' or `Makefile'. (We recommend `Makefile' because it appears prominently near the beginning of a directory listing, right near other important files such as `README'.) The first name checked, `GNUmakefile', is not recommended for most makefiles.
some projects put their makefile in src/ subdirectory of the root directories of the projects, some projects put their makefiles in the root directory of the project.
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 -f
hosts:=$(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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With