+# Use variable indirection here so that we can have variable
+# names with fun chars in them like equal signs
+define check-tool-var
+ifeq ($(filter $(clean_targets) CLEAN_%,$(MAKECMDGOALS)),)
+_v = $(2)_$(3)
+ifndef $$(_v)
+$$(_v) := $$(call $(1),$(subst %, ,$(3)))
+export $$(_v)
+endif
+endif
+endef
+
+# Usage: check-gcc-var,<flag>
+# Check the C compiler to see if it supports <flag>.
+# Export the variable CFLAG_<flag> if it does.
+define check-gcc-var
+$(call check-tool-var,check_gcc,CFLAG,$(1))
+endef
+# Usage: check-as-var,<flag>
+# Check the assembler to see if it supports <flag>. Export the
+# variable ASFLAG_<flag> if it does (for invoking the assembler),
+# as well CFLAG_-Wa<flag> (for invoking the compiler driver).
+define check-as-var
+$(call check-tool-var,check_as,ASFLAG,$(1))
+_v = CFLAG_-Wa$(1)
+export $$(_v) = $$(if $$(ASFLAG_$(1)),-Wa$$(comma)$$(ASFLAG_$(1)))
+endef
+# Usage: check-ld-var,<flag>
+# Check the linker to see if it supports <flag>. Export the
+# variable LDFLAG_<flag> if it does (for invoking the linker),
+# as well CFLAG_-Wl<flag> (for invoking the compiler driver).
+define check-ld-var
+$(call check-tool-var,check_ld,LDFLAG,$(1))
+_v = CFLAG_-Wl$(1)
+export $$(_v) = $$(if $$(LDFLAG_$(1)),-Wl$$(comma)$$(LDFLAG_$(1)))
+endef
+# Usage: cache-output-var,<variable>,<shell command>
+# Execute <shell command> and cache the output in <variable>.
+define cache-output-var
+ifndef $(1)
+$(1) := $$(shell $(2))
+export $(1)
+endif
+endef
+
+