MDN may have intermittent access issues April 18 13:00 - April 19 01:00 UTC. See whistlepig.mozilla.org for all notifications.

mozilla

Revision 78838 of Makefiles - Best practices and suggestions

  • Revision slug: How_Mozilla's_build_system_works/Makefiles_-_Best_practices_and_suggestions
  • Revision title: Makefiles - Best practices and suggestions
  • Revision id: 78838
  • Created:
  • Creator: Joey Armstrong
  • Is current revision? No
  • Comment 3 words added, 1 words removed

Revision Content

  • Always include dependencies when creating a target
    • Initial make call should always be the workhorse: build, generate, deploy, install, etc.
    • All subsequent make calls must become a NOP unless sources or dependencies change or have been removed.
    • Incorrect dependencies will contribute to wasted cycles and can contribute to circular dependencies.
  • Directory dependencies
    • Do not use directories as a dependency for generated targets.  Any activity within a directory will alter inodes forcing targets to always be stale.
    • Parallel make: add an explicit timestamp dependency (.done) that make can synchronize threaded calls on to avoid a race condition.
# Transient directory for storing timestamps
TS=.ts

#####################################################
## Extra dep needed to synchronize parallel execution
#####################################################
$(TS): $(TS)/.done
$(TS)/.done:
  
$(MKDIR) -p $(dir $@)
  
touch $@

#  "clean" target
GARBAGE_DIRS += $(TS)
  • Maintain clean targets - makefiles should be able to remove all content that is generated so "make clean" will return the sandbox/directory back to a clean state.
targets = foo bar tans timestampDIR = .ts  all: $(timestampDIR) $(targets)  %: %.c        $(CC) -o $@ $<  $(timestampDIR):     $(mkdir) $@  # "clean" target GARBAGE += $(targets) GARBAGE_DIRS += $(timestampDIR)
  • Wrapper check/unit tests with a ENABLE_TESTS conditional so tests can be disabled on demand.
ifdef ENABLE_TESTS 
ifeq ($(NULL),$(filter WINNT OS2,$(OS_ARCH)))
DIRS += test
endif # WIN
endif # ENABLE_TESTS

Revision Source

<ul> <li>Always include dependencies when creating a target <ul> <li>Initial make call should always be the workhorse: build, generate, deploy, install, etc.</li> <li>All subsequent make calls must become a NOP unless sources or dependencies change or have been removed.</li> <li>Incorrect dependencies will contribute to wasted cycles and can contribute to circular dependencies.</li> </ul> </li> <li>Directory dependencies <ul> <li>Do not use directories as a dependency for generated targets.  Any activity within a directory will alter inodes forcing targets to always be stale.</li> <li>Parallel make: add an explicit timestamp dependency (.done) that make can synchronize threaded calls on to avoid a race condition.</li> </ul> </li>
</ul>
<pre><span style="color:#008800;"># Transient directory for storing timestamps<br>TS=.ts<br><br>#####################################################<br></span><span style="color:#008800;">## Extra dep needed to synchronize parallel execution<br></span><span style="color:#008800;">#####################################################<br></span><span style="color:#008800;">$(TS): $(TS)/.done<br></span><span style="color:#008800;">$(TS)/.done:<br>  </span><span style="color:#008800;">$(MKDIR) -p $(dir $@)<br>  </span><span style="color:#008800;">touch $@ <br><br>#  "clean" target<br>GARBAGE_DIRS += $(TS)</span>
</pre>
<ul> <li>Maintain clean targets - makefiles should be able to remove all content that is generated so "make clean" will return the sandbox/directory back to a clean state.</li>
</ul>
<pre><span style="color: rgb(51, 153, 51);">targets = foo bar tans timestampDIR = .ts  all: $(timestampDIR) $(targets)  %: %.c        $(CC) -o $@ $&lt;  $(timestampDIR):     $(mkdir) $@  # "clean" target GARBAGE += $(targets) GARBAGE_DIRS += $(timestampDIR)</span>
</pre>
<ul> <li>Wrapper check/unit tests with a ENABLE_TESTS conditional so tests can be disabled on demand.</li>
</ul>
<pre class="bz_comment_text" id="comment_text_6"><span style="color: rgb(51, 153, 51);"><span class="quote">ifdef ENABLE_TESTS <br>    ifeq ($(NULL),$(filter WINNT OS2,$(OS_ARCH)))<br>        DIRS += test <br>    endif # WIN<br>endif # ENABLE_TESTS</span></span>
</pre>
Revert to this revision