[CMake] Adding a generated file as a dependency to a target

Michael Hertling mhertling at online.de
Fri Jan 27 16:01:16 EST 2012


On 01/27/2012 07:15 PM, Michael Hertling wrote:
> On 01/27/2012 06:41 PM, Schuchard, Matthew wrote:
>> Contrary to the CMake FAQ, but consistent with what I have been reading elsewhere, it does not seem possible for me with CMake 2.8.6 to add a generated file as a dependency to a target, even in the same directory.
>>
>> I have done something similar to the following:
>>
>> add_custom_command(OUTPUT foo.ext)
>> add_executable(foo foo2.ext)
>> add_custom_target(foo_custom DEPENDS foo.ext)
>> set_source_files_properties(foo.ext PROPERTIES GENERATED TRUE)
>> add_dependencies(foo foo_custom)
>>
>> The above will not cause a dependency of foo on foo.ext, and when attempting to run make, throws an error of:
>> No rule to make target 'CMakeFiles/foo_custom.dir/requires'
>>
>> Could anyone please shine some light on this?
>> What I have been reading elsewhere (CMake mailing list, stackoverflow, etc.) is that the majority of the time, attempting something similar to the above does not succeed.
>> Could this fix be included among 2.8.8 updates?
> 
> Is foo2.txt in ADD_EXECUTABLE() a typo? Could you provide a minimal but
> complete example which exposes your issue for detailed investigation?
> 
> The DEPENDS clause of custom targets/commands should always be used
> with *full* paths, even if the concerned files are generated in the
> same CMakeLists.txt, since the behavior w.r.t. relative paths isn't
> specified. [...]

Addendum: The documentation for ADD_CUSTOM_COMMAND/TARGET() states

[ADD_CUSTOM_COMMAND()]
"If an output name is a relative path it will be interpreted relative to
the build tree directory corresponding to the current source directory."
...
"If any dependency is an OUTPUT of another custom command in the same
directory (CMakeLists.txt file) CMake automatically brings the other
custom command into the target in which this command is built."

and

[ADD_CUSTOM_TARGET()]
"Dependencies listed with the DEPENDS argument may reference files and
outputs of custom commands created with add_custom_command() in the
same directory (CMakeLists.txt file)."

IMO, these wordings imply that within the same CMakeLists.txt, one can
specify the same paths for the DEPENDS clauses as have been specified
for the OUTPUT clause - including relative ones, and AFAICS, it does
actually work. Nonetheless, the usual advice to use full paths makes
things bullet-proof.

Regards,

Michael

> [...] So, you might retry with DEPENDS ${CMAKE_CURRENT_BINARY_
> DIR}/foo.ext. Besides, the GENERATED property doesn't need to be set
> on files generated by ADD_CUSTOM_COMMAND(OUTPUT ...) as this is done
> automatically.
> 
> Regards,
> 
> Michael


More information about the CMake mailing list