[tsan] Fix more warnings in TSan tests.
[blocksruntime.git] / make / util.mk
blob0687755faa55a2b9bb6ef660c22dc7af9e088d7f
1 # Generic Makefile Utilities
3 ###
4 # Utility functions
6 # Function: streq LHS RHS
8 # Return "true" if LHS == RHS, otherwise "".
10 # LHS == RHS <=> (LHS subst RHS is empty) and (RHS subst LHS is empty)
11 streq = $(if $(1),$(if $(subst $(1),,$(2))$(subst $(2),,$(1)),,true),$(if $(2),,true))
13 # Function: strneq LHS RHS
15 # Return "true" if LHS != RHS, otherwise "".
16 strneq = $(if $(call streq,$(1),$(2)),,true)
18 # Function: contains list item
20 # Return "true" if 'list' contains the value 'item'.
21 contains = $(if $(strip $(foreach i,$(1),$(if $(call streq,$(2),$(i)),T,))),true,)
23 # Function: is_subset a b
24 # Return "true" if 'a' is a subset of 'b'.
25 is_subset = $(if $(strip $(set_difference $(1),$(2))),,true)
27 # Function: set_difference a b
28 # Return a - b.
29 set_difference = $(foreach i,$(1),$(if $(call contains,$(2),$(i)),,$(i)))
31 # Function: Set variable value
33 # Set the given make variable to the given value.
34 Set = $(eval $(1) := $(2))
36 # Function: Append variable value
38 # Append the given value to the given make variable.
39 Append = $(eval $(1) += $(2))
41 # Function: IsDefined variable
43 # Check whether the given variable is defined.
44 IsDefined = $(call strneq,undefined,$(flavor $(1)))
46 # Function: IsUndefined variable
48 # Check whether the given variable is undefined.
49 IsUndefined = $(call streq,undefined,$(flavor $(1)))
51 # Function: VarOrDefault variable default-value
53 # Get the value of the given make variable, or the default-value if the variable
54 # is undefined.
55 VarOrDefault = $(if $(call IsDefined,$(1)),$($(1)),$(2))
57 # Function: CheckValue variable
59 # Print the name, definition, and value of a variable, for testing make
60 # utilities.
62 # Example:
63 # foo = $(call streq,a,a)
64 # $(call CheckValue,foo)
65 # Example Output:
66 # CHECKVALUE: foo: $(call streq,,) - true
67 CheckValue = $(info CHECKVALUE: $(1): $(value $(1)) - $($(1)))
69 # Function: CopyVariable src dst
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.
73 CopyVariable = \
74 $(call AssertValue,$(call IsUndefined,$(2)),destination is already defined)\
75 $(if $(call IsUndefined,$(1)),,\
76 $(call Set,$(2),$($(1))))
78 # Function: Assert value message
80 # Check that a value is true, or give an error including the given message
81 Assert = $(if $(1),,\
82 $(error Assertion failed: $(2)))
84 # Function: AssertEqual variable expected-value
86 # Check that the value of a variable is 'expected-value'.
87 AssertEqual = \
88 $(if $(call streq,$($(1)),$(2)),,\
89 $(error Assertion failed: $(1): $(value $(1)) - $($(1)) != $(2)))
91 # Function: CheckCommandLineOverrides list
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.
96 CheckCommandLineOverrides = \
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)")))
103 # Clean up make behavior
105 # Cancel all suffix rules. We don't want no stinking suffix rules.
106 .SUFFIXES:
109 # Debugging
111 # General debugging rule, use 'make print-XXX' to print the definition, value
112 # and origin of XXX.
113 make-print-%:
114 $(error PRINT: $(value $*) = "$($*)" (from $(origin $*)))