Recipes

Posted by Beetle B. on Thu 20 June 2019

The recipe of a rule consists of one or more shell command lines to be executed, one at a time, in the order they appear. Typically, the result of executing these commands is that the target of the rule is brought up to date.

The default shell is /bin/sh unless specified otherwise.

Each line in the recipe must start with a tab (or the first character in the value of the ‘.RECIPEPREFIX’ variable; *note Special Variables::), except that the first recipe line may be attached to the target-and-prerequisites line with a semicolon in between.

Blank lines are ignored. But blank lines that begin with a tab are not. They are interpreted as an empty recipe.

A comment in a recipe is not a “make” comment. It is passed to the shell as is.

There’s a whole section on splitting lines, with subtleties. I did not preserve the info in there.

Make does expand variables. Now if you intended to have a shell variable, you need to escape the dollar sign:

for i in $(LIST); do \
    echo $$i; \
done

The first one is a Make variable. The second is a shell variable.

Make will print each line to the screen while executing. To suppress printing a line, start it with ‘@’.

If you invoke make with -n, it will echo all the lines, but execute none. Useful for debugging.

-s suppresses all echoing.

Each line has its own subshell! So don’t do a “cd” in one line and expect the effect to be preserved in the next. If you need to have these effects, ensure it is all in one line:

cd $(@D) && command...

You can force all lines in a recipe to take place in one shell like so:

.ONESHELL:
foo : bar/lose
        cd $(@D)
        gobble $(@F) > ../$@

If ‘.ONESHELL’ is provided, then only the first line of the recipe will be checked for the special prefix characters (‘@’, ‘-‘, and ‘+’). Subsequent lines will include the special characters in the recipe line when the ‘SHELL’ is invoked. There are some more details about this but I did not read it.

You can explicitly specify the shell using the SHELL variable.

Recursive make and parallel execution together would need special care. Now in general when you run in parallel, the output will be mangled. You can fix this by -O.

If you want make to keep executing even when a line gives an error, begin the line with a hyphen.

It has a whole section on recursive make.

If a sequence of commands is common to many recipes, you can declare it separately and invoke it. I didn’t note the details.

tags : make