devgp
devgp

Reputation: 1321

Makefile Substituting for loop variables to functions

I'm not sure what the best way to do this, and some pointers in this regard would be helpful

Code:

#Else where in different file and included in this makefile i have
LIBRARY_LIST    := CONFIG_MTS_PTHREADS
CONFIG_MTS_PTHREADS :=  y

    collect-compilation:
        if  [   $(strip $(CONFIG_MTS_PTHREADS)) ==  y   ]; then \
            echo "ok";  \
        fi;
        for compile in $(LIBRARY_LIST)  ;   do  \
            if  [   $(strip $$compile)  ==  y   ]; then \
                echo "ok";  \
            fi; \
        done

So from the above code snippet., the top part the 'IF' loop works fine as expected and i see 'OK'. displayed.

Now for the second for-loop, i have some problems substituting the $$compile to the 'IF'. Eventually i expect $$compile gets replaced by CONFIG_MTS_PTHREADS and the expression should evaluate to y == y and display 'OK' but for me.,

Output:

make -C ./Dev2.0.1/OSX
if  [   y   ==  y   ]; then \
        echo "ok";  \
    fi;
ok    <----- fine and expected
for compile in CONFIG_MTS_PTHREADS  ;   do  \
        if  [   $compile    ==  y   ]; then \
            echo "ok";  \
        fi; \
    done      <------ Here it skips the then part and proceeds further, i expect 'OK' after this though.

Upvotes: 0

Views: 4019

Answers (1)

Beta
Beta

Reputation: 99094

The trouble is that you're mixing Make-expansion and shell-expansion.

We start with this command:

    for compile in $(LIBRARY_LIST)  ;   do  \
        if  [   $(strip $$compile)  ==  y   ]; then \
            echo "ok";  \
        fi; \
    done

Make expands the variables:

    for compile in CONFIG_MTS_PTHREADS  ;   do  \
        if  [   $compile  ==  y   ]; then \
            echo "ok";  \
        fi; \
    done

(note that compile has no value yet) and passes the command to the shell, which expands the variables, runs the for loop and winds up testing:

        if  [   CONFIG_MTS_PTHREADS  ==  y   ]; then 

which clearly must fail. It is too late to expand CONFIG_MTS_PTHREADS; the shell doesn't know that it's a variable with a value. Make knew, but Make has already handed to command off to the shell.

One solution is to use the value function, so that Make will expand the variable before passing it to the shell:

    for compile in $(value $(LIBRARY_LIST))  ;   do  \
        if  [   $(strip $$compile)  ==  y   ]; then \
            echo "ok";  \
        fi; \
    done

Upvotes: 1

Related Questions