Reputation: 1018
I am completely new to Cmake. My Query might be very simple for you
I want to copy few files from source directory to destination Directory. below is my Source directory structure
SourceDir\tem1.txt
SourceDir\tem1.log
SourceDir\tem1.xml
SourceDir\tem1.js
SourceDir\tem3.xml
SourceDir has another directory Say ChildDir ; below is the structure
SourceDir\ChildDir\tem1.ts
SourceDir\ChildDir\tem4.log
SourceDir\ChildDir\tem5.xml
SourceDir\ChildDir\tem6.js
SourceDir\ChildDir\tem7.xml
I want to copy only .xml and .js file in my destination directory. The output should be as shown below
DestinationDir/tem1.xml
DestinationDir/tem1.js
DestinationDir/tem3.xml
DestinationDir/tem5.xml
DestinationDir/tem6.js
DestinationDir/tem7.xml
below is my incomplete codesnipet
cmake_minimum_required(VERSION 3.2.0)
project(copyFiles)
add_custom_target(temp)
add_custom_command( TARGET temp
PRE_BUILD
${CMAKE_COMMAND} -E
COPY ${CMAKE_CURRENT_SOURCE_DIR}/SourceDir
${CMAKE_CURRENT_BINARY_DIR}/DestinationDir)
Is there any way to update this code snippet to get my work done? or do let me know if there is any other better solution. thanks in advance
Upvotes: 0
Views: 354
Reputation: 559
First step : match all .js and .xml files
You can use the file()
with GLOB_RECURSE :
file (GLOB_RECURSE
MY_FILES
"SourceDir/*.js"
"SourceDir/*.xml"
)
The GLOB_RECURSE will match all file in the folder and all its subfolders.
Now MY_FILES
variable have all your .xml and .js files.
Second step : copy files
Now MY_FILES variable is a list. Definition of a list in CMAKE :
NOTES: A list in cmake is a ; separated group of strings. To create a list the set command can be used. For example, set(var a b c d e) creates a list with a;b;c;d;e, and set(var “a b c d e”) creates a string or a list with one item in it. (Note macro arguments are not variables, and therefore cannot be used in LIST commands.)
Now you have two choices to copy.
At CMake time :
You can use file(COPY)
, this will copy file when you do cmake
:
file(COPY ${MY_FILES} DESINATION "DestinationDir/")
At make time :
That's the method you use currently. But you need to iterate on the list of MY_FILES
. In order to do this you need to use the foreach()
loop. The code will be something like :
foreach(MY_FILE ${MY_FILES})
add_custom_command( TARGET temp
PRE_BUILD
${CMAKE_COMMAND} -E
COPY ${MY_FILE}
${CMAKE_CURRENT_BINARY_DIR}/DestinationDir
)
endforeach()
Upvotes: 1