2014-11-23 31 views
5

我的問題是要更好地理解我在製作過程中錯過了什麼.SECONDARY目的與PRECIOUS,不要讓我的腳本工作,因爲它已經工作。爲什麼.SECONDARY不支持模式(%),而.PRECIOUS呢?

我正在使用make來打開一個文件(java但與此問題無關)的emacs編輯器,或者使用模板(如果不存在)創建它。

如果它與現有文件很好地協作,使用生成的文件時,它將在末尾被刪除。

我在.SECONDARY中添加了先決條件,但沒有幫助,我不得不將它添加到.PRECIOUS中。

這是問題爲什麼它不在.SECONDARY中工作?

從我發現SO .SECONDARY不與模式(%)工作,但即使知道,我不知道這是由設計,或者如果它是在化妝的錯誤。 (.SECONDARY for a pattern rule with GNU MakeMakefile pattern rule either ignores phony rule or spontaneously deletes output file

這裏是我的Makefile的一個精簡的內容來重現我的問題(請創建一個com/stackoverflow/question目錄來測試它)。

PACKAGE=com.stackoverflow.question 
PACKAGE_DIR=$(subst .,/,$(PACKAGE)) 
OUT=out 

clean: 
    find $(OUT) -name "*.class" -type f -print0|xargs -0 rm 

# does not work : deleted at end due to intermediate file removal. 
$(PACKAGE_DIR)/%.java: 
    @echo "package com.stackoverflow.question;\npublic class $(subst .java,,$(subst $(PACKAGE_DIR)/,,[email protected]))\n{\n /** TODO */ \n}" >[email protected] 

work/%: $(PACKAGE_DIR)/$(subst work/,,%).java 
    emacs $< 

.PHONY: clean work/% 

# tried to avoid intermediate file removal : does not work 
.SECONDARY: $(PACKAGE_DIR)/%.java 

# if not commented this does work : once precious intermediate file is not removed. 
#.PRECIOUS: $(PACKAGE_DIR)/%.java 

嘗試

使工作/ SoTest

我明白這是標記中間。

然後看在SO我試圖設置它.SECONDARY:目標列表:也不工作。

看着化妝源代碼,我發現,使中間文件去除這一背景下完成的:

if (f->intermediate && (f->dontcare || !f->precious) 
    && !f->secondary && !f->cmd_target) 

,所以我把我的文件中.PRECIOUS:現在它的工作原理。

它顯示安慰:

COM /計算器/問題/ SoTest.java

它運行與合適的模板的emacs在這麼創作是OK 這裏我退出Emacs

並刪除文件末尾

rm com/stackoverflow/question/SoTest。java的

去除月底是由於中間文件,這可以用在化妝

-d選項中可以看出

LANG = C使-d工作/ SoTest

... 
Must remake target 'work/SoTest'. 
emacs com/stackoverflow/question/SoTest.java 
Putting child 0xc3b580 (work/SoTest) PID 20681 on the chain. 
Live child 0xc3b580 (work/SoTest) PID 20681 
Reaping winning child 0xc3b580 PID 20681 
Removing child 0xc3b580 PID 20681 from chain. 
Successfully remade target file 'work/SoTest'. 
Removing intermediate files... 
rm com/stackoverflow/question/SoTest.java 

有它的工作,我需要取消.PRECIOUS段的註釋。

使--version

GNU Make 4.0 
Construit pour x86_64-pc-linux-gnu 
Copyright (C) 1988-2013 Free Software Foundation, Inc. 
Licence GPLv3+ : GNU GPL version 3 ou ultérieure <http://gnu.org/licenses/gpl.html> 
Ceci est un logiciel libre : vous êtes autorisé à le modifier et à la redistribuer. 
Il ne comporte AUCUNE GARANTIE, dans la mesure de ce que permet la loi. 
+0

http://stackoverflow.com/questions/17625394/secondary-for-a-pattern-rule-with-gnu-make inidicates **。寶貴的作品與模式,但奇怪的是,沒有.SECONDARY。**會解釋這種行爲。 – 2014-11-23 17:05:40

+0

**我們顯然不能使用%.out作爲.SECONDARY的目標。**看來另一個確認wihtin http://stackoverflow.com/questions/19883282/makefile-pattern-rule-either-ignores-phony-rule-or -spontaneously-deletes-output/19892035#19892035 – 2014-11-23 17:24:39

+1

請注意,.PRECIOUS在作爲.SECONDARY的替代方法時有缺點:make不會因信號而刪除中斷文件,這會打開部分寫入的對象文件到達的可能性鏈接器([source](https://www.gnu.org/software/make/manual/html_node/Interrupts.html))。 – alexei 2015-09-29 00:10:26

回答

4

答案「爲什麼.SECONDARY不與模式(%)工作,同時.​​PRECIOUS呢?」here:文件說

你也可以列出一個隱含規則的目標模式(如「%的.o」)作爲特殊目標.PRECIOUS的先決條件文件

但並沒有說這個關於.SECONDARY。但是對於少數明顯的例外情況,沒有一個特殊目標接受模式。

+1

即使經過多年的使用,似乎我仍在使用它的基本水平......並且當然要感謝。 – 2014-11-23 17:52:02

+0

看到我的答案... – 2014-11-23 19:09:30

+2

這不回答「爲什麼」;它回答「是否記錄在案,如果有的話,在哪裏?」 – Kaz 2014-12-05 00:30:35

10

感謝亞歷克斯(見答案)我進一步在我的搜索。

,我發現那是什麼它被記錄在化妝項目TODO.private 15年....

使用Git://git.savannah.gnu.org/make.git你可以看到歷史TODO.private內容:

6) Right now the .PRECIOUS, .INTERMEDIATE, and .SECONDARY 
    pseudo-targets have different capabilities. For example, .PRECIOUS 
    can take a "%", the others can't. Etc. These should all work the 
    same, insofar as that makes sense. 

這些都應該工作一樣,只要是有道理的。但沒有編碼。

+2

'有意義',我的.PRECIOUS %% – 2014-11-23 20:59:41

+0

可能 - 爲 - 主 - RMS的響鈴... – 2014-11-23 21:19:32

相關問題