[CMake] Copying directories as post build events without losing parent directory

Robert Dailey rcdailey.lists at gmail.com
Mon Dec 7 17:15:50 EST 2015


On Mon, Dec 7, 2015 at 3:59 PM, David Cole <DLRdave at aol.com> wrote:
> $ cmake -E copy_directory foo bar/foo
>
> ?
>
>
> On Mon, Dec 7, 2015 at 4:53 PM, Robert Dailey <rcdailey.lists at gmail.com> wrote:
>> I have a custom target which runs a command similar to this:
>>
>> $ cmake -E copy_directory foo bar
>>
>> The problem is that the contents of "foo" are copied inside of "bar",
>> instead of it creating a directory "bar/foo" and putting the contents
>> in there.
>>
>> Is there a way to make copy_directory behave like this? If not, I
>> guess my only option is to create a little CMake script that runs
>> file(INSTALL) to copy the directories like I want.
>> --
>>
>> Powered by www.kitware.com
>>
>> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>>
>> Kitware offers various services to support the CMake community. For more information on each offering, please visit:
>>
>> CMake Support: http://cmake.org/cmake/help/support.html
>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>
>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/cmake

That's the obvious answer but due to complexity reasons I don't want
to repeat the destination directory name. I want the copy to be
inclusive of the source directory name. I'd have to use
get_filename_component() to find the leaf directory name in the list
of directories I provide to my function for copying, simply to repeat
that name in the destination path I build. I want to avoid this.
Another weirdness is that I like to pass in a list with a mixture of
directories and files. Think of this is a way for me to pass in a list
of game data resources that need to be copied to the EXE directory so
the game can find them and run. I also have to add weird
if(IS_DIRECTORY) checks so I can tell my custom command to use "copy"
or "copy_directory".

A simple little install.cmake script that I run is able to do this,
but as far as complexity goes, it doesn't save me a whole lot:

function( copy_data )
    if( NOT TARGET copy_data )
        add_custom_target( copy_data COMMENT "Copying data directories
and files..." )
    endif()

    set( data_directories ${ARGN} )

    foreach( data ${data_directories} )
        get_filename_component( data ${data} ABSOLUTE )
        set( output_dir "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$<CONFIGURATION>" )
        set( install_script "${BUILD_CMAKE_DIR}/scripts/install.cmake" )

        add_custom_command( TARGET copy_data POST_BUILD
            COMMAND ${CMAKE_COMMAND}
            ARGS "-DSOURCE=${data}" "-DOUTPUT_DIR=${output_dir}" -P
"${install_script}"
        )
    endforeach()
endfunction()


More information about the CMake mailing list