copy.cmake 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # MIT License
  2. # Copyright (c) 2023 SineStriker
  3. # Description:
  4. # Copy file or directory to destination if different.
  5. # Mainly use `file(INSTALL)` to implement.
  6. # Usage:
  7. # cmake
  8. # -D src=<files/dirs...>
  9. # -D dest=<dir>
  10. # [-D dest_base=<dir>]
  11. # [-D args=<args...>]
  12. # -P copy.cmake
  13. # Check required arguments
  14. if(NOT src)
  15. message(FATAL_ERROR "src not defined.")
  16. endif()
  17. if(NOT dest)
  18. message(FATAL_ERROR "dest not defined.")
  19. endif()
  20. # Calculate destination
  21. if(dest_base)
  22. set(_dest_base ${dest_base})
  23. else()
  24. set(_dest_base ${CMAKE_BINARY_DIR})
  25. endif()
  26. get_filename_component(_dest ${dest} ABSOLUTE BASE_DIR ${_dest_base})
  27. # Copy
  28. foreach(_file IN LISTS src)
  29. # Avoid using `get_filename_component` to keep the trailing slash
  30. set(_path ${_file})
  31. if(NOT IS_ABSOLUTE ${_path})
  32. set(_path "${CMAKE_BINARY_DIR}/${_path}")
  33. endif()
  34. if(IS_DIRECTORY ${_path})
  35. file(INSTALL DESTINATION ${_dest}
  36. TYPE DIRECTORY
  37. FILES ${_path}
  38. ${args}
  39. )
  40. else()
  41. set(_paths)
  42. if(${_path} MATCHES "\\*\\*")
  43. file(GLOB_RECURSE _paths ${_path})
  44. else()
  45. file(GLOB _paths ${_path})
  46. endif()
  47. file(INSTALL DESTINATION ${_dest}
  48. TYPE FILE
  49. FILES ${_paths}
  50. ${args}
  51. )
  52. endif()
  53. endforeach()