-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.mk
121 lines (106 loc) · 3.81 KB
/
functions.mk
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
ifndef BS__FUNCTIONS_MK
BS__FUNCTIONS_MK=1
# dirname
#
# Arguments: a file name
#
# Returns the directory containing the given file name. Differs from
# $(dir $(FOO)) in that this won't have a trailing slash. Grr.
dirname=$(patsubst %/,%,$(dir $(1)))
# get-default
#
# Arguments: argument string, default string
#
# If the argument string is non-empty, return it. Else, return the default string
get-default = $(if $(1),$(1),$(2))
# get-unless-default
#
# Arguments: argument string, default string
#
# If the argument string isn't the same as the default string, return it
get-unless-default = $(if $(filter-out $(2),$(1)),$(1))
# add-dir
#
# Arguments: list of directories to add.
#
# Adds all the given directories to the list of those which will be
# automatically created.
#
# This is stupid: one version of this function works on make 3.81
# but not make 3.82, while the version that works on 3.82 spews
# screenfuls of warnings on 3.81. Ugly hack time.
ifeq ($(MAKE_VERSION),3.81)
_do_add_dir=$(if $(filter $(1),$(DIRS)),,$(eval DIRS += $(1)))
_add_one_dir=$(call _do_add_dir,$(patsubst %/,%,$(1)/))
else
_do_add_dir=$(if $(filter $(1),$(DIRS)),,$(eval DIRS += $(1) $(1)/))
_add_one_dir=$(call _do_add_dir,$(patsubst %/,%,$(1)))
endif
add-dir=$(foreach d,$(1),$(call _add_one_dir,$(patsubst %/,%,$(d))))
# save-vars
#
# Arguments: list of variable names to save, unique tag name under
# which to save them.
#
# Saves the current values of all the given variables under the tag name given,
# to be later retrieved by load-vars.
#
save-vars=$(eval __saved_vars_$(2)_=$(1)) \
$(foreach v,$(1), $(eval __saved_$(v)_$(2)_=$(value $(v))))
# load-vars
#
# Arguments: tag name
#
# Restores the variables saved under the given tag name to their saved values.
#
load-vars=$(foreach v,$(__saved_vars_$(1)_), $(eval $(v)=$(value __saved_$(v)_$(1)_)))
# expand-target-variable
#
# Arguments: target name, variable name
#
# Returns the concatenated value of the given variable to use when building the
# named target.
#
# This consists of:
# - the global default value
# - the value specified in the target's subdirectory (if the variable name is
# in SUBDIR_VARIABLES)
# - the target-specific value specified in the target's subdirectory (if the
# variable name is in PER_TARGET_VARIABLES)
#
expand-target-variable=$($(2)) \
$(if $(filter $(2),$(SUBDIR_VARIABLES)),$($(SUBDIR_$(1))_$(2)),) \
$(if $(filter $(2),$(TARGET_VARIABLES)),$($(1)_$(2)),) \
$(if $(filter $(2),$(TARGET_VARIABLES)),$(_BS_INTERNAL_$(1)_$(2)),)
# get-target-variable
#
# Arguments: target name, variable name
#
# Returns the most specific defined value of the given variable for the named
# target. Unlike expand-target-variable which concatenates all defined versions,
# this returns only the most specific version.
_do_bs_first_defined = $(if $(1),$(1),$(if $(2),$(2),$(3)))
_bs_first_defined = $(call _do_bs_first_defined,$(strip $1),$(strip $(2)),$(strip $(3)))
get-target-variable = $(call _bs_first_defined, \
$(if $(filter $(2),$(TARGET_VARIABLES)),$($(1)_$(2)),), \
$(if $(filter $(2),$(SUBDIR_VARIABLES)),$($(SUBDIR_$(1))_$(2)),), \
$($(2)) \
)
# expand-subdir-variable, get-subdir-variable
#
# As above, but for subdirectory variables not target ones
expand-subdir-variable = $($(2)) $(if $(filter $(2),$(SUBDIR_VARIABLES)),$($(1)_$(2)))
get-subdir-variable = $(call _bs_first_defined,$(if $(filter $(2),$(SUBDIR_VARIABLES)),$($(1)_$(2))),$($(2)))
# recurse-dir
#
# Arguments: A directory name
#
# Returns a space-separated list of all files in the given directory and all subdirectories,
# excluding those that 'wildcard' doesn't match (i.e. dotfiles).
#
# This is quite simplistic, and not aware of symlinks. Be careful.
recurse-dir = $(foreach f, $(wildcard $(1)/*), \
$(if $(wildcard $(f)/.), \
$(call recurse-dir,$(f)), \
$(f)))
endif