code_fodder
code_fodder

Reputation: 16371

makefile process variable with list of items where one item is a quoted string with spaces

Take the following makefile snippet:

VAR_LIST = "item1" "item2" "item 3 that has spaces" "item4"
ARGS = $(addprefix echo ,$(VAR_LIST))

What I am trying to achive is for ARGS to contain:

echo "item1" echo "item2" echo "item 3 that has spaces" echo "item4"

What I can't figure out how to resolve is that functions like addprefix act on spaces...

Upvotes: 0

Views: 288

Answers (2)

Vroomfondel
Vroomfondel

Reputation: 2898

You can do that with the help of gmtt entirely inside GNUmake. It is not as straightforward as in a full programming language, but at least it is portable and independent of external shell flavours and tools.

include gmtt/gmtt.mk

VAR_LIST = "item1" "item2" "item 3 that has spaces" "item4"

# make a prefix-list by splitting at ". This will yield superfluous space 
# characters between the items, but we can eliminate them later
prefix-list := $(call chop-str-spc,$(VAR_LIST),A $(-alnum-as-str))
$(info $(prefix-list))

# Now we select the data payload from the prefix-list. Spaces inside items 
# are still encoded as $(-spacereplace) characters, which is good as we have  
# a normal make list this way
string-list := $(call get-sufx-val,$(prefix-list),A,,100)
$(info $(string-list))

# Using get-sufx-val() is fine, but we can have it even simpler, by dropping
# the prefix, as we have only one in the list anyway:
string-list := $(call drop-prfx,$(prefix-list))

# Now step through the list with a normal for loop, converting $(-spacereplace)
# back to real spaces 
$(foreach item,$(string-list),$(if $(strip $(call spc-unmask,$(item))),\
   $(info [$(call spc-unmask,$(item))])))

Output:

$ make
 A¤item1 A¤§ A¤item2 A¤§ A¤item§3§that§has§spaces A¤§ A¤item4
item1 § item2 § item§3§that§has§spaces § item4
[item1]
[item2]
[item 3 that has spaces]
[item4]

Upvotes: 1

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136425

Not sure whether what you need is easily achievable with make because quoting strings have no effect on make functions that process words: " is a part of a word.

I would use a shell or python script for that.

Upvotes: 1

Related Questions