Deduplicate multiple $(inherit-product) calls

The makefile product inheritance code was supposed
to deduplicate inheritance calls, but there was
a bug in the uniq-word function that caused it
to not work when duplicated words were adjacent.
$(subst |||x|||,||| |||,|||x|||x|||) produces
||| |||x||| instead of ||| ||| |||.

Rewrite the uniq-word function to fix the bug.

This issue was causing a discrepency between
the makefile and starlark based product configurations,
as the starlark implementation didn't have this bug.

Bug: 237019892
Test: ./build/bazel/ci/rbc_dashboard.py --quick on an internal-only product
Change-Id: I543a80746412ffcb9743203399413a0e707111e6
diff --git a/core/node_fns.mk b/core/node_fns.mk
index 2243cd7..144eb8b 100644
--- a/core/node_fns.mk
+++ b/core/node_fns.mk
@@ -83,27 +83,17 @@
 # If needle appears multiple times, only the first occurrance
 # will survive.
 #
-# How it works:
-#
-# - Stick everything in haystack into a single word,
-#   with "|||" separating the words.
-# - Replace occurrances of "|||$(needle)|||" with "||| |||",
-#   breaking haystack back into multiple words, with spaces
-#   where needle appeared.
-# - Add needle between the first and second words of haystack.
-# - Replace "|||" with spaces, breaking haystack back into
-#   individual words.
-#
 define uniq-word
 $(strip \
   $(if $(filter-out 0 1,$(words $(filter $(2),$(1)))), \
-    $(eval h := |||$(subst $(space),|||,$(strip $(1)))|||) \
-    $(eval h := $(subst |||$(strip $(2))|||,|||$(space)|||,$(h))) \
-    $(eval h := $(word 1,$(h)) $(2) $(wordlist 2,9999,$(h))) \
-    $(subst |||,$(space),$(h)) \
-   , \
-    $(1) \
- ))
+    $(eval _uniq_word_seen :=) \
+    $(foreach w,$(1), \
+      $(if $(filter $(2),$(w)), \
+        $(if $(_uniq_word_seen),, \
+          $(w) \
+          $(eval _uniq_word_seen := true)), \
+        $(w))), \
+  $(1)))
 endef
 
 INHERIT_TAG := @inherit: