1 # Generic Makefile Utilities 2 3 ### 4 # Utility functions 5 6 # 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) 11 streq = $(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 "". 16 strneq = $(if $(call streq,$(1),$(2)),,true) 17 18 # Function: contains list item 19 # 20 # Return "true" if 'list' contains the value 'item'. 21 contains = $(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'. 25 is_subset = $(if $(strip $(set_difference $(1),$(2))),,true) 26 27 # Function: set_difference a b 28 # Return a - b. 29 set_difference = $(foreach i,$(1),$(if $(call contains,$(2),$(i)),,$(i))) 30 31 # Function: Set variable value 32 # 33 # Set the given make variable to the given value. 34 Set = $(eval $(1) := $(2)) 35 36 # Function: Append variable value 37 # 38 # Append the given value to the given make variable. 39 Append = $(eval $(1) += $(2)) 40 41 # Function: IsDefined variable 42 # 43 # Check whether the given variable is defined. 44 IsDefined = $(call strneq,undefined,$(flavor $(1))) 45 46 # Function: IsUndefined variable 47 # 48 # Check whether the given variable is undefined. 49 IsUndefined = $(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. 55 VarOrDefault = $(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 67 CheckValue = $(info CHECKVALUE: $(1): $(value $(1)) - $($(1))) 68 69 # 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. 73 CopyVariable = \ 74 $(call AssertValue,$(call IsUndefined,$(2)),destination is already defined)\ 75 $(if $(call IsUndefined,$(1)),,\ 76 $(call Set,$(2),$($(1)))) 77 78 # Function: Assert value message 79 # 80 # Check that a value is true, or give an error including the given message 81 Assert = $(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'. 87 AssertEqual = \ 88 $(if $(call streq,$($(1)),$(2)),,\ 89 $(error Assertion failed: $(1): $(value $(1)) - $($(1)) != $(2))) 90 91 # 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. 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)"))) 101 102 ### 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 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 $*))) 115