makefile: remove duplicate words without sorting
makefile
Solution
Boring `$eval` based method:
define uniq =
$(eval seen :=)
$(foreach _,$1,$(if $(filter $_,${seen}),,$(eval seen += $_)))
${seen}
endef
w := z z x x y c x
$(info $(sort $w))
$(info $(call uniq,$w))
Extremely fiendish make standard library recursive call (recursive make considered extremely fiendish?):
uniq = $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1)))
It's worth noting that no variables are damaged in this second formulation (see `seen` in the first). It is preferable just for that (given the lack of locals in make)!
EDIT
My obscure comment about recursive make above seems to have muddied the waters somewhat. "Recursive" in the context of this post means recursive function. It really has nothing to do with the execrable recursive make.
The latter (recursive) definition of uniq is extremely nice, performant, small, and is definitely the one to use.
Problem
Is there a possibility to remove duplicates in a list of words without sorting in a makefile? ``` $(sort foo bar lose) ``` does remove duplicates (which is for me the main functionality in this case), but also sorts (for me an unfortunate side effect in this case). I want to avoid that. [update] bobbogo's answer works very nicely. Just remember to use `define uniq` for v3.81 and (did not check this) `define uniq =` for later versions. larsmans' answer works very nicely too if your record separator is not a space, e.g. if you want to remove duplicates from `_foo_bar_lose_lose_bar_baz_` or the like. Just remember to use the `RS` and `ORS` awk options instead of `tr`, and wrap it all with `$(firstword $(shell ... ))`