From 7bd5d854d4614e28646c04e5aa008cd0fd249b58 Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Thu, 29 Sep 2016 22:40:05 +0200
Subject: [PATCH] [CMake] Moved packaging executable dependencies to package
 target.

Beforehand this logic was executed in a forced addional CMake run
everytime after the specific executable was built. Now this is done
at the moment this information is needed (at building the package or
install target).
---
 CHANGELOG.md                                  |  3 ++
 CMakeLists.txt                                |  1 +
 scripts/cmake/packaging/GetDependencies.cmake | 43 +++++++++++++++++++
 .../cmake/packaging/InstallDependencies.cmake | 35 +--------------
 scripts/cmake/packaging/Pack.cmake            | 11 +++++
 5 files changed, 60 insertions(+), 33 deletions(-)
 create mode 100644 scripts/cmake/packaging/GetDependencies.cmake

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 18f45ba213b..bcf02828921 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,6 +19,9 @@
 ### Infrastructure
  - Added a generic natural boundary condition class. #1337
  - Migrated all important Jenkins jobs to script-based [Jenkins Pipeline](https://jenkins.io/doc/pipeline/) functionality (#1392, #1396, #1404, #1411, #1424, #1428, #1436). For an introduction see [docs.opengeosys.org - Continuous Integration](https://docs.opengeosys.org/docs/devguide/development-workflows/continuous-integration)
+ - Moved CMake logic for packaging executable dependencies (such as shared libs)
+   to the install and package targets instead of running after each executable
+   gets build, #1458
 
 ### Fixes:
  - Fix an issue that a shape vector was defined as a column vector. Corrected to a row vector  #1288
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 12e691385c4..cf2982e9df3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -220,3 +220,4 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/BaseLib/BuildInfo.cpp.in"
     "${CMAKE_CURRENT_BINARY_DIR}/BaseLib/BuildInfo.cpp" @ONLY)
 
 include(scripts/cmake/MarkVariablesAdvanced.cmake)
+include(scripts/cmake/packaging/GetDependencies.cmake)
diff --git a/scripts/cmake/packaging/GetDependencies.cmake b/scripts/cmake/packaging/GetDependencies.cmake
new file mode 100644
index 00000000000..28d8162a878
--- /dev/null
+++ b/scripts/cmake/packaging/GetDependencies.cmake
@@ -0,0 +1,43 @@
+foreach(TARGET ${INSTALL_DEPENDENCIES})
+
+    if(MSVC AND NOT CMAKE_GENERATOR STREQUAL "Ninja" )
+        if(USE_CONAN)
+            set(TARGET_EXE ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_BUILD_TYPE}/${TARGET}${CMAKE_EXECUTABLE_SUFFIX})
+        else()
+            set(TARGET_EXE ${EXECUTABLE_OUTPUT_PATH}/Release/${TARGET}${CMAKE_EXECUTABLE_SUFFIX})
+        endif()
+    else()
+        set(TARGET_EXE ${EXECUTABLE_OUTPUT_PATH}/${TARGET}${CMAKE_EXECUTABLE_SUFFIX})
+    endif()
+
+    if(EXISTS ${TARGET_EXE})
+        include(GetPrerequisites)
+        # arg3: exclude system, arg4: recursive
+        if (VTK_BUILD_SHARED_LIBS)
+            list(APPEND dirs ${vtkIOXML_RUNTIME_LIBRARY_DIRS})
+        endif()
+        list(APPEND dirs "/usr/local/lib")
+        get_prerequisites(${TARGET_EXE} TARGET_DEPENDENCIES 1 1 "" ${dirs})
+        if(PRE_INSTALL_RUN)
+            message("-- Dependencies of target ${TARGET}:")
+        endif()
+        foreach(DEPENDENCY ${TARGET_DEPENDENCIES})
+            if(NOT ${DEPENDENCY} MATCHES "@loader_path")
+                gp_resolve_item("${TARGET_EXE}" "${DEPENDENCY}" "" "" DEPENDENCY_PATH)
+                get_filename_component(RESOLVED_DEPENDENCY_PATH "${DEPENDENCY_PATH}" REALPATH)
+                string(TOLOWER ${DEPENDENCY} DEPENDENCY_LOWER)
+                set(DEPENDENCY_PATHS ${DEPENDENCY_PATHS} ${RESOLVED_DEPENDENCY_PATH})
+                if(PRE_INSTALL_RUN)
+                    message("     ${RESOLVED_DEPENDENCY_PATH}")
+                endif()
+            endif()
+        endforeach()
+        if(PRE_INSTALL_RUN)
+            message("")
+        endif()
+        install(FILES ${DEPENDENCY_PATHS} DESTINATION bin COMPONENT ${INSTALL_COMPONENT})
+    endif()
+
+endforeach()
+
+unset(PRE_INSTALL_RUN CACHE)
diff --git a/scripts/cmake/packaging/InstallDependencies.cmake b/scripts/cmake/packaging/InstallDependencies.cmake
index 384162b3e00..0ee4c65deee 100644
--- a/scripts/cmake/packaging/InstallDependencies.cmake
+++ b/scripts/cmake/packaging/InstallDependencies.cmake
@@ -1,35 +1,4 @@
+# Registers a target for installing its dependencies (dll / so files)
 macro(InstallDependencies TARGET INSTALL_COMPONENT)
-
-    if(MSVC AND NOT CMAKE_GENERATOR STREQUAL "Ninja" )
-        set(TARGET_EXE ${EXECUTABLE_OUTPUT_PATH}/Release/${TARGET}${CMAKE_EXECUTABLE_SUFFIX})
-    else()
-        set(TARGET_EXE ${EXECUTABLE_OUTPUT_PATH}/${TARGET}${CMAKE_EXECUTABLE_SUFFIX})
-    endif()
-
-    if(EXISTS ${TARGET_EXE})
-        include(GetPrerequisites)
-        # arg3: exclude system, arg4: recursive
-        if (VTK_BUILD_SHARED_LIBS)
-            list(APPEND dirs ${vtkIOXML_RUNTIME_LIBRARY_DIRS})
-        endif()
-        list(APPEND dirs "/usr/local/lib")
-        get_prerequisites(${TARGET_EXE} TARGET_DEPENDENCIES 1 1 "" ${dirs})
-        message(STATUS "${TARGET_EXE} dependencies:")
-        foreach(DEPENDENCY ${TARGET_DEPENDENCIES})
-            if(NOT ${DEPENDENCY} MATCHES "@loader_path")
-                gp_resolve_item("${TARGET_EXE}" "${DEPENDENCY}" "" "" DEPENDENCY_PATH)
-                get_filename_component(RESOLVED_DEPENDENCY_PATH "${DEPENDENCY_PATH}" REALPATH)
-                string(TOLOWER ${DEPENDENCY} DEPENDENCY_LOWER)
-                set(DEPENDENCY_PATHS ${DEPENDENCY_PATHS} ${RESOLVED_DEPENDENCY_PATH})
-                message("    ${RESOLVED_DEPENDENCY_PATH}")
-            endif()
-        endforeach()
-        install(FILES ${DEPENDENCY_PATHS} DESTINATION bin COMPONENT ${INSTALL_COMPONENT})
-        add_custom_command(TARGET ${TARGET} POST_BUILD COMMAND ;)
-    else()
-        # Run CMake after target was built to run GetPrerequisites on executable
-        add_custom_command(TARGET ${TARGET} POST_BUILD COMMAND ${CMAKE_COMMAND}
-            ARGS ${CMAKE_SOURCE_DIR} WORKING_DIRECTORY ${CMAKE_BINARY_DIR} VERBATIM)
-    endif()
-
+    set(INSTALL_DEPENDENCIES "${INSTALL_DEPENDENCIES};${TARGET}" CACHE INTERNAL "")
 endmacro()
diff --git a/scripts/cmake/packaging/Pack.cmake b/scripts/cmake/packaging/Pack.cmake
index 59cdc8c474a..767bffe6b2f 100644
--- a/scripts/cmake/packaging/Pack.cmake
+++ b/scripts/cmake/packaging/Pack.cmake
@@ -100,3 +100,14 @@ cpack_add_component(ogs_docs
     DESCRIPTION "PDF documentation."
     GROUP Utilities
 )
+
+# Clear cache variable holding all targets to install dependencies for.
+set(INSTALL_DEPENDENCIES "" CACHE INTERNAL "")
+
+# Rerun CMake on install
+install(CODE "\
+    message(STATUS \"Rerunning CMake for getting executable dependencies.\")\n \
+    execute_process(COMMAND ${CMAKE_COMMAND} \
+                    ARGS ${CMAKE_SOURCE_DIR} -DPRE_INSTALL_RUN=ON \
+                    WORKING_DIRECTORY ${CMAKE_BINARY_DIR} OUTPUT_QUIET)"
+)
-- 
GitLab