Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>Keeping make from deleting intermediary files</h3> <p>I recommend against using <code>.PRECIOUS</code> (see below as to why). Using <code>.SECONDARY</code> would preserve the <code>.out</code> files:</p> <pre><code>TARGETS=foo bar all: $(TARGETS:=.all) %.all: %.out @echo Made $* %.out: touch $@ .SECONDARY: $(TARGETS:=.out) </code></pre> <p><code>$(TARGETS:=.all)</code> just appends <code>.all</code> to all names in <code>TARGETS</code>. <code>$(TARGETS:=.out)</code> appends <code>.out</code>. We apparently cannot use <code>%.out</code> as a target of <code>.SECONDARY</code>. These just save having to relist all targets individually.</p> <p>I prefer to not use <code>.PRECIOUS</code> for this because <a href="https://www.gnu.org/software/make/manual/make.html#Special-Targets" rel="nofollow">the documentation says</a></p> <blockquote> <p>if make is killed or interrupted during the execution of their recipes, the target is not deleted.</p> </blockquote> <p>This can leave corrupted files in the file system. Here's an example.</p> <pre><code>all: foo.all bar.all %.all: %.out @echo Made $* %.out: sh -e -c 'echo "{1, 2, 3" &gt; $@; FAIL!; echo "}" &gt;&gt; $@' .PRECIOUS: %.out </code></pre> <p>The FAIL! command simulates a tool that crashes in the middle of its work. Here's a shell session working with the Makefile above:</p> <pre><code>$ ls Makefile $ make sh -e -c 'echo "{1, 2, 3" &gt; foo.out; FAIL!; echo "}" &gt;&gt; foo.out' sh: 1: FAIL!: not found make: *** [foo.out] Error 127 $ cat foo.out {1, 2, 3 </code></pre> <p>Yikes... my <code>foo.out</code> file is incomplete. Let's try making again:</p> <pre><code>$ make Made foo sh -e -c 'echo "{1, 2, 3" &gt; bar.out; FAIL!; echo "}" &gt;&gt; bar.out' sh: 1: FAIL!: not found make: *** [bar.out] Error 127 $ cat *.out {1, 2, 3 {1, 2, 3 </code></pre> <p>Make is none the wiser about files left around by earlier runs so when you run make again, it will take the corrupted files at face value. <code>foo.out</code> was not remade (despite the "Made foo" message) because it already exists and the Makefile went straight to trying to make bar.</p> <p><code>.SECONDARY</code> makes it so that:</p> <blockquote> <p>The targets which .SECONDARY depends on are treated as intermediate files, except that they are never automatically deleted.</p> </blockquote> <p>This means they are never automatically deleted just <em>because they are intermediate files</em>. The default make behavior of deleting targets that were being rebuilt if the tool rebuilding them crashed is not affected.</p> <h3>Using <code>.PHONY</code> with pattern rules</h3> <p>It seems though that <code>.PHONY</code> works only for targets that are explicit, not inferred. I've not found documentation confirming this. However, this works:</p> <pre><code>TARGETS:=foo bar TARGETS_all:=$(TARGETS:=.all) .PHONY: all all: $(TARGETS_all) .PHONY: $(TARGETS_all) $(TARGETS_all): %.all: %.out @echo Made $* %.out: touch $@ .SECONDARY: $(TARGETS:=.out) </code></pre> <p>In this rule <code>$(TARGETS_all): %.all: %.out</code> <code>$(TARGETS_all):</code> gives the list of targets to which the pattern can be applied. It makes <code>foo.all</code> and <code>bar.all</code> explicit targets. Without this, they would be inferred targets.</p> <p>You can test that it works by creating file called <code>foo.all</code> in your directory and run make over and over. The <code>foo.all</code> file has no effect on make.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload