[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: conditional macro definitions
From: |
John Graham-Cumming |
Subject: |
Re: conditional macro definitions |
Date: |
Sun, 20 Nov 2005 20:24:45 +0100 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040208 Thunderbird/0.5 Mnenhy/0.6.0.104 |
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.
Correct. That syntax is common in the version of Make used on Sun
systems. What you are looking for are "target-specific variables" in
GNU Make parlance.
The equivalent syntax in GNU Make is:
foo: CFLAGS=-DFOO
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
There are a couple of things that you could do. The first is just have
a target called 'debug' that sets CFLAGS:
debug: CFLAGS=-DFOO
and then make everything else depend on debug. So imagine that you have
an 'all' target then you'd add:
debug: all
And when you do 'make debug' it'll build 'all' (and whatever else it
will need), but CFLAGS will have the special debug value for every
target 'behind' 'all'. That's because in GNU Make when you set a
target-specific variable you are setting it for the target and all its
prerequisites.
As an aside, you might like to do
debug: CFLAGS+=-DFOO
which will just append the flags you need since the rest of CFLAGS might
still be relevant.
The second way to approach this is to use check what target was
specified on the command-line of Make and then set CFLAGS conditionally.
You can do that like this:
ifeq ($(MAKECMDGOALS),debug)
CFLAGS+=-DFOO
endif
But you are still going to need to hook 'debug' up to the rest of your
build in some way (above I did this with debug: all), so I think the
approach above is cleaner.
2) Why is the conditional macro definition syntax not incorporated
into gnu-make? It seems like a good idea.
It is; it's just called a 'target-specific variable' instead.
John.
--
John Graham-Cumming
address@hidden
Home: http://www.jgc.org/
POPFile: http://getpopfile.org/
GNU Make Standard Library: http://gmsl.sf.net/
Fast, Parallel Builds: http://www.electric-cloud.com/
Sign up for my Spam and Anti-spam Newsletter
at http://www.jgc.org/
PGP key: http://www.jgc.org/pgp/