summaryrefslogtreecommitdiff
path: root/make/util.mk
blob: 0687755faa55a2b9bb6ef660c22dc7af9e088d7f (plain)
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
# Generic Makefile Utilities

###
# Utility functions

# Function: streq LHS RHS
#
# Return "true" if LHS == RHS, otherwise "".
#
# LHS == RHS <=> (LHS subst RHS is empty) and (RHS subst LHS is empty)
streq = $(if $(1),$(if $(subst $(1),,$(2))$(subst $(2),,$(1)),,true),$(if $(2),,true))

# Function: strneq LHS RHS
#
# Return "true" if LHS != RHS, otherwise "".
strneq = $(if $(call streq,$(1),$(2)),,true)

# Function: contains list item
#
# Return "true" if 'list' contains the value 'item'.
contains = $(if $(strip $(foreach i,$(1),$(if $(call streq,$(2),$(i)),T,))),true,)

# Function: is_subset a b
# Return "true" if 'a' is a subset of 'b'.
is_subset = $(if $(strip $(set_difference $(1),$(2))),,true)

# Function: set_difference a b
# Return a - b.
set_difference = $(foreach i,$(1),$(if $(call contains,$(2),$(i)),,$(i)))

# Function: Set variable value
#
# Set the given make variable to the given value.
Set = $(eval $(1) := $(2))

# Function: Append variable value
#
# Append the given value to the given make variable.
Append = $(eval $(1) += $(2))

# Function: IsDefined variable
#
# Check whether the given variable is defined.
IsDefined = $(call strneq,undefined,$(flavor $(1)))

# Function: IsUndefined variable
#
# Check whether the given variable is undefined.
IsUndefined = $(call streq,undefined,$(flavor $(1)))

# Function: VarOrDefault variable default-value
#
# Get the value of the given make variable, or the default-value if the variable
# is undefined.
VarOrDefault = $(if $(call IsDefined,$(1)),$($(1)),$(2))

# Function: CheckValue variable
#
# Print the name, definition, and value of a variable, for testing make
# utilities.
#
# Example:
#   foo = $(call streq,a,a)
#   $(call CheckValue,foo)
# Example Output:
#   CHECKVALUE: foo: $(call streq,,) - true
CheckValue = $(info CHECKVALUE: $(1): $(value $(1)) - $($(1)))

# Function: CopyVariable src dst
#
# Copy the value of the variable 'src' to 'dst', taking care to not define 'dst'
# if 'src' is undefined. The destination variable must be undefined.
CopyVariable = \
  $(call AssertValue,$(call IsUndefined,$(2)),destination is already defined)\
  $(if $(call IsUndefined,$(1)),,\
       $(call Set,$(2),$($(1))))

# Function: Assert value message
#
# Check that a value is true, or give an error including the given message
Assert = $(if $(1),,\
           $(error Assertion failed: $(2)))

# Function: AssertEqual variable expected-value
#
# Check that the value of a variable is 'expected-value'.
AssertEqual = \
  $(if $(call streq,$($(1)),$(2)),,\
       $(error Assertion failed: $(1): $(value $(1)) - $($(1)) != $(2)))

# Function: CheckCommandLineOverrides list
#
# Check that all command line variables are in the given list. This routine is
# useful for validating that users aren't trying to override something which
# will not work.
CheckCommandLineOverrides = \
  $(foreach arg,$(MAKEOVERRIDES),\
    $(call Set,varname,$(firstword $(subst =, ,$(arg)))) \
    $(if $(call contains,$(1),$(varname)),,\
      $(error "Invalid command line override: $(1) $(varname) (not supported)")))

###
# Clean up make behavior

# Cancel all suffix rules. We don't want no stinking suffix rules.
.SUFFIXES:

###
# Debugging

# General debugging rule, use 'make print-XXX' to print the definition, value
# and origin of XXX.
make-print-%:
	$(error PRINT: $(value $*) = "$($*)" (from $(origin $*)))