[CMake] Help Accommodating Ninja when including CMAKE_CURRENT_BINARY_DIR

Robert Maynard robert.maynard at kitware.com
Tue May 8 12:27:00 EDT 2018


Here is how I would do this (
https://github.com/robertmaynard/Sandbox/tree/master/CMakeCustomCommandAfterTarget).
In my example the executable is the target I want to have a custom
PRE_BUILD step for.
On Sat, Apr 28, 2018 at 5:02 AM Stephen McDowell <sjm324 at cornell.edu> wrote:


> After much deliberation, I was never successful in getting a synthetic
target to work as expected.  The dilemma is that `add_custom_command`
cannot specify both `TARGET` and `OUTPUT`, the work flow was something like

> add_custom_target(resource-dependency)
> add_custom_command(TARGET resource-dependency ... PRE_BUILD)
> add_custom_command(OUTPUT <files that are generated> DEPENDS
resource-dependency)
> ...
> add_library(actual-obj-library OBJECt <files that are generated> ...
others ...)
> add_dependencies(actual-obj-library resource-dependency)

> I'm sure that's a flawed approach, but in the end I realized I can
actually generate these files at *configure* time <3  The
add_custom_command was just a call to ${CMAKE_COMMAND} ...
resources/bin2c.cmake.  So in the end, it's actually a lot cleaner for us
to just include(resources/bin2c.cmake).

> I doubt this solution will be widely applicable to many projects, but if
you ever find yourself in a situation where you are doing
add_custom_command, where the custom command is calling ${CMAKE_COMMAND},
don't rely on PRE_BUILD because it's not a guarantee.  If you can, just
execute the script at configure time via an include(...) call.


More information about the CMake mailing list