help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: conditional macro definitions


From: bill
Subject: Re: conditional macro definitions
Date: Mon, 21 Nov 2005 20:23:47 +0000
User-agent: Mozilla Thunderbird 1.0.7-1.1.fc3 (X11/20050929)

Eric West wrote:

On Sunday 20 November 2005 02:50 am, bill wrote:
The syntax
foo:=CFLAGS=-DFOO
for redefining CFLAGS for the target foo seems like a great thing,
but it's not valid in gnu-make.  I'm currently setting up my make
files so that I type "make DEBUG=1" and then defining CFLAGS
conditionally based on the definition of DEBUG.  I'd much rather type
"make debug" and use the syntax given above.  2 questions:

1)  Is it possible to do something like:
if target==debug;    CFLAGS = -DDEBUG; end if
rather than:
ifdef DEBUG; CFLAGS = -DDEBUG; end if

2) Why is the conditional macro definition syntax not incorporated
into gnu-make?
It seems like a good idea.



You can tailor a make file to do (1). One approach is to test the target(s) passed on the command line using the MAKECMDGOALS variable. This variable is initialized by make when it is invoked.
Example:

$ cat make.tst
CFLAGS := -DXXXX

ifneq (,$(findstring debug,${MAKECMDGOALS}))
   CFLAGS += -DDEBUG
   ifeq "$(words ${MAKECMDGOALS})" "1"
       tgt := all
   endif
endif

all: FRC
       # all: CFLAGS = ${CFLAGS}

foo: FRC
       # foo: CFLAGS = ${CFLAGS}

.PHONY : debug
debug : ${tgt}
        @echo > /dev/null

FRC:

$ make -f make.tst debug
# all: CFLAGS = -DXXXX -DDEBUG
$ make -f make.tst debug foo
# foo: CFLAGS = -DXXXX -DDEBUG
$ make -f make.tst foo
# foo: CFLAGS = -DXXXX
$

Variations on this theme are left to your imagination.

 --Eric West

Thanks to everyone who responded to that question. I'm a little confused about how to deal with the issue that the conditional definition seems to occur too late. In the following example, I modify the value of $(DIR) when the target is debug. But the target $(DIR) maintains the old value. So when I "make debug", there is no
rule to build /tmp/debug.  How do I make the conditional values be targets?

% rmdir /tmp/foo
% cat makefile

DIR = /tmp/foo

debug : DIR = /tmp/debug

all: $(DIR)

debug: all

$(DIR):
       mkdir -p $@
       @echo target:$@
       @echo DIR:$(DIR)
% make debug
mkdir -p /tmp/foo
target:/tmp/foo
DIR:/tmp/debug









reply via email to

[Prev in Thread] Current Thread [Next in Thread]