Rules

Table of Contents

Define targets with recursive wildcards howto

Manage background processes howto

test: db
  pipenv run pytest

db: db.pid

db.pid:
  sls dynamodb start &> 'db.log' & echo "$$!" > 'db.pid'

db-stop: PID  = $(shell cat db.pid)
db-stop: PGID = $(shell ps -o pgid= $(PID))
db-stop:
  -kill -INT -$(PGID)
  -rm 'db.pid'

Multiple Rules for One Target discussion

objects = foo.o bar.o
foo.o : defs.h
bar.o : defs.h test.h
$(objects) : config.h

For using prerequisites in recipes, you need to use $< or $+:

Double-Colon(::) Rules discussion

Implicit Rules(Pattern Rules) vs Static Pattern Rules discussion

The difference with Implicit Rules is in how make decides when the rule applies:

Implicit Rules
  • An implicit rule can apply to any target that matches its pattern
  • It does apply only when the target has no recipe otherwise specified, and only when the prerequisites can be found.
Static Pattern Rules
  • A static pattern rule applies to the precise list of targets that you specify in the rule.
  • It cannot apply to any other target and it invariably does apply to each of the targets specified.

Why exported variables in Makefile is not received by executable? discussion

Split Recipe Lines howto

all : ; @echo 'hello \
        world' ; echo "hello \
    world"

What if multiple pattern rules match a target? discussion

3.81 or before
  • make would choose the first rule(in the order the rule is defined in Makefile)
From 3.82
  • make would choose the shortest stem pattern rule.
  • This produces the usually-desired behavior where more specific patterns are preferred.

What is stem in Pattern Rules discussion

stem is matched portion of the rule:

all: sub/foo.x

# if matched, 'sub/foo' is the stem
%.x:
    @echo "$*"

# if matched, 'foo' is the stem
sub/%.x:
    @echo "$*"

Automatic Prerequisites, Auto-Dependency Generation discussion

In my opinion, it is somewhat complicated and there are a lot of tricky parts. Consider only using this pattern when you are sure that it is of huge benefit.

Chained Rules discussion

Special Recipe Prefixes(@echo, -rm, etc) discussion

@
suppresses the normal echo of the command that is executed.
-
ignores the exit status of the command that is executed. Normally, a non-zero exit status would stop that part of the build.
+

forcefully executes the command even under non-executing options like:

  • -n for doing nothing, just print
  • -t for touch
  • -q for question of whether there is needed to be updated.

Can I define a pattern rule with no recipe discussion

No. When you define a pattern rule with no recipe, it means just canceling implicit rules related to the pattern. It does not mean adding any dependencies.