CopyFiles.cmake 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. include(MakeTargetDirs)
  2. # copyFiles(target
  3. # [ALL]
  4. # COMMAND <cmd>
  5. # [ARGUMENTS <arg1> ... <argN>]
  6. # [OUTPUT_DIR <dir>]
  7. # [FORMAT <ext>]
  8. # FILES <source1> ... <sourceN>
  9. # [OUTPUT_FILES <outputVar>]
  10. # )
  11. function(copyFiles Target)
  12. CMAKE_PARSE_ARGUMENTS(
  13. ARGS
  14. "ALL"
  15. "OUTPUT_DIR;OUTPUT_FILES;INPUT_DIR;ARGUMENTS"
  16. "FILES"
  17. ${ARGN}
  18. )
  19. # Argument flags.
  20. if (ARGS_ALL)
  21. set(ARGS_ALL "ALL")
  22. else()
  23. set(ARGS_ALL)
  24. endif()
  25. # Argument options.
  26. if (ARGS_OUTPUT_DIR)
  27. file(TO_CMAKE_PATH ${ARGS_OUTPUT_DIR} ARGS_OUTPUT_DIR)
  28. endif()
  29. foreach(SrcFile ${ARGS_FILES})
  30. set(DstFile ${SrcFile})
  31. # Set src filename
  32. # Set destination filename to absolute.
  33. if (ARGS_INPUT_DIR)
  34. set(RealSrcFile ${ARGS_INPUT_DIR}/${SrcFile})
  35. else()
  36. set(RealSrcFile ${SrcFile})
  37. endif()
  38. # Set destination filename to absolute.
  39. if (ARGS_OUTPUT_DIR)
  40. get_filename_component(DstFile ${DstFile} NAME)
  41. set(DstFile ${ARGS_OUTPUT_DIR}/${DstFile})
  42. else()
  43. set(DstFile ${OUTPUT_DIR}/${DstFile})
  44. endif()
  45. get_filename_component(SrcFileFull ${RealSrcFile} ABSOLUTE)
  46. # Command needs existence of target directories.
  47. makeTargetDirs(${DstFile})
  48. #message("COPY ${SrcFileFull} TO ${DstFile} DEPENDS ${RealSrcFile}")
  49. add_custom_command(
  50. OUTPUT ${DstFile}
  51. COMMAND cp ${SrcFileFull} ${DstFile}
  52. DEPENDS ${RealSrcFile}
  53. VERBATIM
  54. )
  55. list(APPEND DstFiles ${DstFile})
  56. endforeach()
  57. if(ARGS_OUTPUT_FILES)
  58. set(${ARGS_OUTPUT_FILES} ${${ARGS_OUTPUT_FILES}} ${DstFiles} PARENT_SCOPE)
  59. else()
  60. add_custom_target(${Target} ${ARGS_ALL} DEPENDS ${DstFiles})
  61. endif()
  62. endfunction()