0

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

code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • What is the bigger problem you are trying to solve with this? – Maxim Egorushkin Jan 15 '19 at 10:55
  • @MaximEgorushkin I want a doxygen config variable that makefile users can fill in. The items look like `"VAR=Some Value"`. Then I need to convert each of those into something that looks like: `; echo "VAR=Some Value"`, where semicolon is the list seperator. Then I pipe that into the doxygen command. See first answer here: https://stackoverflow.com/questions/11032280/specify-doxygen-parameters-through-command-line. Does that make sense? – code_fodder Jan 15 '19 at 10:59
  • @MaximEgorushkin I could make the user add items like this: `; echo "VAR=some value"` ... but that looks like a horrible syntax to impose on the user... At the moment I am saying they can use only values without spaces (which is mostly ok). – code_fodder Jan 15 '19 at 11:02
  • Not sure whether what you need is easily achievable with make. I would use a shell or python script for that. – Maxim Egorushkin Jan 16 '19 at 10:33
  • @MaximEgorushkin yeah, I think I am with you on this one. There probably is some horrendus set of make functions that can be applied, but it would be really convoluted (if at all possible). I have seen some really clever/horrible code out there!... But as you say, use a script! - I just did not think of that, great idea - feel free to add as an answer :) – code_fodder Jan 16 '19 at 12:22

2 Answers2

1

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.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • 1
    It is doable (see my answer) - one can argue over the complexity but IMHO the batteries-included-property outweighs lacking elegance. – Vroomfondel Jan 17 '19 at 13:48
1

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]
Vroomfondel
  • 2,704
  • 1
  • 15
  • 29
  • wow.... that is pretty amazing that you can do that.... but the code is really not nice - i can barely understand it so for future maintenence its not my first option. but +1 for the code! – code_fodder Jan 17 '19 at 21:20
  • You mean this code or the code thats behind it in the lib? All make programming is bascially a very simple functional paradigma with one data type, string. Prefix lists are one way to handle collections. – Vroomfondel Jan 18 '19 at 08:07