help-make
[Top][All Lists]
Advanced

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

Re: conditional based on target? + profiling


From: John Graham-Cumming
Subject: Re: conditional based on target? + profiling
Date: Fri, 14 Jan 2005 09:29:35 -0500

On Thu, 2005-01-13 at 17:56, Ross Boylan wrote:
> Profiling my code is almost the same as a regular build, so I added a
> profile target.
>  
> The problem is that I need to add -pg to CXXFLAGS and LDFLAGS in this
> case, so that implicit rules work properly.
> 
> My understanding is that I can't do this via conditionals, since
> conditionals don't depend on target choice.

I'm not 100% sure what you mean by that last statement, but I'll try to
answer your question.   I think you have two ways you can handle a
profiling option: using a target-specific variable and using a
command-line override.

1. Target-specific variable

Suppose that your Makefile has an 'all' target that is used to build the
project under non-profiling conditions.  You could add the following:

    .PHONY: profile
    profile: all
    profile: CXXFLAGS += -pg

Target-specific variables are set in the target they are defined for and
all prerequisites.  So here CXXFLAGS is augmented for profile, all and
everything that all depends on.  This _does_ apply to implicit rules. 
Here's a little Makefile that verifies this:

    .PHONY: all
    all: foo.o

    .PHONY: profile
    profile: all
    profile: CXXFLAGS += -pg

You can see the difference if you compare running this Makefile on the
all target and the profile target using the -n switch.  (I created a
dummy file foo.cc):

    $ make -n all
    g++    -c -o foo.o foo.cc
    $ make -n profile
    g++ -pg   -c -o foo.o foo.cc

As you can see the CXXFLAGS have been altered in the profile target and
passed into the implicit rule to build foo.o.

2. Command-line override

Add code such that putting PROFILE=yes on the command-line results in
the profile build.

    ifeq ($(PROFILE),yes)
    CXXFLAGS += -pg
    endif

This works just fine too and has the advantage that you can use it with
any existing targets in the Makefile.

> I suspect the right thing is to make a separate directory for the
> profile build anyway, since otherwise I may get confused and mix
> different types of .o files (with and without -pg).  In that case the
> problem is easy to solve.

That's also a good option, but doesn't stop you using 1 or 2 above.  You
could have the output directory controlled by whether or not you are
doing profiling.  

John.
-- 
John Graham-Cumming

Home: http://www.jgc.org/
Work: http://www.electric-cloud.com/
POPFile: http://getpopfile.org/






reply via email to

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