macOS copy dependency to package

Hello there,
I have a small problem with c++ cmake on OSX.

I am writing an application that depends on Qt, Curl, and FFmpeg.

  • I am able to build the application
  • I am able to create a *.app directory
  • I am able to create a *dmg file vie DragNDrop.

At the moment I am trying to include the dependencies in the *.app file.
For this I woulde like to utilize the BundleUtilities.

But to be honest, I am not sureif this is the right way.

At the moment my code looks more ore lss like this:

install(TARGETS exe-filename
       DESTINATION .
       COMPONENT app)

get_filename_component(CURL_LIB_DIR "${CURL_LIBRARIES}" DIRECTORY)
list(APPEND DIRS ${CURL_LIB_DIR})
foreach(FFMPEG_SINGLE_LIB ${FFMPEG_LIBRARIES})
    get_filename_component(FFMPEG_LIB_DIR "${FFMPEG_SINGLE_LIB}" DIRECTORY)
    list(APPEND DIRS ${FFMPEG_LIB_DIR})
endforeach()
get_target_property(QT_CORE_LIB Qt5::Core LOCATION)
get_filename_component(QT_RUNTIME_DIR "${QT_CORE_LIB}" DIRECTORY)

list(REMOVE_DUPLICATES DIRS)

set(CPACK_DMG_BACKGROUND_IMAGE "${CMAKE_CURRENT_SOURCE_DIR}/res/icon_OSX/CMakeDMGBackground.tif")
set(CPACK_DMG_DS_STORE_SETUP_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/res/icon_OSX/Packaging_CMakeDMGSetup.scpt")
set(CPACK_GENERATOR DragNDrop)

include(CPack)

include(BundleUtilities)
set(MAIN_APP "exe-filename")
install(CODE "fixup_bundle(\"\${MAIN_APP}\" \"\" \"${DIRS}\")")

Is this the right way to achive my goal?
Best regards
Sebastian

The BundleUtilities module should be included at install time. See code in CMake itself as an example.

EDIT: The code in CMake itself refers to the BundleUtilities module in a way that works only inside its own build. Project code can use plain include(BundleUtilities) for that.