CMake, OS X bundle, recursively copy directory to Resources

As was already mentioned, you can get a list of all of the files inside of a directory using file(GLOB_RECURSE RES_SOURCES "${SOURCE_ROOT}/data-folder/*"). Using SET_SOURCE_FILES_PROPERTIES(${RES_SOURCES} PROPERTIES MACOSX_PACKAGE_LOCATION Resources) sets the same location property for each file in the list ${RES_SOURCES} (putting all of them into a flat structure in Resources in this case).

This command doesn't take into consideration the structure of data-folder, if you would like to copy the files and directory structure you will have to specify it yourself:

#Get a list of all of the files in the data-folder
file(GLOB_RECURSE RES_SOURCES "${SOURCE_ROOT}/data-folder/*")

#Add our executable, and all of the files as "Source Files"
add_executable(${PROJECT_NAME} MACOSX_BUNDLE ${SRC} ${RES_SOURCES})

#individually set the file's path properties
foreach(RES_FILE ${RES_SOURCES})

  #Get the relative path from the data-folder to the particular file
  file(RELATIVE_PATH RES_PATH "${SOURCE_ROOT}/data-folder" ${RES_FILE})

  #Set it's location inside the app package (under Resources)
  set_property(SOURCE ${RES_FILE} PROPERTY MACOSX_PACKAGE_LOCATION "Resources/${RES_PATH}")

endforeach(RES_FILE)

This snippet will copy the contents of data-folder into the Resources folder in the app, preserving the structure like you would expect a copy command to do.


Use file(GLOB_RECURSE to collect a list of files before copying them.

file(GLOB_RECURSE RES_SOURCES "${SOURCE_ROOT}/data-folder/*")
add_executable(${PROJECT_NAME} MACOSX_BUNDLE ${SRC} ${RES_SOURCES})
SET_SOURCE_FILES_PROPERTIES(${RES_SOURCES} PROPERTIES MACOSX_PACKAGE_LOCATION Resources)

Tags:

Macos

Qt

Cmake