Update libxml2 CMake detection
authorMark Abraham <mark.j.abraham@gmail.com>
Mon, 20 Jan 2014 18:57:19 +0000 (19:57 +0100)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Wed, 22 Jan 2014 15:36:04 +0000 (16:36 +0100)
find_package doesn't check that the library found can be linked.
Particularly when cross-compiling (observed on Power7 front end to
BlueGene/Q), this can be a problem. We need libxml2 to be reliably
detected in order to know whether we should attempt to build the test
binaries.

Change-Id: Iaf9aa2c9577ed66bd41a03c97c13b6e4d6f493c0

CMakeLists.txt
cmake/gmxTestLibXml2.cmake [new file with mode: 0644]

index 8613189318d87fa8c9a94b1cbe41592feb1c2a2e..c490acec15b7bdd78e1fa0557e1d5dac1b141231 100644 (file)
@@ -368,13 +368,18 @@ include(gmxManageSharedLibraries)
 # Find external packages                                               #
 ########################################################################
 
-# Unconditionally find the package, as it is also required for unit tests
+# Unconditionally find the package, as it is also required for unit
+# tests. This exports LIBXML2_FOUND, which we should not use because
+# it does not tell us that linking will succeed. Instead, we test that
+# next.
 find_package(LibXml2)
-option(GMX_XML "Use libxml2 to parse xml files (currently has no effect)" ${LIBXML2_FOUND})
+include(gmxTestLibXml2)
+gmx_test_libxml2(HAVE_LIBXML2)
+option(GMX_XML "Use libxml2 to parse xml files (currently has no effect)" ${HAVE_LIBXML2})
 set(PKG_XML "")
 mark_as_advanced(GMX_XML)
 # Don't actually do anything, since libxml2 is currently not used by libgromacs
-#if(GMX_XML AND NOT LIBXML2_FOUND)
+#if(GMX_XML AND NOT HAVE_LIBXML2)
 #    message(FATAL_ERROR "libxml2 not found. Set GMX_XML=OFF to compile without XML support")
 #endif()
 #if(GMX_XML)
@@ -480,13 +485,13 @@ if(GMX_EXTERNAL_BOOST AND NOT Boost_FOUND)
         "version of Boost included with Gromacs.")
 endif()
 
-option(GMX_BUILD_UNITTESTS "Build unit tests with BUILD_TESTING (uses Google C++ Testing and Mocking Frameworks, requires libxml2)" ${LIBXML2_FOUND})
+option(GMX_BUILD_UNITTESTS "Build unit tests with BUILD_TESTING (uses Google C++ Testing and Mocking Frameworks, requires libxml2)" ${HAVE_LIBXML2})
 mark_as_advanced(GMX_BUILD_UNITTESTS)
 gmx_add_cache_dependency(GMX_BUILD_UNITTESTS BOOL BUILD_TESTING OFF)
-if (GMX_BUILD_UNITTESTS AND NOT LIBXML2_FOUND)
+if (GMX_BUILD_UNITTESTS AND NOT HAVE_LIBXML2)
     message(FATAL_ERROR
         "Cannot build unit tests without libxml2. "
-        "Either set GMX_BUILD_UNITTESTS=OFF or tell CMake how to find libxml2.")
+        "Either set GMX_BUILD_UNITTESTS=OFF or tell CMake how to find a working version of libxml2.")
 endif()
 
 ########################################################################
diff --git a/cmake/gmxTestLibXml2.cmake b/cmake/gmxTestLibXml2.cmake
new file mode 100644 (file)
index 0000000..eab5a4b
--- /dev/null
@@ -0,0 +1,65 @@
+#
+# This file is part of the GROMACS molecular simulation package.
+#
+# Copyright (c) 2014, 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.
+
+# - Define macro to check if linking to libxml2 actually works,
+# because the find_package macro is content if one exists.  This can
+# fail in cross-compilation environments, and we want to know about
+# libxml2 so the test binaries are built only when they will work.
+#
+#  GMX_TEST_LIBXML2(VARIABLE)
+#
+#  VARIABLE will be set to true if libxml2 support is present
+
+include(CheckCSourceCompiles)
+include(gmxOptionUtilities)
+function(GMX_TEST_LIBXML2 VARIABLE)
+    if(LIBXML2_FOUND)
+        gmx_check_if_changed(_do_libxml2_recompile LIBXML2_INCLUDE_DIR LIBXML2_LIBRARIES)
+        if(_do_libxml2_recompile)
+            unset(LIBXML2_COMPILES_OK CACHE)
+        endif()
+        set(CMAKE_REQUIRED_INCLUDES "${LIBXML2_INCLUDE_DIR}")
+        set(CMAKE_REQUIRED_LIBRARIES "${LIBXML2_LIBRARIES}")
+        check_c_source_compiles(
+            "#include <libxml/xmlwriter.h>
+int main(void) { xmlTextWriterEndAttribute(0); }"
+            LIBXML2_COMPILES_OK)
+        set(${VARIABLE} ${LIBXML2_COMPILES_OK} PARENT_SCOPE)
+    else()
+        set(${VARIABLE} OFF PARENT_SCOPE)
+    endif()
+endfunction()
+
+
+