From 0e9d84e36c508f8a2a12b20e2d92ee9d48d62e87 Mon Sep 17 00:00:00 2001
From: Riccardo Maria Bianchi <riccardo.maria.bianchi@cern.ch>
Date: Tue, 30 Jun 2020 10:42:18 +0200
Subject: [PATCH] Simplifying and centralizing CMake setup for external
 dependencies

---
 CMakeLists.txt                                     |  6 ++++--
 GeoModelTools/CMakeLists.txt                       | 11 -----------
 GeoModelTools/GeoModelJSONParser/CMakeLists.txt    | 10 +---------
 .../GXClashPointSystems/CMakeLists.txt             | 10 +---------
 .../VP1GeometrySystems/CMakeLists.txt              | 14 +++-----------
 .../{CMakeLists.txt => Dependencies.cmake}         |  0
 dependencies/SetupJSON.cmake                       |  2 +-
 7 files changed, 10 insertions(+), 43 deletions(-)
 rename dependencies/{CMakeLists.txt => Dependencies.cmake} (100%)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8cc6c44d1..1d59c11d2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,7 +7,7 @@ project( "GeoModel" VERSION 1.0.0 LANGUAGES CXX )
 
 
 # === Project wide setup ===
-# Make the module directory visible to CMake.
+# Make the 'cmake' module directory visible to CMake.
 list( APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake )
 # Define color codes for CMake messages
 include( cmake_colors_defs )
@@ -21,7 +21,9 @@ include( BuildType )
 include( PrintBuildInfo )
 
 # === Externally provided content ===
-add_subdirectory(dependencies)
+# Make the 'dependencies' module directory visible to CMake.
+list( APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/dependencies )
+include(Dependencies)
 
 # === Main targets built by this project ===
 
diff --git a/GeoModelTools/CMakeLists.txt b/GeoModelTools/CMakeLists.txt
index 99ce7fba3..659797c1c 100644
--- a/GeoModelTools/CMakeLists.txt
+++ b/GeoModelTools/CMakeLists.txt
@@ -1,7 +1,6 @@
 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
 
 # Set up the project.
-# cmake_minimum_required( VERSION 3.1 )
 project( "GeoModelTools" VERSION 4.0.0 LANGUAGES CXX )
 
 # Set default build options.
@@ -12,16 +11,6 @@ set( CMAKE_CXX_EXTENSIONS FALSE CACHE BOOL "(Dis)allow using GNU extensions" )
 # Make the module directory visible to CMake.
 list( APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake )
 #
-# # Use the GNU install directory names.
-# include( GNUInstallDirs )
-#
-# # Set up the "optional" dependencies.
-# include( SetupXercesC )
-# include( SetupJSON )
-#
-# # Find the externals needed by the project.
-# find_package( GeoModelCore 3.2.0 REQUIRED )
-# find_package( GeoModelIO 3.2.0 REQUIRED )
 find_package( XercesC )
 
 # Set up the build of the libraries of the project.
diff --git a/GeoModelTools/GeoModelJSONParser/CMakeLists.txt b/GeoModelTools/GeoModelJSONParser/CMakeLists.txt
index 479bc0de1..140b8418f 100644
--- a/GeoModelTools/GeoModelJSONParser/CMakeLists.txt
+++ b/GeoModelTools/GeoModelJSONParser/CMakeLists.txt
@@ -22,15 +22,7 @@ endif()
 # installed with Homebrew on macOS).
 # This is not needed if the single-header library is installed in a regular
 # system include folder (e.g., '/usr/local/include', '/usr/include', ...)
-# Also, note that nlohmann_json has been found (or not) at the root level already,
-# but apparently it needs to be "found" here again,
-# otherwise the nlohmann_json CMake config file is not found properly.
-# Thus, in case SetupJSON.cmake finds it at root level, the variable
-# SetupJSON_nlohmann_json_FOUND is set to true, and here we "find" nlohmann_json
-# again. (TODO: to be investigated if we can simplify all that: ideally,
-# finding it at root level inside SetupJSON.cmake should be enough.)
-if ( nlohmann_json_FOUND OR SetupJSON_nlohmann_json_FOUND )
-  find_package(nlohmann_json QUIET)
+if ( nlohmann_json_FOUND )
   target_link_libraries( GeoModelJSONParser PUBLIC nlohmann_json::nlohmann_json )
 endif()
 
diff --git a/GeoModelVisualization/GXClashPointSystems/CMakeLists.txt b/GeoModelVisualization/GXClashPointSystems/CMakeLists.txt
index cf1ddf5d3..ad1ee5450 100644
--- a/GeoModelVisualization/GXClashPointSystems/CMakeLists.txt
+++ b/GeoModelVisualization/GXClashPointSystems/CMakeLists.txt
@@ -71,15 +71,7 @@ target_link_libraries (GXClashPointSystems PRIVATE SoQt::SoQt Coin::Coin  ${Qt5C
 # installed with Homebrew on macOS).
 # This is not needed if the single-header library is installed in a regular
 # system include folder (e.g., '/usr/local/include', '/usr/include', ...)
-# Also, note that nlohmann_json has been found (or not) at the root level already,
-# but apparently it needs to be "found" here again,
-# otherwise the nlohmann_json CMake config file is not found properly.
-# Thus, in case SetupJSON.cmake finds it at root level, the variable
-# SetupJSON_nlohmann_json_FOUND is set to true, and here we "find" nlohmann_json
-# again. (TODO: to be investigated if we can simplify all that: ideally,
-# finding it at root level inside SetupJSON.cmake should be enough.)
-if ( nlohmann_json_FOUND OR SetupJSON_nlohmann_json_FOUND )
-  find_package(nlohmann_json QUIET)
+if ( nlohmann_json_FOUND )
   target_link_libraries( GXClashPointSystems PUBLIC nlohmann_json::nlohmann_json )
 endif()
 
diff --git a/GeoModelVisualization/VP1GeometrySystems/CMakeLists.txt b/GeoModelVisualization/VP1GeometrySystems/CMakeLists.txt
index 4873890c3..50d638538 100644
--- a/GeoModelVisualization/VP1GeometrySystems/CMakeLists.txt
+++ b/GeoModelVisualization/VP1GeometrySystems/CMakeLists.txt
@@ -67,15 +67,7 @@ include_directories(${VP1HEPVis_INCLUDE_DIRS} )
 # installed with Homebrew on macOS).
 # This is not needed if the single-header library is installed in a regular
 # system include folder (e.g., '/usr/local/include', '/usr/include', ...)
-# Also, note that nlohmann_json has been found (or not) at the root level already,
-# but apparently it needs to be "found" here again,
-# otherwise the nlohmann_json CMake config file is not found properly.
-# Thus, in case SetupJSON.cmake finds it at root level, the variable
-# SetupJSON_nlohmann_json_FOUND is set to true, and here we "find" nlohmann_json
-# again. (TODO: to be investigated if we can simplify all that: ideally,
-# finding it at root level inside SetupJSON.cmake should be enough.)
-if ( nlohmann_json_FOUND OR SetupJSON_nlohmann_json_FOUND )
-  find_package(nlohmann_json QUIET)
+if ( nlohmann_json_FOUND )
   target_link_libraries( GXGeometrySystems nlohmann_json::nlohmann_json )
 endif()
 
@@ -89,8 +81,8 @@ endif()
 
 # Set a variable storing the CMake install prefix.
 # -- It is passed to the C++ preprocessor to load, from the C++ code,
-# -- the JSON files storing the default materials and volume attributes
-# -- for Coin3D.
+#    the JSON files storing the default materials and volume attributes
+#    for Coin3D.
 add_definitions ( -DGEOMODEL_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX})
 
 
diff --git a/dependencies/CMakeLists.txt b/dependencies/Dependencies.cmake
similarity index 100%
rename from dependencies/CMakeLists.txt
rename to dependencies/Dependencies.cmake
diff --git a/dependencies/SetupJSON.cmake b/dependencies/SetupJSON.cmake
index bf72b2f4e..bc7d30911 100644
--- a/dependencies/SetupJSON.cmake
+++ b/dependencies/SetupJSON.cmake
@@ -59,7 +59,7 @@ else()
      message(STATUS "SetupJSON - WARNING! 'nlohmann_json' was not found by CMake!! However, if you installed this single-header library in a standard system include dir (e.g., '/usr/local/include'), I will be able to use it.")
    else()
      message(STATUS "SetupJSON - Found 'nlohmann_json' at: ${nlohmann_json_DIR}")
-     set( SetupJSON_nlohmann_json_FOUND TRUE CACHE BOOL "True if nlohmann_json has been found by the SetupJSON module" FORCE )
+     # set( SetupJSON_nlohmann_json_FOUND TRUE CACHE BOOL "True if nlohmann_json has been found by the SetupJSON module" FORCE )
    endif()
 
 endif()
-- 
GitLab