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

Brad King brad.king at kitware.com
Fri Jan 27 13:25:00 EST 2012


On 1/27/2012 12: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
>
> 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

You don't need a separate custom target to generate a file if it will
be used by one target in the same directory.  The code

  add_custom_command(OUTPUT foo.ext ...)
  add_executable(foo foo.ext ...)

will cause it to be generated prior to compiling the sources in "foo".

Even so, your example works for me:

$ cat ../CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
project(FOO C)
add_custom_command(OUTPUT foo.ext COMMAND touch foo.ext)
add_executable(foo foo.c)
add_custom_target(foo_custom DEPENDS foo.ext)
set_source_files_properties(foo.ext PROPERTIES GENERATED TRUE) # not necessary
add_dependencies(foo foo_custom)

$ cmake ..
-- The C compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Configuring done
-- Generating done

$ make foo
Scanning dependencies of target foo_custom
[ 50%] Generating foo.ext
[ 50%] Built target foo_custom
Scanning dependencies of target foo
[100%] Building C object CMakeFiles/foo.dir/foo.c.o
Linking C executable foo
[100%] Built target foo

-Brad


More information about the CMake mailing list