[CMake] add_custom_command

Michael Wild themiwi at gmail.com
Tue Oct 16 03:49:02 EDT 2012


On 10/15/2012 08:20 PM, Totte Karlsson wrote:
> 
> set( resultFileFolder "${PROJECT_SOURCE_DIR}/wiki/reports") set(
> resultFile "${resFileFolder}/tests_auto.xml")
> 
>> First things first: You should *never* pollute your source tree, always
>> output to the build tree.
> This is part of a google code project and the output file is a report in
> the wiki. It belongs in the repository. Actually, its not an output from
> the build itself, but output from a test, run just after the build.
> 
>>
>> You need a top-level target that DEPENDS on the output, that's all. E.g.
>>
>> add_custom_target(createWikiReports ALL DEPENDS ${resultFile})
> I ended up doing this instead;
> set( resultFileFolder "${CMAKE_SOURCE_DIR}/wiki/reports")
> 
> add_custom_target(test PACKAGE
>     COMMAND ${exe_path}/${target}.exe ${resultFileFolder}
> )
> 
> This seem to work well with borland, but with visual studio it is
> executed before the target is built, which is a problem, obviously.

You need a "DEPENDS ${target}", otherwise the build order of top-level
targets is determined by the build system. The drawback is, that custom
targets are always considedered to be out-of-date, i.e. will be invoked
on every single build. After all, they don't know anything about the
output...

> 
> So perhaps the combination of add_custom_command and add_custom_target
> is needed?

If you want to run the command only if any of the dependencies
(executable, input files) changes, yes.

> 
> Weird that it behaves differently using borland and visual studio though?

As mentioned, top-level targets without explicit dependencies are built
in which-ever order they please.

>> BTW: You don't need to specify the full path to the executable in the
>> COMMAND, and also no DEPENDS on it in the custom command. Just use the
>> target name, and CMake will handle the rest for you.
> I always seem to have problem with the paths regarding targets, because
> of Visual Studios addition of Debug and Release.
> -totte

It's not clear what you want to say here... Does it work with the target
name, or does it no?

This will always work:

add_executable(frobnicate frobnicate.c)

add_custom_command(OUTPUT ${resultFile}
  COMMAND frobnicate ${resultFileFolder}
  WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  COMMENT "Generating Wiki reports"
  VERBATIM)

add_custom_target(generateReports ALL DEPENDS ${resultFile})

Michael



More information about the CMake mailing list