[CMake] custom_command

Eric Noulard eric.noulard at gmail.com
Thu Oct 4 02:26:31 EDT 2007


2007/10/4, Javier Gonzalez <javierggt at yahoo.com>:
> Hi all,
>
> I'm having trouble creating files using ADD_CUSTOM_COMMAND. Take a look
> at this
> CMakeLists.txt:
>
> ADD_CUSTOM_COMMAND( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Dir_2/file
>                                                       COMMAND mkdir -p
[...]
>
> ADD_EXECUTABLE( my_exe my_source.cc
>                                       Dir_1/file
>                                       ${CMAKE_CURRENT_BINARY_DIR}/Dir_2/file
> )
>
> If I build in the source directory it works (that meaning that the files
> are created). When I do it in another directory, only the second one is
> created but the build succeeds. The file my_source.cc is a dummy file
> with just an empty main().
>
> I expected that it would fail, since neither
> ${CMAKE_CURRENT_BINARY_DIR}/Dir_1/file nor
> ${CMAKE_CURRENT_SOURCE_DIR}/Dir_1/file exist and is not created but
> there are no complains!
>
> If I add a dummy target for each file and then add the dependencies,
> then it works but I thought that was not the idea.
>
> What am I missing?


I don't really know why this fails but if your need is about
"simple" file copy of file copy with some var substitution in it
you should use CONFIGUE_FILE macro

CONFIGURE_FILE(Dir_2/file.in
                            ${CMAKE_CURRENT_BINARY_DIR}/Dir_2/file
                            COPYONLY)

CONFIGURE_FILE(Dir_1/file.in
                            ${CMAKE_CURRENT_BINARY_DIR}/Dir_1/file
                             COPYONLY)

should be just what you need

If you need more than that and really want to mkdir and cp
yourself you should use CMake command mode because
it more portable:

cmake -E copy or copy_if_different etc..
however command lacks "make_directory" command
which is not yet in 2.4.6. May be in 2.4.7
run
cmake -E
in order to get the list of supported command.

for example in a ADD_CUSTOM_COMMAND you may write

COMMAND ${CMAKE_COMMAND} -E copy <infile> <outfile>


-- 
Erk


More information about the CMake mailing list