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

5
  • What is the bigger problem you are trying to solve with this? Commented Jan 15, 2019 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: stackoverflow.com/questions/11032280/…. Does that make sense? Commented Jan 15, 2019 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). Commented Jan 15, 2019 at 11:02
  • Not sure whether what you need is easily achievable with make. I would use a shell or python script for that. Commented Jan 16, 2019 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 :) Commented Jan 16, 2019 at 12:22

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It is doable (see my answer) - one can argue over the complexity but IMHO the batteries-included-property outweighs lacking elegance.
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]

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.