Dieghito
Dieghito

Reputation: 675

Makefile: call multiline variable inside for loop

Here's my code:

la := a b c d
lb := 1 2 3 4

define test =
    First line $$vara
    Second line $$varb
endef
export test

@for vara in $(la); do \
    for varb in $(lb); do \
        echo $$vara $$varb; \
        echo "$$test"; \
    done \
done

Later this output will go in a file I'm creating using cat.

I'm expecting something like

a 1
First line a
Second line 1
a 2
First line a 
Second line 2
...

But instead I'm getting

a 1
$vara $varb
a 2 
$vara $varb

What am I doing wrong?

EDIT: SOLUTION

I used as a base @Beta's answer and changed it a bit to allow it to save the result in a file and send it over SSH to another terminal.

define test
[program:$$s-$$c]\nString 1\nString 2\n...\nThe End.
endef
export test

testmake:
    @for s in $(serv); do \
        for c in $(cust); do \
            ssh user@host "mkdir -p /var/log/$$s/$$c"; \
            echo "$(call test)" | ssh user@host "cat > /test.txt"; \
        done \
    done

Upvotes: 3

Views: 961

Answers (1)

Beta
Beta

Reputation: 99144

This worked for me in GNU Make 3.81 (but there are some fussy little differences between versions, so you may have to tweak it):

define test
  echo First line $$vara; \
  echo Second line $$varb
endef
export test

some_rule:
    @for vara in $(la); do \
      for varb in $(lb); do \
       echo $$vara $$varb; \
       $(call test); \
      done \
     done

Note that this has the loops in a recipe -- which is what I inferred you were doing. If you want the loops to run outside a rule, the solution will look quite different.

Upvotes: 3

Related Questions