[CMake] copy_if_different on build

Matthew Woehlke matthew.woehlke at kitware.com
Fri Apr 19 19:30:01 EDT 2013


On 2013-04-17 12:41, Skippy VonDrake wrote:
> Thought I understood this - but alas my implementation is wrong.
>
> Here's my test case with a top-level cmake file and 3 subdirectories:
> 'src', 'bin' and 'outdir'.
>
> Top level CMakeLists.txt
>     cmake_minimum_required (VERSION 2.8)
>     project (copyFile)
>     set (TARGETNAME fooCopy)
>     set (TARGETDEST ${PROJECT_SOURCE_DIR}/bin)
>     add_subdirectory (src)
>
> src/CMakeLists.txt
>     add_executable (${TARGETNAME} 	main.cpp)
>     set (input  ${CMAKE_CURRENT_SOURCE_DIR}/bin/config.cfg)
>     set (output ${CMAKE_CURRENT_SOURCE_DIR}/outdir/config.cfg)
>
>     add_dependencies(${TARGETNAME}  ${output})
>
>     add_custom_command(
>         OUTPUT "${output}"
>         DEPENDS "${input}"
>         COMMAND ${CMAKE_COMMAND} -E copy_if_different
> 		"${input}" "${output}"
>     )
>
>     install (TARGETS ${TARGETNAME} DESTINATION ${TARGETDEST})
>
> The config file (config.cfg) is just some text and cpp file is just a
> "hello world"
> example.
>
> Since the config file is not source included as a dependency in the
> add_executable command, I assume my problem is making the target
> properly depend on it? Specifically ${TARGETNAME} should depend
> on "${output}"?

Hmm... yes, I'm not sure if add_dependencies can be used to add a file 
dependency to a target (the documentation only talks about adding other 
targets as dependencies).

Usually things like configured files are used as source files, i.e. as 
inputs to add_executable, etc.

If for some reason that doesn't work, I believe you can make your 
executable dependent on a custom target, which in turn depends on the 
output file from your custom command. IOW:

add_custom_command(OUTPUT out DEPENDS in COMMAND stuff)
add_executable(exe sources)
add_custom_target(exe_config DEPENDS out)
add_dependencies(exe exe_config)


If you only have one custom command, you can probably use 
add_custom_target to run the command instead, i.e.:

add_custom_target(exe_config DEPENDS in COMMAND stuff)
add_executable(exe sources)
add_dependencies(exe exe_config)


Hope that helps,

-- 
Matthew



More information about the CMake mailing list