[CMake] Handling generated headers

Michael Ellery mellery451 at gmail.com
Mon Mar 27 11:08:50 EDT 2017


> On Mar 27, 2017, at 6:31 AM, Bruce Stephens <bruce.r.stephens at gmail.com> wrote:
> 
> I have a build with two or three tools that generate headers and
> source files. Getting the source files compiled is easy enough: when
> they're mentioned as source files (in add_library or add_executable)
> the custom rule gets triggered.
> 
> But that doesn't seem to be true for header files included by
> non-generated source files, presumably because CMake's not looking at
> those files, but rather Ninja is (presumably other generators will
> behave similarly). If the (generated) header changes then anything
> using
> the source file gets rebuilt (as expected), but the first build seems
> not to necessarily succeed (depending on accidents of when the
> generated files are produced).
> 
> Concretely, suppose I have an XML file compat.xml with a custom rule
> to generate messages/Compat.h, and some source files which include
> that header (which does not exist to begin with), I'd like a compile
> of any of those source files to depend on messages/Compat.h such that
> it'll be generated if necessary.
> 
> That doesn't seem to happen automatically using the Ninja generator.
> Is that expected, or have I messed something up?
> 
> For the moment I'm assuming it's as expected and have a workaround: a
> simple Python script that scans source files and creates a deps.cmake
> with calls
> to add_file_dependencies. (Fortunately all the generated headers have
> simple patterns so accurately determining them is straightforward.)
> 
> Unfortunately there are also (non-generated) header files which
> include these headers and that seems harder to handle.
> Calling add_file_dependencies on header files doesn't seem to work.
> 
> It's not a showstopper: I can just explicitly add some dependencies.
> And later, we'll probably split out some of these things so they'll
> be separate builds. It seems a bit annoying, though. Am I missing some
> better way to handle this?
> -- 


it’s not clear from your message — are you using add_custom_command() to create the rule that generates the header from the XML? If you get all of the parameters right to that command (like OUTPUT, DEPENDS, BYPRODUCTS..), then I would expect this to work and the header should get generated before any other source that needs it. In some cases you might need to explicitly specify that a target depends on that header, but if at least ONE thing depends on the header, the the add_custom_command rule should get executed.  You might also try running CMAKE with —trace and generating a verbose makefile if you are still not seeing your custom command running when it should.

-Mike Ellery


More information about the CMake mailing list