bugmake - Bugs: bug #59243, Overcomplicated example of...

 
 

bug #59243: Overcomplicated example of automatic dependency configuration

Submitter:  None
Submitted:  Fri 09 Oct 2020 03:49:50 PM UTC
   
 
Severity:  3 - Normal Item Group:  Documentation
Status:  None Privacy:  Public
Assigned to:  None Open/Closed:  Open
Component Version:  None Operating System:  None
Fixed Release:  None Triage Status:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Fri 09 Oct 2020 10:32:58 PM UTC, comment #2: 

Regarding rm: this is needed in case the .d file is not writable for some reason.

As mentioned by Martin, the suggested replacement -MM doesn't work for all compilers.

I do agree that this code is still too obscure.

Really this entire section should be rewritten to include the "modern" method of dependency tracking, described here: http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/

It does lose us our big example for the usefulness of rebuilding included makefiles though :)

Paul D. Smith <psmith>
Group administrator
Fri 09 Oct 2020 09:55:35 PM UTC, comment #1: 

Given set -o pipefail isn't on by default even where supported, the OP neglects the benefit of set -e in preventing the existing dependency file from being truncated to empty if something goes wrong with the compiler step.  Even the original example does not meet my standard for being production quality because it doesn't take into account what happens if two builds run at the same time or the user hits Ctrl-C after the truncation but before the sed.  Scrambled, and particularly truncated, dependency files stop later rebuilds from being triggered when they should be, leading to hours of frustrating debugging, lost trust in the build system and a culture of wasting time with "make clean" and rebuilds.  Atomic replacement with mv FTW.

Finding the example in context, at https://www.gnu.org/software/make/manual/make.html#Automatic-Prerequisites, I see there is already prose discussing the set -e, explaining the cryptic sed, and suggesting the -MM change that the OP slipped in.  I too might have plunged in and spent minutes trying to understand it, only to kick myself when I spotted the explanation later.  With its two code snippets, the explanation is too large to have before the example.  I don't see a case to answer here.

Martin Dorey <mdorey>
Fri 09 Oct 2020 03:49:50 PM UTC, original submission:  

In section 4.14, the example of the pattern rule is unnecessarily complicated:

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

The first rm command is redundant as redirecting the output of the sed command with ">" will replace it anyway. Instead of creating another temporary file with "$@.$$$$", one can simply pipe the preprocessor output to sed input, making the second rm unnecessary as well. If a file must be created for some reason, why confuse beginners with "$@.$$$$" instead of just "$@.temp". The sed is also overengineered as is.

This example provides the same functionality:

%.d: %.c
    @printf "$@ " > $@
    @$(CC) $(CPPFLAGS) -MM $< >> $@

However, instead of spending 30 minutes to understand it, the developer only needs 5, as this example would be much clearer.

Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by psmith (Posted a comment)
  • -email is unavailable- added by mdorey (Posted a comment)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    No changes have been made to this item

    Back to the top

    Powered by Savane 3.13-d3ae.
    Corresponding source code