1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 Find the XML processing library (libxml2).
13 .. versionadded:: 3.12
15 The following :prop_tgt:`IMPORTED` targets may be defined:
20 .. versionadded:: 3.17
22 xmllint command-line executable.
27 This module will set the following variables in your project:
30 true if libxml2 headers and libraries were found
31 ``LIBXML2_INCLUDE_DIR``
32 the directory containing LibXml2 headers
33 ``LIBXML2_INCLUDE_DIRS``
34 list of the include directories needed to use LibXml2
36 LibXml2 libraries to be linked
37 ``LIBXML2_DEFINITIONS``
38 the compiler switches required for using LibXml2
39 ``LIBXML2_XMLLINT_EXECUTABLE``
40 path to the XML checking tool xmllint coming with LibXml2
41 ``LIBXML2_VERSION_STRING``
42 the version of LibXml2 found (since CMake 2.8.8)
47 The following cache variables may also be set:
49 ``LIBXML2_INCLUDE_DIR``
50 the directory containing LibXml2 headers
52 path to the LibXml2 library
53 #]=======================================================================]
56 cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
58 # use pkg-config to get the directories and then use these values
59 # in the find_path() and find_library() calls
60 find_package(PkgConfig QUIET)
62 pkg_check_modules(PC_LIBXML QUIET libxml-2.0)
65 find_path(LIBXML2_INCLUDE_DIR NAMES libxml/xpath.h
67 ${PC_LIBXML_INCLUDEDIR}
68 ${PC_LIBXML_INCLUDE_DIRS}
72 # CMake 3.9 and below used 'LIBXML2_LIBRARIES' as the name of
73 # the cache entry storing the find_library result. Use the
74 # value if it was set by the project or user.
75 if(DEFINED LIBXML2_LIBRARIES AND NOT DEFINED LIBXML2_LIBRARY)
76 set(LIBXML2_LIBRARY ${LIBXML2_LIBRARIES})
79 find_library(LIBXML2_LIBRARY NAMES xml2 libxml2 libxml2_a
82 ${PC_LIBXML_LIBRARY_DIRS}
85 find_program(LIBXML2_XMLLINT_EXECUTABLE xmllint)
86 # for backwards compat. with KDE 4.0.x:
87 set(XMLLINT_EXECUTABLE "${LIBXML2_XMLLINT_EXECUTABLE}")
89 if(LIBXML2_INCLUDE_DIR AND EXISTS "${LIBXML2_INCLUDE_DIR}/libxml/xmlversion.h")
90 file(STRINGS "${LIBXML2_INCLUDE_DIR}/libxml/xmlversion.h" libxml2_version_str
91 REGEX "^#define[\t ]+LIBXML_DOTTED_VERSION[\t ]+\".*\"")
93 string(REGEX REPLACE "^#define[\t ]+LIBXML_DOTTED_VERSION[\t ]+\"([^\"]*)\".*" "\\1"
94 LIBXML2_VERSION_STRING "${libxml2_version_str}")
95 unset(libxml2_version_str)
98 set(LIBXML2_INCLUDE_DIRS ${LIBXML2_INCLUDE_DIR})
99 set(LIBXML2_LIBRARIES ${LIBXML2_LIBRARY})
101 # Did we find the same installation as pkg-config?
102 # If so, use additional information from it.
103 unset(LIBXML2_DEFINITIONS)
104 foreach(libxml2_pc_lib_dir IN LISTS PC_LIBXML_LIBDIR PC_LIBXML_LIBRARY_DIRS)
105 if (LIBXML2_LIBRARY MATCHES "^${libxml2_pc_lib_dir}")
106 list(APPEND LIBXML2_INCLUDE_DIRS ${PC_LIBXML_INCLUDE_DIRS})
107 set(LIBXML2_DEFINITIONS ${PC_LIBXML_CFLAGS_OTHER})
112 include(FindPackageHandleStandardArgs)
113 find_package_handle_standard_args(LibXml2
114 REQUIRED_VARS LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR
115 VERSION_VAR LIBXML2_VERSION_STRING)
117 mark_as_advanced(LIBXML2_INCLUDE_DIR LIBXML2_LIBRARY LIBXML2_XMLLINT_EXECUTABLE)
119 if(LibXml2_FOUND AND NOT TARGET LibXml2::LibXml2)
120 add_library(LibXml2::LibXml2 UNKNOWN IMPORTED)
121 set_target_properties(LibXml2::LibXml2 PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${LIBXML2_INCLUDE_DIRS}")
122 set_target_properties(LibXml2::LibXml2 PROPERTIES INTERFACE_COMPILE_OPTIONS "${LIBXML2_DEFINITIONS}")
123 set_property(TARGET LibXml2::LibXml2 APPEND PROPERTY IMPORTED_LOCATION "${LIBXML2_LIBRARY}")
126 if(LIBXML2_XMLLINT_EXECUTABLE AND NOT TARGET LibXml2::xmllint)
127 add_executable(LibXml2::xmllint IMPORTED)
128 set_target_properties(LibXml2::xmllint PROPERTIES IMPORTED_LOCATION "${LIBXML2_XMLLINT_EXECUTABLE}")