GNU Make get parent target name

别等时光非礼了梦想. 提交于 2020-01-03 18:59:15

问题


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"

回答1:


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.



来源:https://stackoverflow.com/questions/30333798/gnu-make-get-parent-target-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!