From bc01b8fbb35ef31f67a26a2340313ea7a7ab10d7 Mon Sep 17 00:00:00 2001
From: Ole Hansen <ole@jlab.org>
Date: Tue, 17 Jul 2018 21:22:00 -0400
Subject: [PATCH] CMake updates:

1. Update for ROOT5-enabled root_generate_dictionary CMake function

  root_generate_dictionary now works with both ROOT 5 and ROOT 6.
  It defines the dictionary custom target and PCM file internally,
  as appropriate, and also automatically extracts any required
  -D compile definitions from the given target(s).

2. Add configuration option HCANA_BUILTIN_PODD (default ON)

  If set to OFF (cmake -DHCANA_BUILTIN_PODD=OFF ...), CMake will find a
  pre-built version of Podd according to the usual find_package search
  rules. (It's sufficient to have the installed "analyzer" in your PATH,
  but you can also set CMAKE_PREFIX_PATH to the Podd installation root,
  for example.) This version of Podd has to have been built and
  installed with Podd's CMake system. In this case, only libHallC will
  be built, and the Podd submodule of hcana is ignored and does not even
  have to be present.

3. Clean existing src/hc_compiledata.h during CMake configuration

  This file might be left over from a previous non-CMake build and would
  be incorrectly picked up instead of the CMake-generated version.

4. Generate hcanaConfig.cmake config and version files using latest Podd scripts

  Requires Podd at commit JeffersonLab/analyzer@34dd368
---
 CMakeLists.txt             | 49 +++++++++++++++++++++-----------------
 cmake/CMakeLists.txt       |  3 +++
 cmake/hcanaConfig.cmake.in | 17 +++++++++++++
 src/CMakeLists.txt         | 18 ++++++--------
 4 files changed, 54 insertions(+), 33 deletions(-)
 create mode 100644 cmake/CMakeLists.txt
 create mode 100644 cmake/hcanaConfig.cmake.in

diff --git a/CMakeLists.txt b/CMakeLists.txt
index b875959..815533e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,22 +2,27 @@ cmake_minimum_required(VERSION 3.5)
 
 project(hcana VERSION 0.90 LANGUAGES CXX)
 
-#----------------------------------------------------------------------------
-# Use our modules and utility functions
-set(CMAKE_MODULE_PATH
-  ${PROJECT_SOURCE_DIR}/podd/cmake/Modules
-  ${CMAKE_MODULE_PATH}
-  )
-include(PoddCMakeEnv)
+option(HCANA_BUILTIN_PODD "Use built-in Podd submodule (default: YES)" ON)
 
 #----------------------------------------------------------------------------
-# Install in GNU-style directory layout
-include(GNUInstallDirs)
-set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME_LC})
+# Set up Podd and ROOT dependencies
+if(HCANA_BUILTIN_PODD)
+  set(CMAKE_MODULE_PATH
+    ${PROJECT_SOURCE_DIR}/podd/cmake/Modules
+    ${CMAKE_MODULE_PATH}
+    )
+  include(PoddCMakeEnv)
+  find_package(ROOT 5.10 REQUIRED)
+else()
+  # Find Podd and register it as a dependency
+  # This will also automatically set up ROOT
+  find_package(Podd 1.6 REQUIRED)
+  include(PoddCMakeEnv)
+  config_add_dependency(Podd 1.6)
+endif()
 
-#----------------------------------------------------------------------------
-# Find ROOT (using Podd's FindROOT.cmake)
-find_package(ROOT 5.10 MODULE REQUIRED)
+# Register ROOT dependency - it's in our public interface
+config_add_dependency(ROOT 5.10)
 
 #----------------------------------------------------------------------------
 # Set up the compiler flags
@@ -26,14 +31,14 @@ set_diagnostic_flags(WALL WEXTRA)
 report_build_info()
 
 #----------------------------------------------------------------------------
-# Directories in which we build things
-add_subdirectory(podd)
-add_subdirectory(src)
+# Install in GNU-style directory layout
+include(GNUInstallDirs)
+set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/${MAIN_PROJECT_NAME_LC})
 
 #----------------------------------------------------------------------------
-# Install and export targets defined in the subdirectories
-install(EXPORT ${PROJECT_NAME_LC}-exports
- FILE ${PROJECT_NAME}Targets.cmake
- NAMESPACE ${PROJECT_NAME}::
- DESTINATION ${INSTALL_CONFIGDIR}
- )
+# Directories in which we build things
+if(HCANA_BUILTIN_PODD)
+  add_subdirectory(podd)
+endif()
+add_subdirectory(src)
+add_subdirectory(cmake)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
new file mode 100644
index 0000000..f051a74
--- /dev/null
+++ b/cmake/CMakeLists.txt
@@ -0,0 +1,3 @@
+# Create <Project>Config.cmake and <Project>ConfigVersion.cmake
+
+include(WriteProjectConfig)
diff --git a/cmake/hcanaConfig.cmake.in b/cmake/hcanaConfig.cmake.in
new file mode 100644
index 0000000..e476f96
--- /dev/null
+++ b/cmake/hcanaConfig.cmake.in
@@ -0,0 +1,17 @@
+@PACKAGE_INIT@
+
+message(STATUS "Found @PROJECT_NAME@: @PACKAGE_CMAKE_INSTALL_PREFIX@ (found version @PROJECT_VERSION@@EXTVERS@)")
+
+set_and_check(@PROJECT_NAME_UC@_DIR "@PACKAGE_CMAKE_INSTALL_PREFIX@")
+set_and_check(@PROJECT_NAME_UC@_INCLUDE_DIR "@PACKAGE_CMAKE_INSTALL_INCLUDEDIR@")
+
+if(IS_DIRECTORY "@PACKAGE_INSTALL_CONFIGDIR@/Modules")
+  list(APPEND CMAKE_MODULE_PATH "@PACKAGE_INSTALL_CONFIGDIR@/Modules")
+endif()
+
+@FIND_DEPENDENCY_COMMANDS@
+
+include("@PACKAGE_TARGETS_FILE@")
+
+check_required_components(@PROJECT_NAME_UC@)
+
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index a37c284..151419e 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -40,8 +40,12 @@ file(APPEND "${linkdef}" "${postamble}")
 unset(postamble)
 
 #----------------------------------------------------------------------------
-# Generate ha_compiledata.h
+# Generate hc_compiledata.h
 include(PoddCompileInfo)
+
+# Ensure there's no old hc_compiledata.h hanging around from a non-CMake build
+file(REMOVE "${CMAKE_CURRENT_SOURCE_DIR}/hc_compiledata.h")
+
 configure_file(hc_compiledata.h.in hc_compiledata.h)
 list(APPEND allheaders "${CMAKE_CURRENT_BINARY_DIR}/hc_compiledata.h")
 
@@ -79,7 +83,7 @@ set_target_properties(${LIBNAME} PROPERTIES
   )
 
 install(TARGETS ${LIBNAME}
-  EXPORT ${PROJECT_NAME_LC}-exports
+  EXPORT ${MAIN_PROJECT_NAME_LC}-exports
   LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
   ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
   RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
@@ -88,18 +92,10 @@ install(FILES ${allheaders} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
 
 #----------------------------------------------------------------------------
 # libHallCDict
-get_target_definitions(${LIBNAME} defines)
-
-root_generate_dictionary(${LIBNAME} ${defines} ${headers}
+build_root_dictionary(${LIBNAME} ${headers}
   TARGETS ${LIBNAME}
   LINKDEF ${linkdef}
   )
-set(PCM_FILE ${CMAKE_CURRENT_BINARY_DIR}/lib${LIBNAME}_rdict.pcm)
-
-add_custom_target(${LIBNAME}_ROOTDICT
-  DEPENDS ${src} ${headers} ${LIBNAME}_LinkDef.h ${LIBNAME}Dict.cxx)
-
-install(FILES ${PCM_FILE} DESTINATION ${CMAKE_INSTALL_LIBDIR})
 
 #----------------------------------------------------------------------------
 # hcana executable
-- 
GitLab