GNU Make获取父目标名称 [英] GNU Make get parent target name

查看:74
本文介绍了GNU Make获取父目标名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在gnu make中,是否有办法掌握启动整个链的原始目标并将执行引导至当前配方?

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!"

如上例所示,当我运行'make all'或'make clean'时,我可以使用$(MAKECMDGOALS)告诉从命令行传递了要制造的目标.但是,如果有人称其为全部清理",那么通用"将被调用一次,而我们得到"make:对于"清理"无可奈何.

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'.'.

因此,是否有一种方法可以在每次将其称为目标的先决条件时调用"common",并且还可以计算出原始目标名称,以便可以将其传递给下一个品牌?即使'common'是一个PHONY目标,它也只被调用一次,我不确定为什么.

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

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.

例如,使用您提供的Makefile:

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>

上面我们碰巧是从我们要检查的目标开始的.如果不是这种情况,则可以使用"breakpoint"命令设置一个断点,然后继续运行"continue",直到达到该目标为止.

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.

这篇关于GNU Make获取父目标名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆