GNU Make get parent target name

In gnu make, is there a way to get hold of the original target that initiated the whole chain and lead the execution to the current recipe?

.PHONY : all clean common
all: common
clean: common

common:
    @echo $@
    @echo $(MAKECMDGOALS)
    @for a in $$(ls); do 
        if [ -d $$a ]; then 
            echo -C $$a $(MAKECMDGOALS); 
            #$(MAKE) -C $$a $(MAKECMDGOALS); 
        fi; 
    done;
    @echo "Done!"

As in the above example, when I run the 'make all' or 'make clean', I can use $(MAKECMDGOALS) to tell which target was passed to make from the command line. However, if someone calls it as 'make clean all', then 'common' is called once and we get 'make: Nothing to be done for `clean'.'.

So, is there a way to call 'common' for every time it is referenced as prerequisite for a target and also work out the originating target name so it can be passed into the next make? Even though 'common' is a PHONY target, it is only called once and I am not sure why.

I've got below alternatives but and I think the first one is the neatest

makefile 1

just_a_func = @echo "just_a_func from " $(1)

.PHONY : all clean
all:
    @echo $@ " enter"
    $(call just_a_func, $@)

clean:
    @echo $@ " enter"
    $(call just_a_func, $@)

makefile 2

.PHONY : all clean common
all:
    @echo $@ " enter"
    $(MAKE) -f makefile common

clean:
    @echo $@ " enter"
    $(MAKE) -f makefile common

common:
    @echo $@ " enter"

It strikes me as there are two questions here, the one in the title and another one slipped in when elaborating the problem.

In answer to how to the question in the title: getting hold of the target that leads to a change, use remake.

For example, using the Makefile you provided:

$ remake -X -f Makefile
GNU Make 4.1+dbg0.91
...
Reading makefiles...
Updating makefiles....
Updating goal targets....
 File 'PHONY' does not exist.
   File 'all' does not exist.
-> (/tmp/Makefile:5)
common:
remake<0> T
=>#0  common at /tmp/Makefile:5
  #1  all at /tmp/Makefile:2
  #2  PHONY at /tmp/Makefile:1
remake<1> quit

$ remake -X -f Makefile clean
GNU Make 4.1+dbg0.91
...
Reading makefiles...
Updating makefiles....
Updating goal targets....
 File 'clean' does not exist.
-> (/tmp/Makefile:5)
common:
remake<0> T
=>#0  common at /tmp/Makefile:5
  #1  clean at /tmp/Makefile:3
remake<1>

Above we so happened to start at the target we want to inspect. If that's not the case you can use the "breakpoint" command to set a breakpoint, and "continue" to run until you hit that target.

链接地址: http://www.djcxy.com/p/64124.html

上一篇: 使用假目标作为目标别名

下一篇: GNU Make获取父目标名称