Blob Blame History Raw
From aaf0fed5dccb53333ff2706be608be9c327707ea Mon Sep 17 00:00:00 2001
From: Mark Abraham <mark.j.abraham@gmail.com>
Date: Thu, 12 May 2016 17:39:18 +0200
Subject: [PATCH] Re-add support for linking against external TinyXML-2

I always intended this support, to permit convenient packaging of
GROMACS by distributions, but it got lost from gerrit while rebasing
from patch set 4 to 5 of I6153136.

Fixes #1956

Change-Id: Ie76dc9e8c6116814439d813d5a9555c5bfb7bfc5
---

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 14f091b..2e64a6a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -512,6 +512,17 @@
     endif()
 endif()
 
+option(GMX_EXTERNAL_TINYXML2 "Use external TinyXML-2 instead of compiling the version bundled with GROMACS." OFF)
+mark_as_advanced(GMX_EXTERNAL_TINYXML2)
+if(GMX_EXTERNAL_TINYXML2)
+    # Find an external TinyXML-2 library.
+    find_package(TinyXML-2 3.0.0)
+    set(HAVE_TINYXML2 ${TinyXML2_FOUND})
+    if(NOT HAVE_TINYXML2)
+        message(FATAL_ERROR "External TinyXML-2 could not be found, please adjust your search paths")
+    endif()
+endif()
+
 option(GMX_EXTRAE "Add support for tracing using EXTRAE" OFF)
 mark_as_advanced(GMX_EXTRAE)
 
diff --git a/cmake/FindTinyXML-2.cmake b/cmake/FindTinyXML-2.cmake
new file mode 100644
index 0000000..3bea516
--- /dev/null
+++ b/cmake/FindTinyXML-2.cmake
@@ -0,0 +1,89 @@
+#
+# This file is part of the GROMACS molecular simulation package.
+#
+# Copyright (c) 2016, by the GROMACS development team, led by
+# Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+# and including many others, as listed in the AUTHORS file in the
+# top-level source directory and at http://www.gromacs.org.
+#
+# GROMACS is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public License
+# as published by the Free Software Foundation; either version 2.1
+# of the License, or (at your option) any later version.
+#
+# GROMACS is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with GROMACS; if not, see
+# http://www.gnu.org/licenses, or write to the Free Software Foundation,
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
+#
+# If you want to redistribute modifications to GROMACS, please
+# consider that scientific software is very special. Version
+# control is crucial - bugs must be traceable. We will be happy to
+# consider code for inclusion in the official distribution, but
+# derived work must not be called official GROMACS. Details are found
+# in the README & COPYING files - if they are missing, get the
+# official version at http://www.gromacs.org.
+#
+# To help us fund GROMACS development, we humbly ask that you cite
+# the research papers on the package. Check out http://www.gromacs.org.
+
+# This package tries to find the TinyXML-2 library. It will consider
+# the directory ${TinyXML2_DIR} in its search. Upon exit, the
+# following variables may be set:
+#
+# TinyXML2_FOUND       - TinyXML-2 was found
+# TinyXML2_INCLUDE_DIR - TinyXML-2 include directory
+# TinyXML2_LIBRARIES   - TinyXML-2 libraries
+# TinyXML2_LINKS_OK    - TinyXML-2 libraries link correctly
+# TinyXML2_VERSION     - TinyXML-2 version string as "major.minor"
+
+find_package(PkgConfig QUIET)
+if(PKG_CONFIG_FOUND)
+    if(TinyXML2_FIND_VERSION)
+        if(TinyXML2_FIND_VERSION_EXACT)
+            pkg_check_modules(PC_TINYXML2 QUIET tinyxml2=${TinyXML2_FIND_VERSION})
+        else()
+            pkg_check_modules(PC_TINYXML2 QUIET tinyxml2>=${TinyXML2_FIND_VERSION})
+        endif()
+    else()
+        pkg_check_modules(PC_TINYXML2 QUIET tinyxml2)
+    endif()
+endif()
+
+# Try to find tinyxml2, perhaps with help from pkg-config
+find_path(TinyXML2_INCLUDE_DIR tinyxml2.h PATHS "${TinyXML2_DIR}" "${PC_TINYXML2_INCLUDE_DIRS}" PATH_SUFFIXES include)
+find_library(TinyXML2_LIBRARIES NAMES tinyxml2 PATHS "${TinyXML2_DIR}" "${PC_TINYXML2_LIBRARIES}" PATH_SUFFIXES lib64 lib)
+
+if(TinyXML2_INCLUDE_DIR AND EXISTS "${TinyXML2_INCLUDE_DIR}/tinyxml2.h" AND TinyXML2_LIBRARIES)
+    file(STRINGS "${TinyXML2_INCLUDE_DIR}/tinyxml2.h" _TinyXML2_H_MAJOR REGEX "TIXML2_MAJOR_VERSION = [0-9]+;")
+    file(STRINGS "${TinyXML2_INCLUDE_DIR}/tinyxml2.h" _TinyXML2_H_MINOR REGEX "TIXML2_MINOR_VERSION = [0-9]+;")
+    string(REGEX REPLACE "TIXML2_MAJOR_VERSION = ([0-9]+);" "\\1" _TinyXML2_MAJOR_VERSION "${_TinyXML2_H_MAJOR}")
+    string(REGEX REPLACE "TIXML2_MINOR_VERSION = ([0-9]+);" "\\1" _TinyXML2_MINOR_VERSION "${_TinyXML2_H_MINOR}")
+    set(TinyXML2_VERSION "${_TinyXML2_MAJOR_VERSION}.${_TinyXML2_MINOR_VERSION}")
+endif()
+
+# Make sure we can also link, so that cross-compilation is properly supported
+if (TinyXML2_INCLUDE_DIR AND TinyXML2_LIBRARIES)
+    include(CheckCXXSourceCompiles)
+    set(CMAKE_REQUIRED_INCLUDES ${TinyXML2_INCLUDE_DIR})
+    set(CMAKE_REQUIRED_LIBRARIES ${TinyXML2_LIBRARIES})
+    check_cxx_source_compiles("#include <tinyxml2.h>\nint main(){tinyxml2::XMLDocument doc;}" TinyXML2_LINKS_OK)
+endif()
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(TinyXML2
+    FOUND_VAR
+    TinyXML2_FOUND
+    REQUIRED_VARS
+    TinyXML2_INCLUDE_DIR
+    TinyXML2_LIBRARIES
+    TinyXML2_LINKS_OK
+    VERSION_VAR
+    TinyXML2_VERSION)
+
+mark_as_advanced(TinyXML2_INCLUDE_DIR TinyXML2_LIBRARIES)
diff --git a/src/testutils/CMakeLists.txt b/src/testutils/CMakeLists.txt
index eb0fa8d..2bd34f4 100644
--- a/src/testutils/CMakeLists.txt
+++ b/src/testutils/CMakeLists.txt
@@ -48,8 +48,11 @@
     testoptions.cpp
     textblockmatchers.cpp
     xvgtest.cpp
-    ../external/tinyxml2/tinyxml2.cpp
     )
+
+if(NOT HAVE_TINYXML2)
+    list(APPEND TESTUTILS_SOURCES ../external/tinyxml2/tinyxml2.cpp)
+endif()
 
 add_library(testutils STATIC ${UNITTEST_TARGET_OPTIONS} ${TESTUTILS_SOURCES})
 set(TESTUTILS_LIBS testutils)
@@ -57,6 +60,13 @@
 set_property(TARGET testutils APPEND PROPERTY COMPILE_FLAGS "${GMOCK_COMPILE_FLAGS}")
 target_link_libraries(testutils libgromacs ${GMOCK_LIBRARIES})
 
+if(HAVE_TINYXML2)
+    include_directories(SYSTEM ${TinyXML2_INCLUDE_DIR})
+    target_link_libraries(testutils ${TinyXML2_LIBRARIES})
+else()
+    include_directories(BEFORE SYSTEM "../external/tinyxml2")
+endif()
+
 set(TESTUTILS_DIR ${CMAKE_CURRENT_SOURCE_DIR})
 set(TESTUTILS_DIR ${TESTUTILS_DIR} PARENT_SCOPE)
 set(TESTUTILS_LIBS ${TESTUTILS_LIBS} PARENT_SCOPE)
diff --git a/src/testutils/refdata-xml.cpp b/src/testutils/refdata-xml.cpp
index ae2eadd..bdf6018 100644
--- a/src/testutils/refdata-xml.cpp
+++ b/src/testutils/refdata-xml.cpp
@@ -44,7 +44,7 @@
 
 #include "refdata-xml.h"
 
-#include "external/tinyxml2/tinyxml2.h"
+#include <tinyxml2.h>
 
 #include "gromacs/utility/exceptions.h"