Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does $@ work in a make conditional?

I'm using GNU Make 3.80. In my Makefile, I use automatic variable $@ to refer to the current target.

    @echo current target is ... [$@]
ifeq ($@,sms)
    @echo yep, they are equal
else
    @echo no, they are not equal
endif

It seems that $@ expands to sms, as shown in the output below.

Output is:

current target is ... [sms]
no, they are not equal

My question: since $@ (apparently) expands to sms, shouldn't the "true" branch of the ifeq conditional be executed (with the consequence that the output should read yep, they are equal)? [I am at a loss as to why the output is no, they are not equal.]

like image 642
JaysonFix Avatar asked Oct 25 '25 01:10

JaysonFix


1 Answers

From the GNU Make manual:

10.5.3 Automatic Variables ... It's very important that you recognize the limited scope in which automatic variable values are available: they only have values within the recipe. ... there is a special feature of GNU make, secondary expansion (see Secondary Expansion), which will allow automatic variable values to be used in prerequisite lists.

That is, $@ can only be used inside the section containing commands to build the target and, with some restrictions, inside the prerequisites list.

However you can use shell commands to implement conditions inside the list of commands used to build the target:

    @echo current target is ... [$@]
    if [[ "$@" == "sms" ]]; then \
         echo yep, they are equal; \
    else \
         echo no, they are not equal; \
    fi

Also if you want to check what targets were specified on the command line use MAKECMDGOALS variable.

like image 109
ssmir Avatar answered Oct 28 '25 03:10

ssmir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!