diff --git a/prelude.mk b/prelude.mk index 8ce60a6536..acb2f6da59 100644 --- a/prelude.mk +++ b/prelude.mk @@ -16,6 +16,7 @@ # String support: # - Variable: export NL # - Variable: SPACE +# - Function: str.eq # # Path support: # - Function: path.trimprefix diff --git a/prelude_str.mk b/prelude_str.mk index 9ac84b0e86..966fcd3e76 100644 --- a/prelude_str.mk +++ b/prelude_str.mk @@ -15,3 +15,7 @@ export NL define SPACE endef + +# Usage: $(call str.eq,STR1,STR2) +# Evaluates to either $(TRUE) or $(FALSE) +str.eq = $(if $(subst x$1,,x$2)$(subst x$2,,x$1),$(FALSE),$(TRUE)) diff --git a/tests/prelude_str.bats b/tests/prelude_str.bats index 62be2f726c..59850e2ecd 100644 --- a/tests/prelude_str.bats +++ b/tests/prelude_str.bats @@ -11,3 +11,29 @@ load common # Honestly, this checks `quote.shell` more than it does SPACE. check_expr_eq strict '$(SPACE)' ' ' } + +@test "prelude_str.mk: str.eq" { + cat >>Makefile <<-'__EOT__' + include build-aux/prelude.mk + test = $(info ("$1" == "$2") => $(if $(call str.eq,$1,$2),$$(TRUE),$$(FALSE))) + $(call test,foo,foo) + $(call test,foo,bar) + $(call test,bar,bar) + $(call test,f%,foo) + $(call test,foo,f%) + $(call test,f%,f%) + all: noop + noop: ; @true + .PHONY: noop + __EOT__ + + make >actual + printf '("%s" == "%s") => $(%s)\n' >expected \ + foo foo TRUE \ + foo bar FALSE \ + bar bar TRUE \ + f% foo FALSE \ + foo f% FALSE \ + f% f% TRUE + diff -u expected actual +}