birgersp
birgersp

Reputation: 4926

How can I print a variable containing a list of filenames, with each filename on a separate line?

A variable in my makefile contains a long list of files, separated by space. I need to look through this list, but since the filenames are separated by space it's not very easy to do. So I'd like to print out the list, but where each filename separated by a new line.

Here's my attempt:

SOME_VARIABLE := I want this to be on separate lines
SOME_VARIABLE_SPLIT := $(subst ' ','\n',$(SOME_VARIABLE))
$(info $(SOME_VARIABLE_SPLIT))

Output:

I want this to be on separate lines

How do I print a variable containing space-separated filenames, with each filename on a separate line?

Upvotes: 0

Views: 327

Answers (1)

Renaud Pacalet
Renaud Pacalet

Reputation: 29260

Your problem comes from the way subst works: your quotes and backslashes are taken literally.

A solution consists in first defining two variables containing a space and a newline:

# $(NULL) is empty string
NULL  :=
# $(SPACE) is one space
SPACE := $(NULL) $(NULL)
# $(\n) is new line
define \n


endef

and then using them in your substitution:

SOME_VARIABLE       := I want this to be on separate lines
SOME_VARIABLE_SPLIT := $(subst $(SPACE),$(\n),$(SOME_VARIABLE))

$(info $(SOME_VARIABLE_SPLIT))

Notes:

  • yes, you need this empty NULL variable to define a variable which value is a space,
  • yes, you can name a make variable \n; maybe not the best thing to do for readability (readers could be puzzled) but funny enough to use it anyway,
  • and yes, you need two newlines in the define statement to get only one at the end.

Upvotes: 1

Related Questions