Sergei Shumilin
Sergei Shumilin

Reputation: 431

Parent target name of the current target in GNU Makefile

What I need:

all: release debug

release: compile

debug: compile

compile: 
    if parent_target_name = release: 
        $(CXXFLAGS) = for rel
    else: $(CXXFLAGS) = for deb

The problem: How to check the name of the target which invoked the current target?

I've seen this question GNU Make get parent target name but it didn't help.

Upvotes: 2

Views: 1979

Answers (1)

Renaud Pacalet
Renaud Pacalet

Reputation: 29290

What you are probably looking for is Target-specific Variable Values. If you carefully read this section of the manual you'll see how they propagate to the prerequisites.

Just to illustrate how they work:

.PHONY: all release debug compile

all:
    $(MAKE) release
    $(MAKE) debug

release: CXXFLAGS = for rel
debug: CXXFLAGS = for deb

release debug: compile
    @echo 'building $@ with CXXFLAGS = $(CXXFLAGS)'

compile: a b c
    @echo 'building $@ with CXXFLAGS = $(CXXFLAGS)'

a b c:
    @echo 'building $@ with CXXFLAGS = $(CXXFLAGS)'

Demo:

$ make --no-print-directory all
make release
building a with CXXFLAGS = for rel
building b with CXXFLAGS = for rel
building c with CXXFLAGS = for rel
building compile with CXXFLAGS = for rel
building release with CXXFLAGS = for rel
make debug
building a with CXXFLAGS = for deb
building b with CXXFLAGS = for deb
building c with CXXFLAGS = for deb
building compile with CXXFLAGS = for deb
building debug with CXXFLAGS = for deb

Upvotes: 4

Related Questions