From 2c567db3e7a4bb34b4f6ba826eb1b7f6239ee6eb Mon Sep 17 00:00:00 2001
From: Attila Krasznahorkay <Attila.Krasznahorkay@cern.ch>
Date: Thu, 12 Mar 2020 14:46:08 +0100
Subject: [PATCH] Taught the project how to build XercesC on its own if
 necessary.

In the same style in which GeoModelCore can optionally download
Eigen for itself.
---
 CMakeLists.txt           |  4 ++-
 XMLParser/CMakeLists.txt |  3 ++
 cmake/SetupXercesC.cmake | 65 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+), 1 deletion(-)
 create mode 100644 cmake/SetupXercesC.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d91498a..998c212 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -15,11 +15,13 @@ list( APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake )
 # Use the GNU install directory names.
 include( GNUInstallDirs )
 
+# Set up the "optional" dependencies.
+include( SetupXercesC )
+
 # Find the externals needed by the project.
 find_package( GeoModelCore REQUIRED )
 find_package( GeoModelIO REQUIRED )
 find_package( nlohmann_json )
-find_package( XercesC REQUIRED )
 
 # Set up the build of the libraries of the project.
 add_subdirectory( XMLParser )
diff --git a/XMLParser/CMakeLists.txt b/XMLParser/CMakeLists.txt
index a52cd3a..f5f10c3 100644
--- a/XMLParser/CMakeLists.txt
+++ b/XMLParser/CMakeLists.txt
@@ -16,6 +16,9 @@ source_group( "src" FILES ${SOURCES} )
 set_target_properties( XMLParser PROPERTIES
    VERSION ${PROJECT_VERSION}
    SOVERSION ${PROJECT_VERSION_MAJOR} )
+if( GEOMODEL_USE_BUILTIN_XERCESC )
+   add_dependencies( XMLParser XercesC )
+endif()
 
 # Install the library.
 install( TARGETS XMLParser
diff --git a/cmake/SetupXercesC.cmake b/cmake/SetupXercesC.cmake
new file mode 100644
index 0000000..ba333b5
--- /dev/null
+++ b/cmake/SetupXercesC.cmake
@@ -0,0 +1,65 @@
+# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
+#
+# This module is used to set up XercesC for the project. Either by looking for
+# it on the build machine, or by downloading it during the build itself.
+#
+
+# Configuration option for how XercesC should be used.
+option( GEOMODEL_USE_BUILTIN_XERCESC
+   "Download/build a version of XercesC during the build" FALSE )
+
+# Now do what was requested.
+if( GEOMODEL_USE_BUILTIN_XERCESC )
+
+   # Tell the user what's happening.
+   message( STATUS "Building XercesC as part of the project" )
+
+   # The include directory and library that will be produced.
+   set( XercesC_INCLUDE_DIR
+      "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/XercesCInstall/${CMAKE_INSTALL_INCLUDEDIR}" )
+   set( XercesC_INCLUDE_DIRS "${XercesC_INCLUDE_DIR}" )
+   set( XercesC_LIBRARY
+      "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/XercesCInstall/${CMAKE_INSTALL_LIBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}xerces-c${CMAKE_SHARED_LIBRARY_SUFFIX}" )
+   set( XercesC_LIBRARIES "${XercesC_LIBRARY}" )
+
+   file( MAKE_DIRECTORY "${XercesC_INCLUDE_DIR}" )
+
+   # Build/install Eigen3 using ExternalProject_Add(...).
+   include( ExternalProject )
+   ExternalProject_Add( XercesC
+      PREFIX "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/XercesCBuild"
+      INSTALL_DIR "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/XercesCInstall"
+      URL "https://cern.ch/lcgpackages/tarFiles/sources/xerces-c-3.1.3.tar.gz"
+      URL_MD5 "70320ab0e3269e47d978a6ca0c0e1e2d"
+      CONFIGURE_COMMAND
+      ${CMAKE_COMMAND} -E env CXXFLAGS=-std=c++${CMAKE_CXX_STANDARD}
+      <SOURCE_DIR>/configure --disable-static --prefix=<INSTALL_DIR>
+      INSTALL_COMMAND make install
+      COMMAND ${CMAKE_COMMAND} -E remove -f
+      "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/XercesCInstall/${CMAKE_INSTALL_LIBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}xerces-c.la"
+      COMMAND ${CMAKE_COMMAND} -E remove_directory
+      "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/XercesCInstall/${CMAKE_INSTALL_LIBDIR}/pkgconfig"
+      BUILD_BYPRODUCTS "${XercesC_INCLUDE_DIR}" "${XercesC_LIBRARY}" )
+   install( DIRECTORY
+      "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/XercesCInstall/${CMAKE_INSTALL_BINDIR}"
+      "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/XercesCInstall/${CMAKE_INSTALL_LIBDIR}"
+      DESTINATION .
+      COMPONENT Runtime
+      USE_SOURCE_PERMISSIONS )
+   install( DIRECTORY "${XercesC_INCLUDE_DIR}"
+      DESTINATION .
+      COMPONENT Development
+      USE_SOURCE_PERMISSIONS )
+
+   # Set up XercesC's imported target.
+   add_library( XercesC::XercesC UNKNOWN IMPORTED )
+   set_target_properties( XercesC::XercesC PROPERTIES
+      INTERFACE_INCLUDE_DIRECTORIES "${XercesC_INCLUDE_DIR}"
+      IMPORTED_LOCATION "${XercesC_LIBRARY}" )
+
+else()
+
+   # Just find an existing installation of XercesC.
+   find_package( XercesC REQUIRED )
+
+endif()
-- 
GitLab