title: Makefile: dependencies on directory content when creating a tar file
body: <p>In my battle against <code>.PHONY</code> targets I re-wrote:</p>
  
  <pre><code># Makefile v0
  
  tar:
      tar -cf tarfile.tar dir
  
  .PHONY: tar
  </code></pre>
  
  <p>to be:</p>
  
  <pre><code># Makefile v1
  
  tar: tarfile.tar
  
  tarfile.tar: $(shell find dir)
      tar -cf $@ dir
  
  .PHONY: tar
  </code></pre>
  
  <p>Which seems to work for a toy example.</p>
  
  <p>However, I can see that this might do the wrong thing, depending on the time when <code>$(shell find dir)</code> is evaluated. If there is some rule that will create or delete files in <code>dir</code> after the Makefile is parsed, this might break.</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>Based on @user562374's answer, I seem to have a better solution, but consider this case:</p>
  
  <pre><code># Makefile v2
  
  tar: tarfile.tar
  
  tarfile.tar: dir/.dirstamp
      tar --create --exclude $&lt; --file $@ $(&lt;D)
  
  dir/.dirstamp: .FORCE
      [ ! -e $@ -o "$(find $(@D) -newer $@ -print -quit)" ] &amp;&amp; touch $@
  
  dir/a: src/a
      cp $&lt; $@
  
  .FORCE:
  .PHONY: .FORCE tar
  </code></pre>
  
  <p>Now suppose <code>src/a</code> has been changed. Since there is no dependency between <code>tarfile.tar</code> and <code>dir/a</code> (not direct nor indirect), the <code>tarfile.tar:</code> target might be evaluated before <code>dir/a:</code>, and thus <code>tarfile.tar</code> will not be up-to-date.</p>
  
  <hr>
  
  <p>So, my questions are: What is the best practice of dealing with such cases? Must I maintain the list of files in <code>dir</code> separately? If so what is the easiest way of doing it?</p>