packaging: create valid bundle and dmg

We use BundleUtilities to fix embedded deps, and CPack to create
the DragNDrop final .dmg

Refs #69161

Change-Id: I082f94b7cbf8a42e74bd3d61abf56c631a78acb9
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5475cb0..aa1af2d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -5,9 +5,9 @@
 ENDIF(POLICY CMP0022)
 
 SET(PROJ_NAME Ring)
-SET(RING_VERSION "0.1.x")
+SET(RING_VERSION "0.1.0")
 SET(RING_VERSION_NAME "Samuel de Champlain")
-SET(BUNDLE_VERSION "Samuel de Champlain (0.1.x")
+SET(BUNDLE_VERSION "Samuel de Champlain (0.1.0")
 
 SET(PROJ_COPYRIGHT " © 2015 Savoir-faire Linux \n GPLv3 https://www.gnu.org/copyleft/gpl.html")
 
@@ -174,3 +174,46 @@
                       COMMENT "Compiling ${CMAKE_CURRENT_SOURCE_DIR}/${xib}.xib")
 
 ENDFOREACH()
+
+set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} ${CMAKE_INSTALL_PREFIX})
+
+SET(APPS "\${CMAKE_INSTALL_PREFIX}/${PROJ_NAME}.app")
+
+#--------------------------------------------------------------------------------
+# Install the QtTest application, on Apple, the bundle is at the root of the
+# install tree
+INSTALL(TARGETS ${PROJ_NAME} BUNDLE DESTINATION . COMPONENT Runtime)
+
+SET(QT_PLUGINS_DESTDIR ${PROJ_NAME}.app/Contents/Plugins/platforms)
+#--------------------------------------------------------------------------------
+# Install needed Qt plugins by copying directories from the qt installation
+LIST(APPEND QT_PLUGINS Qt5::QTgaPlugin Qt5::QTiffPlugin Qt5::QCocoaIntegrationPlugin)
+FOREACH(plugin ${QT_PLUGINS})
+  GET_TARGET_PROPERTY(_loc ${plugin} LOCATION)
+  INSTALL(FILES ${_loc} DESTINATION ${QT_PLUGINS_DESTDIR} COMPONENT Runtime)
+ENDFOREACH()
+
+# directories to look for dependencies
+SET(DIRS ${CMAKE_INSTALL_PREFIX}/lib ${QT_LIB_DIR})
+
+INSTALL(CODE "
+    file(GLOB_RECURSE QTPLUGINS
+      \"\${CMAKE_INSTALL_PREFIX}/${QT_PLUGINS_DESTDIR}/*${CMAKE_SHARED_LIBRARY_SUFFIX}\")
+    include(BundleUtilities)
+    SET(BU_CHMOD_BUNDLE_ITEMS TRUE)
+    fixup_bundle(\"${APPS}\" \"\${QTPLUGINS}\" \"${DIRS}\")
+    " COMPONENT Runtime)
+
+#================================
+# Packaging
+#================================
+SET( CPACK_PACKAGE_DESCRIPTION_SUMMARY ${PROJ_NAME})
+SET( CPACK_PACKAGE_NAME ${PROJ_NAME} )
+SET( CPACK_PACKAGE_CONTACT "Alexandre Lision")
+SET( CPACK_PACKAGE_VENDOR "Savoir-faire Linux")
+SET( CPACK_PACKAGE_VERSION_MAJOR ${PROG_MAJOR_VERSION})
+SET( CPACK_PACKAGE_VERSION_MINOR ${PROG_MINOR_VERSION})
+SET( CPACK_PACKAGE_VERSION_PATCH ${PROG_PATCH_VERSION})
+SET(CPACK_BINARY_DRAGNDROP ON)
+SET( CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}")
+INCLUDE(CPack)
diff --git a/main.mm b/main.mm
index b97e612..c1d8676 100644
--- a/main.mm
+++ b/main.mm
@@ -29,9 +29,17 @@
  */
 #import <AppKit/NSApplication.h> // NSApplicationMain
 #import <qapplication.h>
+#import <QDebug>
+#import <QDir>
 
 int main(int argc, const char *argv[]) {
 
+    QDir dir(QString::fromUtf8(argv[0]));
+    dir.cdUp();
+    dir.cdUp();
+    dir.cd("Plugins");
+    QCoreApplication::addLibraryPath(dir.absolutePath());
+    qDebug() << "" << QCoreApplication::libraryPaths();
     //Qt event loop will override native event loop
     QApplication* app = new QApplication(argc, const_cast<char**>(argv));
     app->setAttribute(Qt::AA_MacPluginApplication);