[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: target-specific object files
From: |
Paul Smith |
Subject: |
Re: target-specific object files |
Date: |
Sat, 14 Nov 2015 13:44:40 -0500 |
On Fri, 2015-11-13 at 13:29 -0800, Ted Stern wrote:
> I'm familiar with target-specific variables and have been using them
> for many years.
>
> But they don't seem to be doing what I want in one circumstance -- I
> want to build different target names depending on an inherited
> target-specific variable.
You cannot do this. First, remember that all rules are parsed by make
initially upon reading in the makefiles. All variables that are part of
target and prerequisite names are expanded at that time.
When make is parsing the makefile it cannot know what the value of
EXESUFF will be later on, when it's running rules.
As the docs say: target-specific variables can ONLY be used inside of
recipes. They cannot be used in either target or prerequisite lists.
Secondary expansion is a caveat but that only applies to prerequisite
lists, not target lists.
> EXESUFF =# blank to start
>
> a$(EXESUFF).o : a.cpp
> <tab> $(CXX) $(CXXFLAGS) -c $< -o $@
>
> OBJS = a$(EXESUFF).o
>
> b$(EXESUFF).out : $(OBJS)
> <tab> $(CXX) $(CXXFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
>
> all: b.out
>
> clean:
> <tab> /bin/rm *.o *.out
>
> test: EXESUFF = _test
> test: CXXFLAGS += -D__TEST__
>
> % make all
>
> (builds a.o and b.out)
>
> % make clean; make test
>
> (builds a.o and b.out -- but I want it to build a_test.o and
> b_test.out)
>
> Is there a way to do what I want?
Well, there are many ways depending on exactly what you want. If you
only want to do it as above, where you run different make invocations,
you can change your makefile to something like:
ifeq ($(filter test,$(MAKECMDGOALS)),test)
EXESUFF = _test
else
EXESUFF =
endif
Now if you run "make test" you'll get the test output. But, if you run
"make all test" you'll still get the test output (only).
If you want to be able to build all the different outputs inside the
same make invocation you'll have to get fancier and use metaprogramming
of some type; either secondary expansion or eval. But without more
details of your requirements and restrictions it's hard to say.
See this: http://make.mad-scientist.net/category/metaprogramming/
for some hints.