Mazzy
Mazzy

Reputation: 14179

Set variable in a rule and have it available in another rule

I've the following makefile:

.ONESHELL:

SHELL := /bin/bash

build-%:
        @[ $(findstring -, $*) ] && DIR_ENV=$(subst -,/,$*) || DIR_ENV=$*
        @echo ${DIR_ENV}

I'm trying to have available the DIR_ENV but without no luck. I know that every command executed is executed in its own shell so no sharing of variavbles. However I've added ONESHELL directive. But it still doesn't work. What am I'm missing?

Upvotes: 0

Views: 33

Answers (1)

RTLinuxSW
RTLinuxSW

Reputation: 842

You are missing the fact the string ${DIR_ENV} is evaluated by make first and the resulting value is a null string. Use this

build-%:
        @[ $(findstring -, $*) ] && DIR_ENV=$(subst -,/,$*) || DIR_ENV=$*
        @echo $${DIR_ENV}

Also the Make syntax

$(if $(findstring -,$*),$(subst -,/,$*),$*)

Does the if/then/else logic in Make, not the shell

Upvotes: 2

Related Questions