Question

Given a colon-delimited list of paths, getting a space-delimited list with GNU Make is straightforward:

CPATHS := /usr/bin/foo:/usr/bin/baz:/usr/bin/baz
SPATHS := $(subst :, ,$(CPATHS))

However, I couldn't find a nice way to go the opposite direction. The following hack does work (at least if sed is installed) but I'm pretty sure there will be a nicer way to solve this just using Make's internal functions.

SPATHS := /usr/bin/foo /usr/bin/baz /usr/bin/baz
CPATHS := $(shell echo $(SPATHS) > tmp; sed 's/ \+/:/g' tmp; rm tmp)
Was it helpful?

Solution

The only tricky part here is to define a literal space:

space := $(subst ,, )

SPATHS := /usr/bin/foo /usr/bin/baz /usr/bin/baz
CPATHS := $(subst $(space),:,$(SPATHS))

OTHER TIPS

The shortest way to get a literal space would be via $() $(). Thus:

$(subst $() $(),:,$(CPATHS))

Or, for brevity:

_=$() $()
$(subst $(_),:,$(CPATHS))

It is perhaps an interesting curiosity that the same trick works with cmake's macros, i.e. that ${} is a separator but introduces no whitespace by itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top