Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any interesting uses of Makefiles to share?

"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?

like image 957
mjy Avatar asked Dec 27 '08 18:12

mjy


People also ask

What is the use of makefiles?

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.

Are makefiles still used?

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.

What should I name my makefiles?

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.

Where do you put 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.


1 Answers

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).

like image 58
jturner Avatar answered Nov 15 '22 15:11

jturner