[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: conditional macro definitions
From: |
Eric West |
Subject: |
Re: conditional macro definitions |
Date: |
Sun, 20 Nov 2005 15:10:09 -0500 |
User-agent: |
KMail/1.8.2 |
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