makefiles

References

Summary

Makefiles are collaborations between Make and Bash

Tabs v.s. Spaces

Every command line must start with a single tab

Rules

The general structure of a makefile revolves and rules:

target: dependencies
        system_command

Targets

The target is the literal file you want the computer to target

If a file with the target filename is found in the filesystem, Make looks at the dependencies

Default

The first target in the file is the default

You can specify a target by running make target_name

target_name: dependencies
\t commands

For some reason I can't get my tabs working inside of the source-block, so I used i͡nstead.

Phony

If you just want Make to run terminal commands and not create any files, you create a phony target

.PHONY name

Dynamic Targets

We use single dollar signs to represent Makefile variables (as opposed to bash variables) or call built-in Make functions

$(MAKE) runs the command make

Dependencies

When evaluating a target, make will jump down to its dependencies, if they exist

Symbols

$@ Is shorthand for the name of the current target

% is a wildcard

@ tells Make to not echo the command text you wrote

echo "hello world"
hello world

Variables

:= declares a variable by evaluating the right side immediately

= declares a variable, but it doesn't evaluate the right side until the variable is actually used in a command

Bash

Variables

Bash shell variables are denoted with two dollar signs:

$$variable
$$var
$$name

Redirection

> overwrite redirect >> append redirect

Semicolons

Bash uses semicolons but Make doesn't. This can lead to some oddness in makefiles — you have to be clear on where semicolons are necessary and where they aren't.

Bash uses semicolons as a newline symbol

Comments

You can't put comments inside of multiline commands being sent to bash

Performance

Make tries to execute every single line as a separate shell command, executed in a separate isolated subshell

To keep multiple lines inside of the same shell (like when doing a for loop) we use

\ to tell us that the end of the current line isn't the end of what to send to this shell

NOTE: when we use \ Make appends the next line to the end of the current one

Directories

Directories are a bit special when it comes to Make

Changing the content of a file contained in a directory doesn't change its name or pointer, and so doesn't alter the directory's text file or its time stamp