prefixing使与目标名称输出 - 像蚂蚁呢 [英] prefixing make output with target name - like ant does
问题描述
假设我有如下的的Makefile
:
.PHONY: mytarget
mytarget:
echo "Hello World!"
运行使mytarget
给出了以下的输出:
echo "Hello World!"
Hello World!
我想看到的是类似下面的
what I would like to see is something like the below
[mytarget] echo "Hello World!"
[mytarget] Hello World!
这可能吗?
我一直在寻找 http://www.gnu.org/software /make/manual/make.html 但还没有找到一个解决方案(当然,我还没有看过整本手册)
I've been searching http://www.gnu.org/software/make/manual/make.html but haven't yet found a solution (granted, I haven't yet read the whole manual)
推荐答案
我不知道有现成的东西给你想要什么gnumake的(根据您的链接,我认为我们正在谈论这个味道) 。你可以在某种程度上效仿,但恐怕没有解决将是完美的。
I'm not sure that there is something readily available for what you want in GNUmake (based on your link, I assume that we are speaking about this flavor). You can emulate it somehow, but I'm afraid no solution will be perfect.
使--debug = j的mytarget
将让使得它即将推出的每个命令
输出信息,包括触发它的目标。你的情况,结果将是:
make --debug=j mytarget
will let make
output information about each command it is about to launch, including the target that triggers it. In your case, the result will be:
Live child 0x021d2360 (mytarget) PID 10747
echo Hello World!
Hello World!
Reaping winning child 0x021d2360 PID 10747
Removing child 0x021d2360 PID 10747 from chain.
您的资料,但它不是真正的pretty,我想平行运行( -j
选项)不会产生正确的输出。
You have the information, but it's not really pretty, and I guess that parallel runs (-j
options) won't produce a correct output.
您可以设置 SHELL
变量来告诉制作
其中除preTER应该是用于启动命令。这可能是(AB)实际使用中启动命令之前执行的回声。在Makefile中的以下行
You can set the SHELL
variable to tell make
which interpreter is supposed to be used for launching the command. This can be (ab)used to perform an echo before actually launching the command. with the following line in the Makefile
SHELL=/bin/sh -c 'echo -n "[$@]: " && $$@'
我们获得
echo "Hello World!"
[mytarget]: "Hello World!"
这是不完美的,因为整条生产线的回声由直接完成制作
,因此不会通过 SHELL影响
This is not perfect, as the echoing of the whole line is done directly by make
and thus not impacted by SHELL
如果您可以修改自己的命令,就可以让制作
展开它们,使最终输出的是你想要什么:
If you can modify the commands themselves, it is possible to let make
expand them so that the final output is what you want:
echo_target=@echo -n '[$@]: ' && echo $(1) && echo -n '[$@]: ' && $(1)
.PHONY: all bla
mytarget:
$(call echo_target, echo "Hello World!")
将导致
[mytarget]: echo Hello World!
[mytarget]: Hello World!
但这需要添加呼叫
来你的Makefile
but this requires adding the call
to each command of your Makefile
正如纳坦赖斯纳,在 SHELL提到
上面提出的命令是不包含空格的论点是正确的。作为事实上,它也发生,我认为你可以使用 SHELL
做呼应启动命令之前:这是一个包含空格的参数工作,一个新的建议,并回用 $ @
prepended命令:
As mentioned by Natan Reisner, the SHELL
command proposed above is not correct for arguments that contain spaces. As a matter of fact, it also occured to me that you can use SHELL
to do the echoing before launching the command: Here is a new proposal that work with arguments containing spaces, and echoes the command with $@
prepended:
SHELL=/bin/sh -c 'echo -n "[$@] " && echo "$$@" && echo -n "[$@] " && eval "$$@"'
以下目标
mytarget:
echo 'Hello World!'
@touch foo\ bar
@ls -l "foo bar"
我们得到这样的结果。
we obtain this result
echo 'Hello World!'
[mytarget] echo 'Hello World!'
[mytarget] Hello World!
[mytarget] echo foo\ bar
[mytarget] foo bar
[mytarget] touch foo\ bar
[mytarget] [mytarget] ls -l "foo bar"
[mytarget] -rw-rw-r-- 1 virgile virgile 0 juil. 23 08:44 foo bar
请注意,仍然存在着对不产生任何输出的命令,例如触摸的问题。这或许可以通过重定向命令的输出在一个变量,呼应仅当所得长度为非零来减轻。
Note that there is still an issue for commands that do not produce any output, such as touch. This could probably be mitigated by redirecting the output of the command in a variable, and echoing only if the resulting length is non-zero.
这篇关于prefixing使与目标名称输出 - 像蚂蚁呢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!