<p>I hope I haven't painted myself into a corner.  I've gotten what seems to be most of the way through implementing a Makefile and I can't get the last bit to work.  I hope someone here can suggest a technique to do what I'm trying to do.</p>

<p>I have what I'll call "bills of materials" in version controlled files in a source repository and I build something like:</p>

<pre><code>make VER=x
</code></pre>

<p>I want my Makefile to use $(VER) as a tag to retrieve a BOM from the repository, generate a dependency file to include in the Makefile, rescan including that dependency, and then build the product.  </p>

<p>More generally, my Makefile may have several targets -- A, B, C, etc. -- and I can build different versions of each so I might do:</p>

<pre><code>make A VER=x
make B VER=y
make C VER=z
</code></pre>

<p>and the dependency file includes information about all three targets.</p>

<p>However, creating the dependency file is somewhat expensive so if I do:</p>

<pre><code>make A VER=x
...make source (not BOM) changes...
make A VER=x
</code></pre>

<p>I'd really like the Makefile to not regenerate the dependency.  And just to make things as complicated as possible, I might do:</p>

<pre><code>make A VER=x
.. change version x of A's BOM and check it in
make A VER=x
</code></pre>

<p>so I need to regenerate the dependency on the second build.</p>

<p>The check out messes up the timestamps used to regenerate the dependencies so I think I need a way for the dependency file to depend not on the BOM but on some indication that the BOM changed.</p>

<p>What I've come to is making the BOM checkout happen in a .PHONY target (so it always gets checked out) and keeping track of the contents of the last checkout in a ".sig" file (if the signature file is missing or the contents are different than the signature of the new file, then the BOM changed), and having the dependency generation depend on the signatures).  At the top of my Makefile, I have some setup:</p>

<pre><code>BOMS = $(addsuffix .bom,$(MAKECMDGOALS)
SIGS = $(subst .bom,.sig,$(BOMS))

DEP = include.d
-include $(DEP)
</code></pre>

<p>And it seems I always need to do:</p>

<pre><code>.PHONY: $(BOMS)

$(BOMS):
     ...checkout TAG=$(VER) $@
</code></pre>

<p>But, as noted above, if i do just that, and continue:</p>

<pre><code>$(DEP) : $(BOMS)
     ... recreate dependency
</code></pre>

<p>Then the dependency gets updated every time I invoke make.  So I try:</p>

<pre><code>$(DEP) : $(SIGS)
     ... recreate dependency
</code></pre>

<p>and</p>

<pre><code>$(BOMS):
     ...checkout TAG=$(VER) $@
     ...if $(subst .bom,.sig,$@) doesn't exist
     ...  create signature file
     ...else
     ...  if new signature is different from file contents
     ...    update signature file
     ...  endif
     ...endif
</code></pre>

<p>But the dependency generation doesn't get tripped when the signature changes.  I think it's because because $(SIGS) isn't a target, so make doesn't notice when the $(BOMS) rule updates a signature.</p>

<p>I tried creating a .sig:.bom rule and managing the timestamps of the checked out BOM with touch but that didn't work.</p>

<p>Someone suggested something like:</p>

<pre><code>$(DEP) : $(SIGS)
    ... recreate dependency
$(BOMS) : $(SIGS)
    ...checkout TAG=$(VER) $@
$(SIGS) :
    ...if $(subst .bom,.sig,$(BOMS)) doesn't exist
    ...  create it
    ...else
    ...  if new signature is different from file contents
    ...    update signature file
    ...  endif
    ...endif
</code></pre>

<p>but how can the BOM depend on the SIG when the SIG is created from the BOM?   As I read that it says, "create the SIG from the BOM and if the SIG is newer than the BOM then checkout the BOM".  How do I bootstrap that process?  Where does the first BOM come from?</p>

[{'answer_id': 71623, 'author': 'mbyrne215', 'author_id': 5241, 'author_profile': 'https://Stackoverflow.com/users/5241', 'pm_score': 0, 'selected': False, 'text': "<p>I'm not a make expert, but I would try have $(BOMS) depend on $(SIGS), and making the $(SIGS) target execute the if/else rules that you currently have under the $(BOMS) target.</p>\n\n<pre><code>$(DEP) : $(SIGS)\n    ... recreate dependency\n$(BOMS) : $(SIGS)\n    ...checkout TAG=$(VER) $@\n$(SIGS) :\n    ...if $(subst .bom,.sig,$(BOMS)) doesn't exist\n    ...  create it\n    ...else\n    ...  if new signature is different from file contents\n    ...    update signature file\n    ...  endif\n    ...endif\n</code></pre>\n\n<p><strong>EDIT:</strong> You're right, of course, you can't have $(BOM) depend on $(SIGS).  But in order to have $(DEP) recreate, you need to have $(SIG) as a target.  Maybe have an intermediate target that depends on both $(BOM) and $(SIG).</p>\n\n<pre><code>$(DEP) : $(SIGS)\n    ... recreate dependency\n$(NEWTARGET) : $(BOMS) $(SIGS)\n$(BOMS) : \n    ...checkout TAG=$(VER) $@\n$(SIGS) :\n    ...if $(subst .bom,.sig,$(BOMS)) doesn't exist\n    ...  create it\n    ...else\n    ...  if new signature is different from file contents\n    ...    update signature file\n    ...  endif\n    ...endif\n</code></pre>\n\n<p>$(SIGS) might also need to depend on $(BOMS), I would play with that and see.</p>\n"}, {'answer_id': 87291, 'author': 'JesperE', 'author_id': 13051, 'author_profile': 'https://Stackoverflow.com/users/13051', 'pm_score': 1, 'selected': False, 'text': "<p>Make is very bad at being able to detect actual file changes, as opposed to just updated timestamps. </p>\n\n<p>It sounds to me that the root of the problem is that the bom-checkout always modifies the timestamp of the bom, causing the dependencies to be regenerated. I would probably try to solve this problem instead -- try to checkout the bom without messing up the timestamp. A wrapper script around the checkout tool might do the trick; first checkout the bom to a temporary file, compare it to the already checked out version, and replace it only if the new one is different.</p>\n\n<p>If you're not strictly bound to using make, there are other tools which are much better at detecting actual file changes (SCons, for example).</p>\n"}]
['https://Stackoverflow.com/questions/71534', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/7685/']