5gon12eder
5gon12eder

Reputation: 25439

GNU Make Convert Spaces to Colons

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)

Upvotes: 30

Views: 10970

Answers (2)

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.

Upvotes: 25

Eldar Abusalimov
Eldar Abusalimov

Reputation: 25523

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))

Upvotes: 36

Related Questions