-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
66 lines (57 loc) · 1.95 KB
/
Makefile
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
62
63
64
65
66
# Normally make prints each line of the recipe before it is executed
# When a line starts with `@`, the echoing of that line is suppressed
Q = @
export Q
SUBDIRS = c cpp
# Functions for Conditionals
#
# $(if condition, then-part[, else-part])
#
# Note that `else-part` is optional, such as in this example
#
#
# The foreach Function
#
# $(foreach var, list, text)
#
# It causes one piece of text to be used repeatedly, each time with
# a different substitution performed on it.
foreach-subdir-all := $(if $(subdirs), \
$(foreach d, $(subdirs), \
$(MAKE) -C $(d); cd ..),:)
foreach-subdir-clean := $(if $(subdirs), \
$(foreach d, $(subdirs), \
$(MAKE) -C $(d) clean; cd ..),:)
# Notice that above variables look so similar that should be abstracted
# as function
#
# The call Function
#
# $(call variable, param, param, ...)
#
# This example doesn't work. According to GNU make manual, the call function
# expands the param arguments before assigning them to temporary variables.
# This means that `variable` values containing references to built-in functions
# that have special expansion rules, like `foreach` or `if`, may not work
# as you expect.
# Hence I guess it is not good idea to mix `call` together with `foreach` or
# `if` to use like that
#
# It turns out that I misunderstood that. The reason why the call function here
# didn't work before is simply that the variable is expanded immediately due to
# improper choice for variable assignment signs (should be '=' instead of ':=')
#
# Though I know better about call function in this example, it is not best practice
# to call foreach in recursive use of make
# See https://www.gnu.org/software/make/manual/make.html#Phony-Targets
foreach-subdir-make = $(if $(subdirs), \
$(foreach d, $(subdirs), \
$(MAKE) -C $(d) $(1); cd ..))
subdirs: $(SUBDIRS)
$(SUBDIRS):
$(MAKE) -C $@
clean:
$(Q)for dir in $(SUBDIRS); do \
$(MAKE) -C $$dir clean; \
done
.PHONY: subdirs all clean $(SUBDIRS)