[CMake] Generate and install a file

Kyle Edwards kyle.edwards at kitware.com
Thu Mar 28 15:33:06 EDT 2019


On Thu, 2019-03-28 at 15:26 -0400, Norton Allen wrote:
> On 3/28/2019 3:11 PM, Kyle Edwards wrote:
> > On Thu, 2019-03-28 at 14:58 -0400, Norton Allen wrote:
> > > because mygeneratedfile is not a 'target', and I can't make it
> > > into a target with add_custom_target() because that's talking
> > > about something else entirely (commands that will be run
> > > unconditionally, not based on dependencies)
> > What exactly do you mean by "based on dependencies"? Do you mean
> > that this file has dependencies on something else, or that
> > something else has dependencies on it?
> I mean this file depends on some source files--if the source files
> change, I want to rerun the generation step.
Using the DEPENDS argument of add_custom_command() will do this.
However, add_custom_command() on its own isn't enough to ensure that
the file is generated before installation. You need to pair
add_custom_command() with add_custom_target(). Example:
add_custom_command(
  OUTPUT mygeneratedfile
  COMMAND mytool -o mygeneratedfile
  DEPENDS mysourcefile
)
add_custom_target(mygeneratedtarget
  DEPENDS mygeneratedfile
)

This is different from:

add_custom_target(mygeneratedtarget
  COMMAND mytool -o mygeneratedfile
  BYPRODUCTS mygeneratedfile
  DEPENDS mysourcefile
)

because it will only run mytool when it needs to (when mysourcefile has
changed).

Kyle
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190328/da37acba/attachment.html>


More information about the CMake mailing list