Shorter and less error-prone rule for automatic prerequisite generation in the GNU Make manual

2010-04-28 Thread Robert Jørgensgaard Engdahl
Hello GNU Make bug-list subscribers

On

http://www.gnu.org/software/make/manual/make.html#Automatic-Prerequisites

The rule for automatically generating prerequisites is

 %.d: %.c
 @set -e; rm -f $@; \
  $(CC) -M $(CPPFLAGS) $  $...@.; \
  sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g'  $...@.  $@; \
  rm -f $...@.

but could be replaced with

%.d : %.c
@$(CC) -MT $@ -MT $*.o -MM $(CPPFLAGS)  $  $@

This is simpler.
This fixes disagreements about object file names between the sed command 
and the cc command, should the c file not be found in the current 
directory.

Perhaps it would be time to make this rule part of the default rules in 
GNU Make? In that case, the -MG option should also be considered for 
avoiding errors when auto generated headers are included, perhaps via an 
CCMFLAGS variable.

Best regards Robert

---
Robert Jørgensgaard Engdahl
Cum Laude MSc. C.S. 
SW-Developer, RD
Video Services  Applications

Bang  Olufsen A/S
Peter Bangs Vej 15
DK-7600 Struer
Denmark

Phone: (+45)  96 84 44 05
Internal phone: 454405
e-mail: r...@bang-olufsen.dk
Location: Factory 1 - West___
Bug-make mailing list
Bug-make@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-make


Re: Shorter and less error-prone rule for automatic prerequisite generation in the GNU Make manual

2010-04-28 Thread Edward Welbourne
 It's also unnecessary - you don't need a rule for %.d at all. You can
 just generate the dependencies as a side-effect of compilation using
 -MMD or similar.

Well, if a .d file gets deleted while its .o file exists, you do need
to regenerate it - or regenerate the .o (which may cause wasteful
knock-on actions since it's now newer than it was, even if unchanged).
This matters if, for example, you have a make clean-depend rule to
purge dependency information.

 Then use '-include' to grab all the deps if they exist.

better: include only the .d files that correspond to .o files *that
exist* - if the .o file doesn't exist, you don't need to know what it
depends on, aside from the primary source from which it's compiled
(which is indicated by the pattern rule that makes the .o file).

 By providing a rule for the .d files you'll cause make to
 re-execute itself, so you'll just end up parsing the Makefile twice.

Only if you include .d files that aren't really needed (which is
common practice).

Amendments along these lines are part of the doc-update I keep not
finding time to finish up ...

Eddy.


___
Bug-make mailing list
Bug-make@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-make