Brain-free
Brain-free

Reputation: 13

Shell command as a variable in Makefile

I have the following (very simplified) Makefile:

# Makefile

MAKESH=shell
USER=$($MAKESH logname)

which aims to store my login name in the USER variable as if the $(shell logname) command was invoked. However this doesn't happen. I suspect that it's due to the way make evaluates variables.

Does anyone have some more insight to this or a possible workaround i.e. explicitly tell make that $MAKESH is actually the shell command?

Thanks.

Edit

Consider the following example:

#parent.mk

...
include child.mk
...

-------------------
#child.mk

export $(shell logname)/path1
export $(shell logname)/path2

-------------------
#bash_script.sh

...
source child.mk
...

(in this example, sourcing child.mk fails)

Essentially, what I wanted to do is to create a file that exports pathnames depending on the current user. The syntax should be valid both for makefiles and bash scripts, since it can be included/sourced from both places.

I also wanted to do subtle changes to these files, since they are part of a larger project and the interconnections are a bit confusing.

I have finally decided to go with this:

#parent.mk

...
export logname=$(shell logname)
include child.mk
...

-------------------
#child.mk

export $(logname)/path1
export $(logname)/path2

-------------------
#bash_script.sh

...
source child.mk
...

which is quite hacky, a tiny bit dangerous, but works. If you know a more elegant way/aprroach, let me know.

Finally, thank you for your solutions (and your warnings), which are valid for the pre-editted question.

Upvotes: 1

Views: 997

Answers (2)

reinierpost
reinierpost

Reputation: 8611

MAKESH=shell
$(eval USER=$$($(MAKESH) logname))
$(info USER is $(USER))

Avoid if possible.

Upvotes: 2

William
William

Reputation: 4935

The syntax should be

USER = $(shell logname)

I don't know why you want to put "shell" in a variable and screw up the function syntax, but if you really insist, try this:

$(eval USER=$$($MAKESH logname))

The $(eval ...) function will just turn it into the first form above, though, so it's wasted effort. (Unless your really trying to do something trickier than your example suggests.)

Upvotes: 5

Related Questions