From d05ed0ce1216cc36b82f0418e7f7941b4c7e8ab8 Mon Sep 17 00:00:00 2001
From: Riccardo Maria Bianchi <riccardo.maria.bianchi@cern.ch>
Date: Wed, 1 Dec 2021 16:51:12 +0100
Subject: [PATCH] Add a target to CMake to generate graphs of dependencies

---
 CMakeLists.txt | 62 ++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 55 insertions(+), 7 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0484d1a71..40e069959 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -24,6 +24,7 @@ include( PrintBuildInfo )
 # Set default build and C++ options
 include( configure_cpp_options )
 
+
 # === Externally provided content ===
 
 # By default prefer not to use frameworks on macOS.
@@ -48,6 +49,7 @@ find_package( SQLite3 3.7.17 )
 
 # switches to let users build specific packages on request
 option(GEOMODEL_BUILD_ALL "Enable the build of all GeoModel sub-packages" OFF)
+option(GEOMODEL_BUILD_IO "Enable the build of GeoModelIO" ON)
 option(GEOMODEL_BUILD_TOOLS "Enable the build of GeoModelTools" OFF)
 option(GEOMODEL_BUILD_EXAMPLES "Enable the build of GeoModelExamples" OFF)
 option(GEOMODEL_BUILD_EXAMPLES_W_GEANT4 "Enable the build of GeoModelExamples, including the ones concerning the interface GeoModel->Geant4" OFF)
@@ -56,43 +58,52 @@ option(GEOMODEL_BUILD_GEOMODELG4 "Enable the build of GeoModelG4" OFF)
 option(GEOMODEL_BUILD_FULLSIMLIGHT "Enable the build of FullSimLight" OFF)
 option(GEOMODEL_BUILD_FULLSIMLIGHT_PROFILING "Enable FullSimLight profiling targets" OFF)
 
+# switches for miscellaneous options
+option(GEOMODEL_CREATE_GRAPH "Enable the build of the GeoModel dependencies graph" OFF)
+
+
+# === Set the build of the sub-packages ===
+
+# a list to keep track of the packages we build
+set(BUILT_PACKAGES "")
+
 if(GEOMODEL_BUILD_FULLSIMLIGHT_PROFILING)
   set(GEOMODEL_BUILD_FULLSIMLIGHT ON CACHE BOOL "Enable the build of FullSimLight" FORCE)
   include(CTest) # needs to be included at the top level
 endif()
 
-# a list to keep track of the packages we build
-set(BUILT_PACKAGES "")
-
+# GeoModelCore is built by default
 add_subdirectory(GeoModelCore)
 list( APPEND BUILT_PACKAGES "GeoModelCore")
-add_subdirectory(GeoModelIO)
-list( APPEND BUILT_PACKAGES "GeoModelIO")
-
 
 if(GEOMODEL_BUILD_ALL)
+    set(GEOMODEL_BUILD_IO            TRUE)
     set(GEOMODEL_BUILD_TOOLS         TRUE)
     set(GEOMODEL_BUILD_VISUALIZATION TRUE)
     set(GEOMODEL_BUILD_FULLSIMLIGHT  TRUE) 
 endif()
 
 if(GEOMODEL_BUILD_TOOLS)
+  set(GEOMODEL_BUILD_IO            TRUE)
   add_subdirectory(GeoModelTools)
   list( APPEND BUILT_PACKAGES "GeoModelTools")
 endif()
 
 if(GEOMODEL_BUILD_VISUALIZATION)
+  set(GEOMODEL_BUILD_IO            TRUE)
   add_subdirectory(GeoModelVisualization)
   list( APPEND BUILT_PACKAGES "GeoModelVisualization")
 endif()
 
 if(GEOMODEL_BUILD_EXAMPLES)
+  set(GEOMODEL_BUILD_IO            TRUE)
   add_subdirectory(GeoModelExamples)
   list( APPEND BUILT_PACKAGES "GeoModelExamples")
 endif()
 
 if(GEOMODEL_BUILD_FULLSIMLIGHT)
-  set(GEOMODEL_BUILD_GEOMODELG4 TRUE) # FullSimLight needs GeoModelG4
+  set(GEOMODEL_BUILD_IO            TRUE)
+  set(GEOMODEL_BUILD_GEOMODELG4    TRUE) # FullSimLight needs GeoModelG4
   add_subdirectory(FullSimLight)
   list( APPEND BUILT_PACKAGES "FullSimLight")
 endif()
@@ -102,6 +113,12 @@ if(GEOMODEL_BUILD_GEOMODELG4 OR GEOMODEL_BUILD_EXAMPLES_W_GEANT4)
   list( APPEND BUILT_PACKAGES "GeoModelG4")
 endif()
 
+if(GEOMODEL_BUILD_IO)
+    add_subdirectory(GeoModelIO)
+    list( APPEND BUILT_PACKAGES "GeoModelIO")
+endif()
+
+
 
 # A function to get a string with comma-separated package names from a list of packages
 # NOTE: We could make use of list(JOIN ...) on CMake >= 3.12,
@@ -127,3 +144,34 @@ getCSVStringFromList( "${BUILT_PACKAGES}" BUILT_PACKAGES_STR )
 message(STATUS "${BoldWhite}-----${ColourReset}")
 message( STATUS "${BoldGreen}Building the following ${BUILT_PACKAGES_LENGTH} packages: ${BUILT_PACKAGES_STR}${ColourReset}")
 message(STATUS "${BoldWhite}-----${ColourReset}")
+
+
+
+# generate the graph of dependencies, if requested
+# This is a two-step process:
+# 1) add the option '-DGEOMODEL_CREATE_GRAPH=1' to the cmake call
+# 2) run 'make graphs' to actually generate the .dot, .png, and .pdf files
+if(GEOMODEL_CREATE_GRAPH)
+    #set_property(GLOBAL PROPERTY GLOBAL_DEPENDS_DEBUG_MODE 1)
+    
+    # only dependencies between libraries
+    set(GRAPHVIZ_EXECUTABLES FALSE CACHE BOOL "" FORCE)
+    set(GRAPHVIZ_STATIC_LIBS FALSE CACHE BOOL "" FORCE)
+    set(GRAPHVIZ_SHARED_LIBS FALSE CACHE BOOL "" FORCE)
+    set(GRAPHVIZ_MODULE_LIBS FALSE CACHE BOOL "" FORCE)
+    set(GRAPHVIZ_INTERFACE_LIBS FALSE CACHE BOOL "" FORCE)
+    set(GRAPHVIZ_OBJECT_LIBS FALSE CACHE BOOL "" FORCE)
+    set(GRAPHVIZ_UNKWON_LIBS FALSE CACHE BOOL "" FORCE)
+    # do not show externals
+    set(GRAPHVIZ_EXTERNAL_LIBS FALSE CACHE BOOL "" FORCE)
+    set(GRAPHVIZ_CUSTOM_TARGETS FALSE CACHE BOOL "" FORCE)
+
+    add_custom_target(graphs ALL
+		COMMAND ${CMAKE_COMMAND} "--graphviz=geomodel_dependencies.dot" .
+		COMMAND dot -T png geomodel_dependencies.dot -o geomodel_dependencies.png
+		COMMAND dot -T pdf geomodel_dependencies.dot -o geomodel_dependencies.pdf
+		WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
+        COMMENT "Generating the graphviz .dot file, and the output .png and .pdf files"
+        )
+endif()
+
-- 
GitLab