copy_files.cmake 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. include("${CONFIG_FILE}")
  2. set(prefix "COPYFILES: ")
  3. set(use_symlink 0)
  4. if(IS_SYMLINK "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/symlink_test")
  5. set(use_symlink 1)
  6. endif()
  7. set(update_dephelper 0)
  8. if(DEFINED DEPHELPER AND NOT EXISTS "${DEPHELPER}")
  9. set(update_dephelper 1)
  10. endif()
  11. set(__state "")
  12. macro(copy_file_ src dst prefix)
  13. string(REPLACE "${CMAKE_BINARY_DIR}/" "" dst_name "${dst}")
  14. set(local_update 0)
  15. if(NOT EXISTS "${dst}")
  16. set(local_update 1)
  17. endif()
  18. if(use_symlink)
  19. if(local_update OR NOT IS_SYMLINK "${dst}")
  20. message("${prefix}Symlink: '${dst_name}' ...")
  21. endif()
  22. get_filename_component(target_path "${dst}" PATH)
  23. file(MAKE_DIRECTORY "${target_path}")
  24. execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${src}" "${dst}"
  25. RESULT_VARIABLE SYMLINK_RESULT)
  26. if(NOT SYMLINK_RESULT EQUAL 0)
  27. #message("Symlink failed, fallback to 'copy'")
  28. set(use_symlink 0)
  29. endif()
  30. endif()
  31. if(NOT use_symlink)
  32. if("${src}" IS_NEWER_THAN "${dst}" OR IS_SYMLINK "${dst}")
  33. file(REMOVE "${dst}") # configure_file(COPYONLY) doesn't update timestamp sometimes
  34. set(local_update 1)
  35. endif()
  36. if(local_update)
  37. message("${prefix}Copying: '${dst_name}' ...")
  38. configure_file(${src} ${dst} COPYONLY)
  39. else()
  40. #message("${prefix}Up-to-date: '${dst_name}'")
  41. endif()
  42. endif()
  43. if(local_update)
  44. set(update_dephelper 1)
  45. endif()
  46. file(TIMESTAMP "${dst}" dst_t UTC)
  47. set(__state "${__state}${dst_t} ${dst}\n")
  48. endmacro()
  49. if(NOT DEFINED COPYLIST_VAR)
  50. set(COPYLIST_VAR "COPYLIST")
  51. endif()
  52. list(LENGTH ${COPYLIST_VAR} __length)
  53. message("${prefix}... ${__length} entries (${COPYLIST_VAR})")
  54. foreach(id ${${COPYLIST_VAR}})
  55. set(src "${${COPYLIST_VAR}_SRC_${id}}")
  56. set(dst "${${COPYLIST_VAR}_DST_${id}}")
  57. if(NOT EXISTS ${src})
  58. message(FATAL_ERROR "Source file/dir is missing: ${src} (${CONFIG_FILE})")
  59. endif()
  60. set(_mode "COPYFILE")
  61. if(DEFINED ${COPYLIST_VAR}_MODE_${id})
  62. set(_mode "${${COPYLIST_VAR}_MODE_${id}}")
  63. endif()
  64. if(_mode STREQUAL "COPYFILE")
  65. #message("... COPY ${src} => ${dst}")
  66. copy_file_("${src}" "${dst}" "${prefix} ")
  67. elseif(_mode STREQUAL "COPYDIR")
  68. get_filename_component(src_name "${src}" NAME)
  69. get_filename_component(src_path "${src}" PATH)
  70. get_filename_component(src_name2 "${src_path}" NAME)
  71. set(src_glob "${src}/*")
  72. if(DEFINED ${COPYLIST_VAR}_GLOB_${id})
  73. set(src_glob "${${COPYLIST_VAR}_GLOB_${id}}")
  74. endif()
  75. file(GLOB_RECURSE _files RELATIVE "${src}" ${src_glob})
  76. list(LENGTH _files __length)
  77. message("${prefix} ... directory '.../${src_name2}/${src_name}' with ${__length} files")
  78. foreach(f ${_files})
  79. if(NOT EXISTS "${src}/${f}")
  80. message(FATAL_ERROR "COPY ERROR: Source file is missing: ${src}/${f}")
  81. endif()
  82. copy_file_("${src}/${f}" "${dst}/${f}" "${prefix} ")
  83. endforeach()
  84. endif()
  85. endforeach()
  86. set(STATE_FILE "${CONFIG_FILE}.state")
  87. if(EXISTS "${STATE_FILE}")
  88. file(READ "${STATE_FILE}" __prev_state)
  89. else()
  90. set(__prev_state "")
  91. endif()
  92. if(NOT "${__state}" STREQUAL "${__prev_state}")
  93. file(WRITE "${STATE_FILE}" "${__state}")
  94. message("${prefix}Updated!")
  95. set(update_dephelper 1)
  96. endif()
  97. if(NOT update_dephelper)
  98. message("${prefix}All files are up-to-date.")
  99. elseif(DEFINED DEPHELPER)
  100. file(WRITE "${DEPHELPER}" "")
  101. endif()