I am attempting to copy multiple files using the ${CMAKE_COMMAND} -E copy <from> <to>
format, but I was wondering if there was a way to provide a number of files to copy to a specific directory. It seems the cmake copy only allows for one file to be copied at a time. I really don't want to use the copy command repeatedly when I would rather provide a list of files to copy as the first argument.
I'm thinking the easiest solution is to use the platform dependent "cp" command. While this definitely is not good for portability, our system is guaranteed to be built on Linux. A simple, platform independent solution would be better.
Copying multiple files is available from CMake 3.5
"cmake -E copy" support for multiple files
Command-Line Tool Mode
I did it with a loop
A relatively simple workaround would be to use
${CMAKE_COMMAND} -E tar
to bundle the sources, move the tarball and extract it in the destination directory.This could be more trouble than it's worth if your sources are scattered across many different directories, since extracting would retain the original directory structure (unlike using
cp
). If all the files are in one directory however, you could achieve the copy in just 2add_custom_command
calls.Say your sources to be moved are all in
${CMAKE_SOURCE_DIR}/source_dir
, the destination is${CMAKE_SOURCE_DIR}/destination_dir
and your list of filenames (not full paths) are in${FileList}
. You could do:Since I had more or less exactly the same issue and didn't like the solutions above I eventually came up with this. It does more than just copy files, but I thought I would post the whole thing as it shows the flexibility of the the technique in conjunction with generator expressions that allow different files and directories depending on the build variant. I believe the COMMAND_EXPAND_LISTS is critical to the functionality here. This function not only copies some files to a new directory but then runs a command on each of them. In this case it uses the microsoft signtool program to add digital signatures to each file.
I hope this saves someone the day or so it took me to figure this out.