blob: 0687755faa55a2b9bb6ef660c22dc7af9e088d7f [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
Daniel Dunbar48464e02010-01-18 06:49:33 +000069# Function: CopyVariable src dst
70#
71# Copy the value of the variable 'src' to 'dst', taking care to not define 'dst'
72# if 'src' is undefined. The destination variable must be undefined.
73CopyVariable = \
74 $(call AssertValue,$(call IsUndefined,$(2)),destination is already defined)\
75 $(if $(call IsUndefined,$(1)),,\
76 $(call Set,$(2),$($(1))))
77
Daniel Dunbar6260e4a2010-01-18 06:48:48 +000078# Function: Assert value message
79#
80# Check that a value is true, or give an error including the given message
81Assert = $(if $(1),,\
82 $(error Assertion failed: $(2)))
83
84# Function: AssertEqual variable expected-value
85#
86# Check that the value of a variable is 'expected-value'.
87AssertEqual = \
88 $(if $(call streq,$($(1)),$(2)),,\
89 $(error Assertion failed: $(1): $(value $(1)) - $($(1)) != $(2)))
90
Daniel Dunbar1d16fde2010-10-14 21:23:37 +000091# Function: CheckCommandLineOverrides list
92#
93# Check that all command line variables are in the given list. This routine is
94# useful for validating that users aren't trying to override something which
95# will not work.
96CheckCommandLineOverrides = \
97 $(foreach arg,$(MAKEOVERRIDES),\
98 $(call Set,varname,$(firstword $(subst =, ,$(arg)))) \
99 $(if $(call contains,$(1),$(varname)),,\
100 $(error "Invalid command line override: $(1) $(varname) (not supported)")))
101
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000102###
103# Clean up make behavior
104
105# Cancel all suffix rules. We don't want no stinking suffix rules.
106.SUFFIXES:
107
108###
109# Debugging
110
Daniel Dunbar557a6ea2010-01-13 16:13:01 +0000111# General debugging rule, use 'make print-XXX' to print the definition, value
112# and origin of XXX.
113make-print-%:
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000114 $(error PRINT: $(value $*) = "$($*)" (from $(origin $*)))