[CMake] How does IMPLICIT_DEPENDS work?

Paul Smith paul at mad-scientist.net
Sat Apr 27 13:49:42 EDT 2019


On Fri, 2019-04-26 at 17:26 -0400, Paul Smith wrote:
> We're trying to implement precompiled headers (yes, I've seen all the
> various github projects around this).  As part of this we need to make
> sure the precompiled header is rebuilt whenever any of the headers that
> it includes changes: this seems like exactly the job for
> add_custom_command's IMPLICIT_DEPENDS.
> 
> But, it doesn't seem to be doing anything at all for me so I wonder
> what I'm doing wrong.

OK, I figured out the issue.  The problem is a combination of the way I
was testing and a sort of bizarre "feature" of the CMake Makefile
generator:

The Makefile generator only generates prerequisite information when a
CMake target is completely built.  It doesn't generate the information
when an individual object file is built.

I was testing (to allow faster turnaround) by simply running:

  cmake ...
  make foo.cpp.o
  touch header.h
  make foo.cpp.o

and the PCH file was not being rebuilt even though it included
header.h.  After MUCH tearing of hair and groping around in the
generated makefiles, I realized that unless I built the full target and
not just the individual object file, no prerequisite information was
created.  I had to run:

  cmake ...
  make footarget
  touch header.h
  make foo.cpp.o

The first make would build foo.cpp.o and also generate all the
prerequisite information as a side-effect of creating footarget.  Then
the second make would correctly update the PCH file in addition to
rebuilding the object file.

Note this is not related to PCH at all: you can reproduce this with the
simplest of CMake configurations.

That is definitely a subtlety of the makefile generator that was
unexpected: maybe it will help someone else trying to do similar
testing.



More information about the CMake mailing list