Joining elements of a list in GNU Make

You can use the $(subst) command, combined with a little trick to get a variable that has a value of a single space:

p = /usr /usr/share /lib
noop=
space = $(noop) $(noop)

all:
        @echo $(subst $(space),:,$(p))

Cleanest Form (that I can find):

classpathify = $(subst $(eval) ,:,$(wildcard $1))
cp = a b c d/*.jar

target:
    echo $(call classpathify,$(cp))
# prints a:b:c:d/1.jar:d/2.jar

Notes:

  • Turning it into a pseudo-function makes the intention clearer than doing a bunch of arcane string manipulation inline.
  • I included the $(wildcard) function because you almost always use these two together when specifying a classpath
  • Make sure not to put any extra spaces in after the commas or you will get something like "::a:b:c:d:e".

You use ':' as separator, so you are on Linux. Consider using bash tool chain to replace continuous spaces by single colon

PATH := $(shell echo $(DIRS) | sed "s/ \+/:/g")

Tags:

Makefile