mox
mox

Reputation: 149

How can I set long options within a makefile

I am writing a makefile for distribution among students. To ease up their hacking experience, I would like make to warn about uninitialised variables.

I know there is the option --warn-undefined-variables to do just this, and of course, I can add an alias á la alias make="make --warn-undefined-variables" to my .bashrc. But I would like to set this option within the makefile so students will automatically profit from those warnings too, when they start to extend the makefile. The logical way to do so would be the MAKEFLAGS variable. However, while it works for short options, I cannot get it to work with --warn-undefined-variables as described in Can make warn me, when I use unset variables?

Makefile:

MAKEFLAGS=--warn-undefined-variables
$(info MAKEFLAGS: $(MAKEFLAGS))
$(info ${BAR})

Call:

$ make
MAKEFLAGS: --warn-undefined-variables
make: *** No targets.  Stop.

$  make --warn-undefined-variables
MAKEFLAGS: --warn-undefined-variables
Makefile:3: warning: undefined variable 'BAR'
make: *** No targets.  Stop.

When I change the MAKEFLAGS to -d the console is flooded with debug information, so I know MAKEFLAGS is set correctly. Any suggestions?

Upvotes: 1

Views: 199

Answers (1)

Louis
Louis

Reputation: 151441

I have GNU make 4.0 here and I cannot for the life of me get make to honor MAKEFLAGS= --warn-undefined-variables with a straightforward Makefile. However, if I make the Makefile invoke itself, then MAKEFLAGS= --warn-undefined-variables works in the child invocation!

MAKEFLAGS= --warn-undefined-variables
$(info MAKEFLAGS: $(MAKEFLAGS))
$(info $(BAR))

# This prevents a warning if we invoke make without a target...
MAKECMDGOALS?=

all:
ifndef RECURSED
    $(MAKE) RECURSED=1 $(MAKECMDGOALS)
else
    echo $(FOO)
endif

If I just run make, I get:

MAKEFLAGS: --warn-undefined-variables

make RECURSED=1 
make[1]: Entering directory '/tmp/t1'
MAKEFLAGS: --warn-undefined-variables
Makefile:3: warning: undefined variable 'BAR'

Makefile:12: warning: undefined variable 'FOO'
echo 

make[1]: Leaving directory '/tmp/t1'

Either I'm borking on something... or there's a bug in make. I'm inclined to think the latter.

Upvotes: 3

Related Questions