Make Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 38: Line 38:
The file name of the target of the rule. In a pattern rule that has multiple targets <code>$@</code> is the name of whichever target caused the rule’s recipe to be run.
The file name of the target of the rule. In a pattern rule that has multiple targets <code>$@</code> is the name of whichever target caused the rule’s recipe to be run.
===<tt>$<</tt>===
===<tt>$<</tt>===
The name of the first prerequisite. For
The name of the first prerequisite. For this rule:
<syntaxhighlight lang='make'>
types.gen.go: petstore.yaml
oapi-codegen -generate types --package petstore $< > $@
</syntaxhighlight>
<code>$<</code> contains "petstore.yaml"

Revision as of 18:17, 26 January 2024

Internal

Rule

A makefile consists of rules with the following syntax:

target ...: prerequisites
    recipe

A target can be either the name of a file that is generated by the recipe, or the name of an action to carry out. These are phony named phony targets, because they are not files.

A prerequisite is a file that is used as input to create the target. A target often depends on several files.

A recipe is an action that "make" carries out. A recipe may have more than one command, either on the same line or each on its own line. You need to put a tab character at the beginning of every recipe line. Is this still true? Usually the recipe (re-)creates the target file if any of the prerequisites change. However, the rule that specifies a recipe for the target need not have prerequisites.

.PHONY

By default, make targets are assumed to be files on disk. They will be built from other files as result of executing make with that name as argument. However, sometimes you want make to run commands that do not represent physical files in the filesystem, and if there's a file with the same name in the filesystem, make will be confused and will pick up the file. To avoid this, you can disambiguate with .PHONY:

.PHONY: clean
clean: 
  rm -rf *.o


Variables

CURDIR

CURDIR refers to the directory make is run from:

something:
	"$(CURDIR)/scripts/something.sh"

Automatic Variables

https://www.gnu.org/software/make/manual/make.html#Automatic-Variables

$@

The file name of the target of the rule. In a pattern rule that has multiple targets $@ is the name of whichever target caused the rule’s recipe to be run.

$<

The name of the first prerequisite. For this rule:

types.gen.go: petstore.yaml
	oapi-codegen -generate types --package petstore $< > $@

$< contains "petstore.yaml"