23

I have this:

FOO = foo1 foo2 ... fooN

and want to get join all these string and separate it with, for instance, colong:

foo1:foo2:foo3:...:fooN

How to do this in GNU Make, without using external UNIX tools?

eold
  • 5,972
  • 11
  • 56
  • 75

2 Answers2

28

See the code below.

# A literal space.
space :=
space +=

# Joins elements of the list in arg 2 with the given separator.
#   1. Element separator.
#   2. The list.
join-with = $(subst $(space),$1,$(strip $2))

Usage:

FOO = foo1 foo2 ... fooN

COLON_SEPARATED_FOO := $(call join-with,:,$(FOO))
Eldar Abusalimov
  • 24,387
  • 4
  • 67
  • 71
21

You can simply replace spaces with colon:

EMPTY :=
SPACE := $(EMPTY) $(EMPTY)
FOO = foo1 foo2 ... fooN
FOO_LIST = $(subst $(SPACE),:,$(FOO))

FOO_LIST is foo1:foo2:...:fooN.

Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92