title: Why .SECONDARY does not work with patterns (%) while .PRECIOUS does?
body: <p>My question is to understand better what i missed in make process and .SECONDARY purpose vs .PRECIOUS, not to get my script working, since it does work already.</p>
  
  <p>I am using make to either open a emacs editor on a file ( java but irrelevant for purpose of this question ) or to create it with a template if not existing.</p>
  
  <p>If it works well with existing files, <strong>when using generated file it is removed at the end</strong>.</p>
  
  <p>I added prerequisite in .SECONDARY but didn't help, i had to add it in .PRECIOUS.</p>
  
  <p>This is question <strong>why wasn't it working in .SECONDARY ?</strong> .</p>
  
  <p>From what i found on SO 
  <strong>.SECONDARY does not work with patterns ( % )</strong>, but even with knowing that i wonder if it is by design or if it is a bug in make. ( <a href="https://stackoverflow.com/questions/17625394/secondary-for-a-pattern-rule-with-gnu-make">.SECONDARY for a pattern rule with GNU Make</a> and <a href="https://stackoverflow.com/questions/19883282/makefile-pattern-rule-either-ignores-phony-rule-or-spontaneously-deletes-output/19892035#19892035">Makefile pattern rule either ignores phony rule or spontaneously deletes output file</a> )</p>
  
  <p>Here a stripped down content of my Makefile to reproduce my problem ( please create a com/stackoverflow/question directory to test it ).</p>
  
  <pre><code>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)/,,$@))\n{\n /** TODO */ \n}" &gt;$@ 
  
  work/%: $(PACKAGE_DIR)/$(subst work/,,%).java
      emacs $&lt;
  
  .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 
  </code></pre>
  
  <p>try </p>
  
  <p><strong>make work/SoTest</strong></p>
  
  <p>I understand this is flagged intermediate.</p>
  
  <p>then looking in SO i tried to set it in .SECONDARY: target list : does not work either.</p>
  
  <p>looking at make source code i spotted that make intermediate files removal is done within this context :</p>
  
  <pre><code>if (f-&gt;intermediate &amp;&amp; (f-&gt;dontcare || !f-&gt;precious)
      &amp;&amp; !f-&gt;secondary &amp;&amp; !f-&gt;cmd_target)
  </code></pre>
  
  <p>so i set my file in .PRECIOUS: and now it works.</p>
  
  <p><em>it displays to console :</em></p>
  
  <p>com/stackoverflow/question/SoTest.java</p>
  
  <p><em>it run emacs with right template in it so creation is OK</em>
  <em>here i exit emacs</em></p>
  
  <p><strong><em>and it removes the file at the end</em></strong></p>
  
  <p>rm com/stackoverflow/question/SoTest.java</p>
  
  <p>Removal at end is due to intermediate file, this can be seen with -d option on make</p>
  
  <p>LANG=C make -d work/SoTest</p>
  
  <pre><code>...
  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
  </code></pre>
  
  <p>To have it working i need to uncomment the .PRECIOUS paragraph.</p>
  
  <p><strong>make --version</strong></p>
  
  <pre><code>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 &lt;http://gnu.org/licenses/gpl.html&gt;
  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.
  </code></pre>