Guy Kedem
Guy Kedem

Reputation: 15

How to add both prefix and suffix to a list in makefile?

In my makefile I'm trying to add to each item in the list both prefix and suffix.

For example - assuming the list contains:

ITEMS =item1 item2 item3

I would like to get this string:

ITEMS_PADDED =--before item1 --after1 --after2 --before item2 --after1 --after2 --before item3 --after1 --after2

I tried with addprefix and addsuffix commands but it treats the added prefix as items so it add a suffix to the prefix as it was an item:

With the following code

ITEMS                   =item1 item2 item3
ITEMS_PREFIX            =$(addprefix ' --before ', $(ITEMS))
ITEMS_SUFFIX            =$(addsuffix ' --after ', $(ITEMS_PREFIX))

list_items:
    @echo $(ITEMS)
    @echo $(ITEMS_PREFIX)
    @echo $(ITEMS_SUFFIX)

I'm getting this results:

item1 item2 item3
--before item1  --before item2  --before item3
--after  --before --after  item1 --after   --after  --before --after  item2 --after   --after  --before --after  item3 --after

which is obviously not correct.

Upvotes: 1

Views: 2527

Answers (1)

Andreas
Andreas

Reputation: 5301

This is a job for foreach:

ITEMS         := item1 item2 item3
ITEMS_FOREACH := $(foreach item,$(ITEMS),--before $(item) --after1 --after2)

list_items:
    @echo $(ITEMS)
    @echo $(ITEMS_FOREACH)

From the manual: https://www.gnu.org/software/make/manual/html_node/Foreach-Function.html

Upvotes: 1

Related Questions