2014-11-23 43 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