1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4#[=======================================================================[.rst:
5FindLibXml2
6-----------
7
8Find the XML processing library (libxml2).
9
10IMPORTED Targets
11^^^^^^^^^^^^^^^^
12
13.. versionadded:: 3.12
14
15The following :prop_tgt:`IMPORTED` targets may be defined:
16
17``LibXml2::LibXml2``
18  libxml2 library.
19``LibXml2::xmllint``
20  .. versionadded:: 3.17
21
22  xmllint command-line executable.
23
24Result variables
25^^^^^^^^^^^^^^^^
26
27This module will set the following variables in your project:
28
29``LibXml2_FOUND``
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
35``LIBXML2_LIBRARIES``
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)
43
44Cache variables
45^^^^^^^^^^^^^^^
46
47The following cache variables may also be set:
48
49``LIBXML2_INCLUDE_DIR``
50  the directory containing LibXml2 headers
51``LIBXML2_LIBRARY``
52  path to the LibXml2 library
53#]=======================================================================]
54
55# use pkg-config to get the directories and then use these values
56# in the find_path() and find_library() calls
57find_package(PkgConfig QUIET)
58PKG_CHECK_MODULES(PC_LIBXML QUIET libxml-2.0)
59
60find_path(LIBXML2_INCLUDE_DIR NAMES libxml/xpath.h
61   HINTS
62   ${PC_LIBXML_INCLUDEDIR}
63   ${PC_LIBXML_INCLUDE_DIRS}
64   PATH_SUFFIXES libxml2
65   )
66
67# CMake 3.9 and below used 'LIBXML2_LIBRARIES' as the name of
68# the cache entry storing the find_library result.  Use the
69# value if it was set by the project or user.
70if(DEFINED LIBXML2_LIBRARIES AND NOT DEFINED LIBXML2_LIBRARY)
71  set(LIBXML2_LIBRARY ${LIBXML2_LIBRARIES})
72endif()
73
74find_library(LIBXML2_LIBRARY NAMES xml2 libxml2 libxml2_a
75   HINTS
76   ${PC_LIBXML_LIBDIR}
77   ${PC_LIBXML_LIBRARY_DIRS}
78   )
79
80find_program(LIBXML2_XMLLINT_EXECUTABLE xmllint)
81# for backwards compat. with KDE 4.0.x:
82set(XMLLINT_EXECUTABLE "${LIBXML2_XMLLINT_EXECUTABLE}")
83
84if(LIBXML2_INCLUDE_DIR AND EXISTS "${LIBXML2_INCLUDE_DIR}/libxml/xmlversion.h")
85    file(STRINGS "${LIBXML2_INCLUDE_DIR}/libxml/xmlversion.h" libxml2_version_str
86         REGEX "^#define[\t ]+LIBXML_DOTTED_VERSION[\t ]+\".*\"")
87
88    string(REGEX REPLACE "^#define[\t ]+LIBXML_DOTTED_VERSION[\t ]+\"([^\"]*)\".*" "\\1"
89           LIBXML2_VERSION_STRING "${libxml2_version_str}")
90    unset(libxml2_version_str)
91endif()
92
93set(LIBXML2_INCLUDE_DIRS ${LIBXML2_INCLUDE_DIR})
94set(LIBXML2_LIBRARIES ${LIBXML2_LIBRARY})
95
96# Did we find the same installation as pkg-config?
97# If so, use additional information from it.
98unset(LIBXML2_DEFINITIONS)
99foreach(libxml2_pc_lib_dir IN LISTS PC_LIBXML_LIBDIR PC_LIBXML_LIBRARY_DIRS)
100  if (LIBXML2_LIBRARY MATCHES "^${libxml2_pc_lib_dir}")
101    list(APPEND LIBXML2_INCLUDE_DIRS ${PC_LIBXML_INCLUDE_DIRS})
102    set(LIBXML2_DEFINITIONS ${PC_LIBXML_CFLAGS_OTHER})
103    break()
104  endif()
105endforeach()
106
107include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
108FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibXml2
109                                  REQUIRED_VARS LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR
110                                  VERSION_VAR LIBXML2_VERSION_STRING)
111
112mark_as_advanced(LIBXML2_INCLUDE_DIR LIBXML2_LIBRARY LIBXML2_XMLLINT_EXECUTABLE)
113
114if(LibXml2_FOUND AND NOT TARGET LibXml2::LibXml2)
115  add_library(LibXml2::LibXml2 UNKNOWN IMPORTED)
116  set_target_properties(LibXml2::LibXml2 PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${LIBXML2_INCLUDE_DIRS}")
117  set_target_properties(LibXml2::LibXml2 PROPERTIES INTERFACE_COMPILE_OPTIONS "${LIBXML2_DEFINITIONS}")
118  set_property(TARGET LibXml2::LibXml2 APPEND PROPERTY IMPORTED_LOCATION "${LIBXML2_LIBRARY}")
119endif()
120
121if(LIBXML2_XMLLINT_EXECUTABLE AND NOT TARGET LibXml2::xmllint)
122  add_executable(LibXml2::xmllint IMPORTED)
123  set_target_properties(LibXml2::xmllint PROPERTIES IMPORTED_LOCATION "${LIBXML2_XMLLINT_EXECUTABLE}")
124endif()
125