Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace spaces with new line in makefile

Does anyone know how to replace all spaces in a string in to a new line in a Makefile(GNU make)

like image 626
Suraj Chandran Avatar asked Jan 19 '11 12:01

Suraj Chandran


People also ask

How to add a new line in a Makefile?

So, you can format your makefiles for readability by adding newlines into the middle of a statement: you do this by escaping the internal newlines with a backslash ( \ ) character.


2 Answers

text := hello a b c

null :=
space := ${null} ${null}
${space} := ${space}# ${ } is a space. Neat huh?

define \n


endef

$(error [$(subst ${ },${\n},${text})])
like image 182
bobbogo Avatar answered Sep 21 '22 14:09

bobbogo


It's probably easier -- and cleaner -- to use GNU Make's shell function with sed to do the replacement, rather than trying to do it entirely within make.

STRING := foo bar baz
SPLIT  := $(shell echo "${STRING}" | sed -e 's/ /\n/g')

Or, slightly better, if your shell is bash instead of the default sh:

STRING := foo bar baz
SPLIT  := $(shell sed -e 's/ /\n/g' <<< ${STRING})
like image 41
Dan Moulding Avatar answered Sep 22 '22 14:09

Dan Moulding