functions.mk 1.95 KB
Newer Older
1 2 3
# NB: don"t use ANY quotes ('/") for put()/add()/set() arguments!
# shell will get confused by ' or args get spammed with "

4
# pay attention to the context functions get called in:
5 6
# e.g. features.in/syslinux/config.mk introduces conditionals

7 8 9
# this one adds whatever is given as an argument
put = $(and $(1),$(put_body))
define put_body
10 11
{ $(log_body); \
printf '%s\n' '$(1)' >> "$(CONFIG)"; }
12 13
endef

14
# these three take two args
15 16 17
# add() just appends an additive rule...
add = $(and $(1),$(2),$(add_body))
define add_body
18
$(if $(filter GLOBAL_% INFO_%,$(1)),$(warning add,$(1) might be a problem)) \
19 20
{ $(log_body); \
printf '%s += %s\n' '$(1)' '$(2)' >> "$(CONFIG)"; }
21 22 23
endef

# ...set() comments out any previous definition
24
# and then appends an assigning rule...
25 26
set = $(and $(1),$(2),$(set_body))
define set_body
27
{ $(log_body); \
Michael Shigorin's avatar
Michael Shigorin committed
28
sed -i 's|^$(1)[ 	]*[+?]*=.*$$|#& # overridden by $@|' "$(CONFIG)"; \
29
printf '%s = %s\n' '$(1)' '$(2)' >> "$(CONFIG)"; }
30 31
endef

32 33 34 35 36 37 38
# try() appends a conditionally-assigning rule
try = $(and $(1),$(2),$(try_body))
define try_body
{ $(log_body); \
printf '%s ?= %s\n' '$(1)' '$(2)' >> "$(CONFIG)"; }
endef

39 40 41 42 43 44 45 46
# xport() requests a variable to be exported to the scripts
xport = $(and $(1),$(xport_body))
define xport_body
{ $(log_body); \
v='$(1:GLOBAL_%=%)'; \
printf 'export GLOBAL_%s = $$(%s)\n' "$$v" "$$v" >> "$(CONFIG)"; }
endef

47
# if the rule being executed isn't logged yet, log it
48
define log_body
49 50
{ [ -s "$(CONFIG)" ] && \
	grep -q '^# $@$$' "$(CONFIG)" || printf '# %s\n' '$@' >> "$(CONFIG)"; }
51 52
endef

53 54 55
# in a use/feature/particularly target, we need a "feature" bit
add_feature = $(call add,FEATURES,$(word 2,$(subst /, ,$@)))

Michael Shigorin's avatar
Michael Shigorin committed
56
# convert tag list into a list of relative package list paths
57
# NB: tags can do boolean expressions: (tag1 && !(tag2 || tag3))
58 59
tags = $(and $(strip $(1)),$(addprefix tagged/,$(shell echo "$(1)" \
       | tags2lists pkg.in/lists/tagged)))
60 61 62

# toplevel Makefile convenience
addsuffices = $(foreach s,$(1),$(call addsuffix,$s,$(2)))