aboutsummaryrefslogtreecommitdiff
path: root/cmake/CopyFolderScript.cmake
blob: e725b68dcecd3da9fb4b64e14e198d8888f241dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Cross-platform recursive copy with exclusion support
#
# Required:
#   COPY_SOURCE   – source directory
#   COPY_DEST     – destination directory
#
# Optional:
#   EXCLUDE_FILES – pipe-separated file patterns to exclude
#   EXCLUDE_FOLDERS – pipe-separated folder patterns to exclude

if(NOT COPY_SOURCE OR NOT COPY_DEST)
  message(FATAL_ERROR "COPY_SOURCE and COPY_DEST must be set.")
endif()

# Replace "|" with ";" to convert the exclusion patterns back into a list
if(EXCLUDE_FILES)
  string(REPLACE "|" ";" EXCLUDE_FILES "${EXCLUDE_FILES}")
endif()

if(EXCLUDE_FOLDERS)
  string(REPLACE "|" ";" EXCLUDE_FOLDERS "${EXCLUDE_FOLDERS}")
endif()

message(STATUS "Copying from ${COPY_SOURCE} to ${COPY_DEST}")

if(CMAKE_HOST_WIN32)
  set(robocopy_args
    "${COPY_SOURCE}" "${COPY_DEST}"
    /S /MT /R:0 /W:0 /NP
  )

  if(EXCLUDE_FILES)
    list(APPEND robocopy_args /XF ${EXCLUDE_FILES})
  endif()

  if(EXCLUDE_FOLDERS)
    list(APPEND robocopy_args /XD ${EXCLUDE_FOLDERS})
  endif()

  execute_process(
    COMMAND robocopy.exe ${robocopy_args}
    RESULT_VARIABLE rc
  )

  if(rc GREATER 7) # Allows for "files copied" and "no files copied" cases, but treats actual errors as failures
    message(FATAL_ERROR "robocopy failed (exit code ${rc})")
  endif()
elseif(CMAKE_HOST_UNIX)
  set(rsync_args -av)

  foreach(pattern IN LISTS EXCLUDE_FILES)
    list(APPEND rsync_args "--exclude=${pattern}")
  endforeach()

  foreach(pattern IN LISTS EXCLUDE_FOLDERS)
    list(APPEND rsync_args "--exclude=${pattern}")
  endforeach()

  # Trailing slashes ensure rsync copies contents, not the directory itself
  execute_process(
    COMMAND rsync ${rsync_args} "${COPY_SOURCE}/" "${COPY_DEST}/"
    RESULT_VARIABLE rs
  )

  if(rs GREATER 0) # Any non-zero exit code indicates an error
    message(FATAL_ERROR "rsync failed (exit code ${rs})")
  endif()
else()
  message(FATAL_ERROR "Unsupported host platform for asset copying.")
endif()