blob: 6764a91cbd2491a2fcd98e720273b9319ac5f2aa [file] [log] [blame]
Daniel Dunbar6260e4a2010-01-18 06:48:48 +00001# Generic Makefile Utilities
Daniel Dunbarb3a69012009-06-26 16:47:03 +00002
3###
4# Utility functions
5
Daniel Dunbarfaf01502010-01-18 06:48:33 +00006# Function: streq LHS RHS
7#
8# Return "true" if LHS == RHS, otherwise "".
9#
10# LHS == RHS <=> (LHS subst RHS is empty) and (RHS subst LHS is empty)
11streq = $(if $(1),$(if $(subst $(1),,$(2))$(subst $(2),,$(1)),,true),$(if $(2),,true))
12
13# Function: strneq LHS RHS
14#
15# Return "true" if LHS != RHS, otherwise "".
16strneq = $(if $(call streq,$(1),$(2)),,true)
17
Daniel Dunbar6260e4a2010-01-18 06:48:48 +000018# Function: contains list item
19#
20# Return "true" if 'list' contains the value 'item'.
21contains = $(if $(strip $(foreach i,$(1),$(if $(call streq,$(2),$(i)),T,))),true,)
22
23# Function: is_subset a b
24# Return "true" if 'a' is a subset of 'b'.
25is_subset = $(if $(strip $(set_difference $(1),$(2))),,true)
26
27# Function: set_difference a b
28# Return a - b.
29set_difference = $(foreach i,$(1),$(if $(call contains,$(2),$(i)),,$(i)))
30
Daniel Dunbarb3a69012009-06-26 16:47:03 +000031# Function: Set variable value
32#
33# Set the given make variable to the given value.
34Set = $(eval $(1) := $(2))
35
36# Function: Append variable value
37#
38# Append the given value to the given make variable.
39Append = $(eval $(1) += $(2))
40
Daniel Dunbar6260e4a2010-01-18 06:48:48 +000041# Function: IsDefined variable
42#
43# Check whether the given variable is defined.
44IsDefined = $(call strneq,undefined,$(flavor $(1)))
45
46# Function: IsUndefined variable
47#
48# Check whether the given variable is undefined.
49IsUndefined = $(call streq,undefined,$(flavor $(1)))
50
51# Function: VarOrDefault variable default-value
52#
53# Get the value of the given make variable, or the default-value if the variable
54# is undefined.
55VarOrDefault = $(if $(call IsDefined,$(1)),$($(1)),$(2))
56
57# Function: CheckValue variable
58#
59# Print the name, definition, and value of a variable, for testing make
60# utilities.
61#
62# Example:
63# foo = $(call streq,a,a)
64# $(call CheckValue,foo)
65# Example Output:
66# CHECKVALUE: foo: $(call streq,,) - true
67CheckValue = $(info CHECKVALUE: $(1): $(value $(1)) - $($(1)))
68
69# Function: Assert value message
70#
71# Check that a value is true, or give an error including the given message
72Assert = $(if $(1),,\
73 $(error Assertion failed: $(2)))
74
75# Function: AssertEqual variable expected-value
76#
77# Check that the value of a variable is 'expected-value'.
78AssertEqual = \
79 $(if $(call streq,$($(1)),$(2)),,\
80 $(error Assertion failed: $(1): $(value $(1)) - $($(1)) != $(2)))
81
Daniel Dunbarb3a69012009-06-26 16:47:03 +000082###
83# Clean up make behavior
84
85# Cancel all suffix rules. We don't want no stinking suffix rules.
86.SUFFIXES:
87
88###
89# Debugging
90
Daniel Dunbar557a6ea2010-01-13 16:13:01 +000091# General debugging rule, use 'make print-XXX' to print the definition, value
92# and origin of XXX.
93make-print-%:
Daniel Dunbarb3a69012009-06-26 16:47:03 +000094 $(error PRINT: $(value $*) = "$($*)" (from $(origin $*)))