2014-07-18 26 views
4

假設我有以下Makefile加前綴使與目標名稱輸出 - 像螞蟻確實

.PHONY: mytarget 
mytarget: 
    echo "Hello World!" 

運行make mytarget給出了以下的輸出:

echo "Hello World!" 
Hello World! 

我想看看什麼是一樣的東西下面

[mytarget] echo "Hello World!" 
[mytarget] Hello World! 

這可能嗎?

我一直在尋找http://www.gnu.org/software/make/manual/make.html,但還沒有找到一個解決方案(當然,我還沒有看全手動)

+1

該指令是'.PHONY:mytarget'。只是FYI。 –

+0

感謝您捕捉那個Etan,現在就糾正! – zsepi

回答

3

我不知道有現成的東西給你想要的東西在GNUmake中(基於你的鏈接,我假設我們正在談論這種味道)。你可以以某種方式效仿它,但恐怕沒有解決方案會是完美的。

使用調試標誌

make --debug=j mytarget會讓它即將推出,包括觸發它的目標的每個命令make輸出信息。在你的情況,結果將是:

Live child 0x021d2360 (mytarget) PID 10747 
echo Hello World! 
Hello World! 
Reaping winning child 0x021d2360 PID 10747 
Removing child 0x021d2360 PID 10747 from chain. 

你有信息,但它不是真的很漂亮,我想平行運行(-j選項)不會產生正確的輸出。

更改SHELL

可以設置SHELL變量告訴用於啓動命令make其解釋被認爲。在實際啓動命令之前,這可以用於(ab)執行回聲。在Makefile中的以下行

SHELL=/bin/sh -c 'echo -n "[[email protected]]: " && [email protected]' 

我們得到

echo "Hello World!" 
[mytarget]: "Hello World!" 

這是不完美的,因爲整條生產線的回聲由make直接完成,因此不會通過SHELL

影響改變命令本身

如果你可以自己修改命令,可以讓make擴大它們從而使最終輸出是你想要什麼:

[email protected] -n '[[email protected]]: ' && echo $(1) && echo -n '[[email protected]]: ' && $(1) 

.PHONY: all bla 

mytarget: 
    $(call echo_target, echo "Hello World!") 

將導致

[mytarget]: echo Hello World! 
[mytarget]: Hello World! 

但這需要添加call到你的Makefile的每個命令

更新:在縫合一起

正如Natan Reisner所述,上面提出的SHELL命令對於參數包含空格。作爲事實上,它也發生在我,你可以使用SHELL啓動命令之前做呼應:下面是含有空格的參數工作,一個新的建議,並回用[email protected]命令前綴:

SHELL=/bin/sh -c 'echo -n "[[email protected]] " && echo "[email protected]" && echo -n "[[email protected]] " && eval "[email protected]"' 

以下目標

mytarget: 
    echo 'Hello World!' 
    @touch foo\ bar 
    @ls -l "foo bar" 

我們得到這樣的結果

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 

注意,還有一個是起訴不產生任何輸出的命令,如觸摸。這可能可以通過將命令的輸出重定向到一個變量中進行緩解,並且只有在得到的長度不爲零時纔會回顯。

+1

當參數包含空格時,'Change SHELL'版本有問題。它不能正確保存參數。 –

+0

確實。我已經用更好的'SHELL'替換更新了答案,謝謝。 – Virgile

+0

這個答案似乎並不適用於make 4.0,儘管它在make 3.81上。 –

2

作爲@Virgile的Change SHELL選項的補充(有問題,請參閱我對他的回答的評論)。

您可以通過調整PS4來獲得執行的shell行的前綴。

SHELL=/bin/bash -x 
export PS4=[[email protected]] 

all: mytarget blah 

.PHONY: mytarget blah 

mytarget: 
     @echo 'Hello World!' 

blah: 
     @echo '[email protected]: output' 

$ make 
[mytarget] echo 'Hello World!' 
Hello World! 
[blah] echo 'blah: output' 
blah: output 

從理論上講,這意味着,如果你能得到Change SHELL選項以安全的方式工作(我做不到,但我覺得我失去了一些東西),你應該能夠得到你想要的。

+1

顯然,'PS4'是POSIX,所以你不需要'bash','sh'就足夠了。另一方面,請注意,它會在'stderr'上得到輸出,這可能會導致重定向和管道更加困難。 – Virgile

+0

@Virgile確實。這種打擊選擇不像打字習慣一樣必要。是的,這可能會使事情複雜化。 –

1

下面是使用remake(GNU的叉子做)時,對該文件運行的Makefile你給輸出:

$ remake -x 
Reading makefiles... 
Updating goal targets.... 
File 'mytarget' does not exist. 
Must remake target 'mytarget'. 
Makefile:3: target 'mytarget' does not exist 
##>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 
echo "Hello World!" 
##<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
Hello World! 
Successfully remade target file 'mytarget'. 

Makefile冒號後的數字給出裏面的Makefile中的行數目標。人字形將從其生成的輸出中調用的命令分開。如果一個目標失敗,你會得到一個類似於你在編程語言中獲得的目標棧跟蹤。

雖然這已經比您提出的要多一些,但如果您需要更多,則可以使用調試器,如果您使用-X而不是-x