Kitiara
Kitiara

Reputation: 498

Cmake copy directory if the files aren't exist

How may i copy contents of a directory into another directory if any of those files aren't exist in specified destination ? (i wanna copy just missing ones, not replace with the new)

Upvotes: 1

Views: 6623

Answers (1)

Kitiara
Kitiara

Reputation: 498

Here it is how you do..

1- Add CMakeLists.txt into the same directory that has the folder you wanna copy with it's contents.

file(GLOB_RECURSE allfiles RELATIVE "${CMAKE_SOURCE_DIR}/test/" "folder/*")

foreach( each_file ${allfiles} )
  set(destinationfile "${CMAKE_BINARY_DIR}/${each_file}")
  set(sourcefile "${CMAKE_CURRENT_SOURCE_DIR}/${each_file}")
    add_custom_command(TARGET uox3
      POST_BUILD
      COMMAND ${CMAKE_COMMAND}
      -Ddestinationfile=${destinationfile}
      -Dsourcefile=${sourcefile}
      -P ${CMAKE_CURRENT_SOURCE_DIR}/check.cmake
    )
endforeach(each_file)

2- Create another file called "check.cmake" at the same directory.

if(NOT EXISTS ${destinationfile})
  execute_process(COMMAND ${CMAKE_COMMAND}
  -E copy ${sourcefile} ${destinationfile})
endif()

That's it. I tested this for CMake 3.0.0 and it worked.

Upvotes: 2

Related Questions