Differences between revisions 1 and 2
Revision 1 as of 2022-03-07 18:41:06
Size: 3116
Comment:
Revision 2 as of 2023-04-04 15:15:52
Size: 790
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
'''`make(1)`''' is a generic toolchain for compiling software. It is part of the POSIX standard, though GNU make has some slight differences to BSD make. '''`make(1)`''' is a build system. While part of the POSIX standard, GNU `make(1)` has slight divergences in behavior.
Line 11: Line 11:
== Rules == == Installation ==
Line 13: Line 13:
A rule has three components: a '''target''', 0 or more '''prerequisites''', and 0 or more '''recipes'''. All [[Linux]] and [[BSD]] distributions offer a `make` package.
Line 15: Line 15:
{{{
hello: hello.cpp
    g++ -o hello hello.cpp

}}}

`hello` is a target. With this `Makefile` in the working directory, it can be built by calling `make hello` in a terminal.

`hello.cpp` is a prerequisite. There can be any number of prerequisites; anything after the colon on the target line is treated as one. Before `hello` can be built, this prerequisite must be satisfied by either...

 * running another rule to build `hello.cpp`
 * locating `hello.cpp` as a file in the working directory
   * Note: if a prerequisite is satisfied with a file '''and''' it is older than the target file, `make(1)` will short circuit as the target does not need to be re-built

The remainder is a recipe. Every tabbed, newline-delimited line following a target line is a recipe. Each recipe is run in a discrete subprocess.



=== Noisy Rules ===

When executing a rule, each recipe is printed to the terminal before the recipe is executed. This can improve debugging. This can also create an extremely noisy terminal.

To make a recipe ''not'' print before executing, prefix it with `@`.

To make a recipe silent, redirect the output (i.e. `>/dev/null 2>&1`).

If errors can be ignored, consider adding `|| true` to the end.



=== Variables in Rules ===

Each recipe is executed in a discrete subprocess. As such, changes to environment variables disappear after execution.

{{{
demo:
    @export DEMO="YES"
    @echo "Do variables carry over between recipes? $${DEMO:-NO}!"
}}}

This will print `Do variables carry over between recipes? NO!`.

Especially on BSD distributions, it may be necessary to specifically install GNU `make(1)`. This is usually available as `gmake`.
Line 63: Line 21:
== Phony Rules ==

`make(1)` can also be used to automate processes that aren't necessarily about making a file.

A common example is a `clean` rule.
== Usage ==
Line 70: Line 24:
.PHONY: clean
clean:
    rm -f hello
make [TARGET]
Line 75: Line 27:
The `.PHONY` component ensures that `make(1)` does not look for a `clean` file. If there were such a file, `make(1)` might short circuit; it would believe that the rule is already satisfied. If a target is not specified, the default target is used.



=== Recipes ===

Recipes define the build process for a target. See [[Makefile]] for details.
Line 81: Line 39:
== Pattern Rules == == See also ==
Line 83: Line 41:
Many programs have a generic and abstract-able build process. Consider the following: [[https://man.archlinux.org/man/make.1|make(1)]]
Line 85: Line 43:
{{{
%.o : %.c
    $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
}}}
[[https://man.archlinux.org/man/make.1p|make(1p)]]
Line 90: Line 45:
For any `foo.o` file, `make(1)` knows that it can be built with `cc -c foo.c -o foo.o`.



=== Automatic Variables ===

Automatic variables are critical to making use of pattern rules. These are populated for every rule.

||'''Variable'''||'''Contents'''||
||`$@` ||file name of the target||
||`$^` ||names of '''all''' the prerequisites, with spaces between them||
||`$<` ||name of the '''first''' prerequisite||
||`$?` ||names of '''all''' the prerequisites that are newer than the target, with spaces between them||
[[Makefile]]

Make

make(1) is a build system. While part of the POSIX standard, GNU make(1) has slight divergences in behavior.


Installation

All Linux and BSD distributions offer a make package.

Especially on BSD distributions, it may be necessary to specifically install GNU make(1). This is usually available as gmake.


Usage

make [TARGET]

If a target is not specified, the default target is used.

Recipes

Recipes define the build process for a target. See Makefile for details.


See also

make(1)

make(1p)

Makefile


CategoryRicottone

Make (last edited 2023-05-31 16:28:15 by DominicRicottone)