Contents

Makefile

Contents
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# command to execute if no argument
default: help

# set default variable
# can be override by setting in argument: make app_name=popo
app_name=foobar
# set a variable from command
project_name=$(shell basename "$(PWD)")
# only set variable if env variable is not already defined
JAVA_HOME?=/opt/java
# stops commandline arguments from changing this variable
override username=admin

# variables can be used using $() or ${}
echo-variables:
	@echo ${app_name}
	@echo $(project_name)

# avoid phony rules breaking when a real file has the same name as the command
.PHONY: help
# command example that read the Makefile and print all lines with prefix ##
help: Makefile
	@echo
	@echo " Choose a command run in "$(project_name)":"
	@echo
	@sed -n 's/^##//p' $< | column -t -s ':' |  sed -e 's/^/ /'
	@echo

foobar:
	cat foo > foo.txt
	# @ stops the command from being echoed to stdout
	@cat foo > foo.txt
	# - means that make will keep going even if there is an error
	-@cat foo > foo.txt


file0.txt:
	echo "foo" > file0.txt

# this rule will only run if the file0.txt is newer than file1.txt
file1.txt: file0.txt
	cat file0.txt > file1.txt

# a rule can have multiple targets and multiple prerequisites
all: file0.txt file1.txt

process: file*.txt  #using a wildcard to match filenames
	@echo $^    # $^ is a variable containing the list of prerequisites
	@echo $@    # prints the target name
	@echo $<    # the first prerequisite listed
	@echo $?    # only the dependencies that are out of date
	@echo $+    # all dependencies including duplicates (unlike normal)

# multi lines are possible, but the shell receives the whole command concatenated into a single
# line, so we also need to terminate with a semi-colon
#
# escape with a $ for using bash scripts and variables
multi-lines:
	@for f in $$(ls); do \
		echo $$f; \
	done