title: GNU make: Generating automatic dependencies with generated header files
body: <p>So I followed the <a href="http://make.paulandlesley.org/autodep.html">Advanced Auto-Dependency Generation</a> paper --</p>
  
  <p><em>Makefile</em>:</p>
  
  <pre><code>SRCS := main.c foo.c
  
  main: main.o foo.o
  
  %.o: %.c
      $(CC) -MMD -MG -MT '$@ $*.d' -c $&lt; -o $@
      cp $*.d $*.tmp
      sed -e 's;#.*;;' -e 's;^[^:]*: *;;' -e 's; *\\$$;;' \
          -e '/^$$/d' -e 's;$$; :;' &lt; $*.tmp &gt;&gt; $*.d
      rm $*.tmp
  
  clean::
      -rm *.o *.d main
  
  -include $(SRCS:.c=.d)
  </code></pre>
  
  <p><em>main.c</em>:</p>
  
  <pre><code>#include "foo.h"
  
  int main(int argc, char** argv) {
    foo() ;
    return 0 ;
  }
  </code></pre>
  
  <p><em>foo.h</em>:</p>
  
  <pre><code>#ifndef __FOO_H__
  #define __FOO_H__
  
  void foo() ;
  
  #endif
  </code></pre>
  
  <p>-- and it works like a charm.</p>
  
  <hr>
  
  <p>But when <code>foo.h</code> becomes a generated file --</p>
  
  <p>Makefile:</p>
  
  <pre><code>...
  
  HDRS := foo.h
  
  $(HDRS):
      mk_header.sh $*
  
  clean::
      -rm $(HDRS)
  ...
  </code></pre>
  
  <p>mk_header.sh:</p>
  
  <pre><code>#!/bin/bash
  UP=$(tr "[:lower:]" "[:upper:]" &lt;&lt;&lt; $1)
  
  cat &lt;&lt;EOF &gt; $1.h
  #ifndef __${UP}_H__
  #define __${UP}_H__
  
  void $1() ;
  
  #endif
  EOF
  </code></pre>
  
  <p>The 1st time I run <code>make</code>, <code>main.d</code> is not yet generated, and thus <code>foo.h</code> is not considered a prerequisite, and thus isn't been generated:</p>
  
  <pre><code>$ ls
  foo.c  main.c  Makefile  mk_header.sh*
  
  $ make
  cc -MMD -MG -MT 'main.o main.d' -c main.c -o main.o
  cp main.d main.tmp
  sed -e 's;#.*;;' -e 's;^[^:]*: *;;' -e 's; *\\$;;' \
      -e '/^$/d' -e 's;$; :;' &lt; main.tmp &gt;&gt; main.d
  rm main.tmp
  cc -MMD -MG -MT 'foo.o foo.d' -c foo.c -o foo.o
  cp foo.d foo.tmp
  sed -e 's;#.*;;' -e 's;^[^:]*: *;;' -e 's; *\\$;;' \
      -e '/^$/d' -e 's;$; :;' &lt; foo.tmp &gt;&gt; foo.d
  rm foo.tmp
  cc   main.o foo.o   -o main
  
  $ ls
  foo.c  foo.d  foo.o  
  main*  main.c  main.d  main.o  
  Makefile  mk_header.sh*
  </code></pre>
  
  <p>Only in the 2nd invocation of <code>make</code>, the <code>foo.h</code> is generated, and as a result another build cascades.</p>
  
  <pre><code>$ make
  ./mk_header.sh foo
  cc -MMD -MG -MT 'main.o main.d' -c main.c -o main.o
  cp main.d main.tmp
  sed -e 's;#.*;;' -e 's;^[^:]*: *;;' -e 's; *\\$;;' \
      -e '/^$/d' -e 's;$; :;' &lt; main.tmp &gt;&gt; main.d
  rm main.tmp
  cc   main.o foo.o   -o main
  
  $ ls
  foo.c  foo.d  foo.h  foo.o  
  main*  main.c  main.d  main.o  
  Makefile  mk_header.sh*
  </code></pre>
  
  <p>And only after that <code>make</code> realizes that:</p>
  
  <pre><code>$ make
  make: `main' is up to date.
  </code></pre>
  
  <hr>
  
  <p>So my question is: <strong>Is there a way to extend the recipe suggested by the paper above, to allow for generated header files, without the elimination of the performance gain realized by not having to re-evaluate the entire make tree when including the <code>*.d</code> fragments?</strong></p>