# 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)
"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()
########################################################################
--- /dev/null
+#
+# 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()
+
+
+