copy_files.cmake 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
include("${CONFIG_FILE}")
set(prefix "COPYFILES: ")

set(use_symlink 0)
if(IS_SYMLINK "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/symlink_test")
  set(use_symlink 1)
endif()

set(update_dephelper 0)
if(DEFINED DEPHELPER AND NOT EXISTS "${DEPHELPER}")
  set(update_dephelper 1)
endif()

set(__state "")

macro(copy_file_ src dst prefix)
  string(REPLACE "${CMAKE_BINARY_DIR}/" "" dst_name "${dst}")
  set(local_update 0)
  if(NOT EXISTS "${dst}")
    set(local_update 1)
  endif()
  if(use_symlink)
    if(local_update OR NOT IS_SYMLINK "${dst}")
24
      #message("${prefix}Symlink: '${dst_name}' ...")
25 26 27
    endif()
    get_filename_component(target_path "${dst}" PATH)
    file(MAKE_DIRECTORY "${target_path}")
28
    execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${src}" "${dst}"
29 30 31 32 33 34 35 36 37 38 39 40
        RESULT_VARIABLE SYMLINK_RESULT)
    if(NOT SYMLINK_RESULT EQUAL 0)
      #message("Symlink failed, fallback to 'copy'")
      set(use_symlink 0)
    endif()
  endif()
  if(NOT use_symlink)
    if("${src}" IS_NEWER_THAN "${dst}" OR IS_SYMLINK "${dst}")
      file(REMOVE "${dst}") # configure_file(COPYONLY) doesn't update timestamp sometimes
      set(local_update 1)
    endif()
    if(local_update)
41
      #message("${prefix}Copying: '${dst_name}' ...")
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
      configure_file(${src} ${dst} COPYONLY)
    else()
      #message("${prefix}Up-to-date: '${dst_name}'")
    endif()
  endif()
  if(local_update)
    set(update_dephelper 1)
  endif()
  file(TIMESTAMP "${dst}" dst_t UTC)
  set(__state "${__state}${dst_t} ${dst}\n")
endmacro()

if(NOT DEFINED COPYLIST_VAR)
  set(COPYLIST_VAR "COPYLIST")
endif()
list(LENGTH ${COPYLIST_VAR} __length)
58
#message("${prefix}... ${__length} entries (${COPYLIST_VAR})")
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
foreach(id ${${COPYLIST_VAR}})
  set(src "${${COPYLIST_VAR}_SRC_${id}}")
  set(dst "${${COPYLIST_VAR}_DST_${id}}")
  if(NOT EXISTS ${src})
    message(FATAL_ERROR "Source file/dir is missing: ${src} (${CONFIG_FILE})")
  endif()
  set(_mode "COPYFILE")
  if(DEFINED ${COPYLIST_VAR}_MODE_${id})
    set(_mode "${${COPYLIST_VAR}_MODE_${id}}")
  endif()
  if(_mode STREQUAL "COPYFILE")
    #message("... COPY ${src} => ${dst}")
    copy_file_("${src}" "${dst}" "${prefix}    ")
  elseif(_mode STREQUAL "COPYDIR")
    get_filename_component(src_name "${src}" NAME)
    get_filename_component(src_path "${src}" PATH)
    get_filename_component(src_name2 "${src_path}" NAME)

    set(src_glob "${src}/*")
    if(DEFINED ${COPYLIST_VAR}_GLOB_${id})
      set(src_glob "${${COPYLIST_VAR}_GLOB_${id}}")
    endif()
    file(GLOB_RECURSE _files RELATIVE "${src}" ${src_glob})
    list(LENGTH _files __length)
83
    #message("${prefix}    ... directory '.../${src_name2}/${src_name}' with ${__length} files")
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    foreach(f ${_files})
      if(NOT EXISTS "${src}/${f}")
        message(FATAL_ERROR "COPY ERROR: Source file is missing: ${src}/${f}")
      endif()
      copy_file_("${src}/${f}" "${dst}/${f}" "${prefix}        ")
    endforeach()
  endif()
endforeach()

set(STATE_FILE "${CONFIG_FILE}.state")
if(EXISTS "${STATE_FILE}")
  file(READ "${STATE_FILE}" __prev_state)
else()
  set(__prev_state "")
endif()
if(NOT "${__state}" STREQUAL "${__prev_state}")
  file(WRITE "${STATE_FILE}" "${__state}")
101
  #message("${prefix}Updated!")
102 103 104 105
  set(update_dephelper 1)
endif()

if(NOT update_dephelper)
106
  #message("${prefix}All files are up-to-date.")
107 108 109
elseif(DEFINED DEPHELPER)
  file(WRITE "${DEPHELPER}" "")
endif()