How to generate goals in a Makefile, iterating over a list?

CODE:

LIST=0 1 2 3 4 5
PREFIX=rambo

# some looping logic to interate over LIST

EXPECTED RESULT:

rambo0:
    sh rambo_script0.sh

rambo1:
    sh rambo_script1.sh

Since my LIST has 6 elements, 6 goals need to be created. In the future, if I want to add more goals, I want to be able to simply modify my LIST and not touch any other part of the code.

How should loop logic be written?

+5
source share
2 answers

Use the text conversion features . With the help of patsubstyou can do pretty general conversions. To create file names addsuffix, addprefixboth are convenient.

For rules, use template rules .

The overall result might look something like this:

LIST = 0 1 3 4 5
targets = $(addprefix rambo, $(LIST))

all: $(targets)

$(targets): rambo%: rambo%.sh
    sh $<
+9
source

GNU make, :

LIST = 0 1 2 3 4 5
define make-rambo-target
  rambo$1:
         sh rambo_script$1.sh
  all:: rambo$1
endef

$(foreach element,$(LIST),$(eval $(call make-rambo-target,$(element))))
+12

All Articles