Writing Rules

Posted by Beetle B. on Wed 12 June 2019

If you run make without a target, it picks the first target of the first rule.

But a target beginning with a period cannot be the default rule unless it contains a slash. A target that defines a pattern rule has no effect on the default goal.

Recipes should begin with a tab.

A general rule looks like this:

TARGETS : PREREQUISITES
        RECIPE
        ...

You can use wildcards in target file names.

You can have order only prerequisites:

TARGETS : NORMAL-PREREQUISITES | ORDER-ONLY-PREREQUISITES

You don’t need a normal prerequisite to have an order only prerequisite.

Consider an example where your targets are to be placed in a separate directory, and that directory might not exist before ‘make’ is run. In this situation, you want the directory to be created before any targets are placed into it but, because the timestamps on directories change whenever a file is added, removed, or renamed, we certainly don’t want to rebuild all the targets whenever the directory’s timestamp changes. One way to manage this is with order-only prerequisites: make the directory an order-only prerequisite on all the targets.

Wildcard expansion doesn’t happen when you define a variable.

If you do:

$(wildcard PATTERN...)

This string, used anywhere in a makefile, is replaced by a space-separated list of names of existing files that match one of the given file name patterns. If no existing file name matches a pattern, then that pattern is omitted from the output of the ‘wildcard’ function.

The value of the ‘make’ variable ‘VPATH’ specifies a list of directories that ‘make’ should search. Directories are separated by blanks or colons.

Similar to the ‘VPATH’ variable, but more selective, is the ‘vpath’ directive (note lower case), which allows you to specify a search path for a particular class of file names: those that match a particular pattern. Thus you can supply certain search directories for one class of file names and other directories (or none) for other file names.

There are three forms of the ‘vpath’ directive:

  • vpath PATTERN DIRECTORIES’: Specify the search path DIRECTORIES for file names that PATTERN.
  • vpath PATTERN’: Clear out the search path associated with PATTERN.
  • vpath’: Clear all search paths previously specified with ‘vpath’ directives.

A ‘vpath’ pattern is a string containing a ‘%’ character. The string must match the file name of a prerequisite that is being searched for, the ‘%’ character matching any sequence of zero or more characters. For example, ‘%.h’ matches files that end in ‘.h’.

I did not read the rest of the directory search section, or chapter.

tags : make