blob: f551dfe1d423d92934fe55487169c72b5d1e16e5 [file] [log] [blame]
Haibo Huang82cd0512020-03-20 14:28:55 -07001# 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
13The following :prop_tgt:`IMPORTED` targets may be defined:
14
15``LibXml2::LibXml2``
16 If the libxml2 library has been found
17``LibXml2::xmllint``
18 If the xmllint command-line executable has been found
19
20Result variables
21^^^^^^^^^^^^^^^^
22
23This module will set the following variables in your project:
24
25``LibXml2_FOUND``
26 true if libxml2 headers and libraries were found
27``LIBXML2_INCLUDE_DIR``
28 the directory containing LibXml2 headers
29``LIBXML2_INCLUDE_DIRS``
30 list of the include directories needed to use LibXml2
31``LIBXML2_LIBRARIES``
32 LibXml2 libraries to be linked
33``LIBXML2_DEFINITIONS``
34 the compiler switches required for using LibXml2
35``LIBXML2_XMLLINT_EXECUTABLE``
36 path to the XML checking tool xmllint coming with LibXml2
37``LIBXML2_VERSION_STRING``
38 the version of LibXml2 found (since CMake 2.8.8)
39
40Cache variables
41^^^^^^^^^^^^^^^
42
43The following cache variables may also be set:
44
45``LIBXML2_INCLUDE_DIR``
46 the directory containing LibXml2 headers
47``LIBXML2_LIBRARY``
48 path to the LibXml2 library
49#]=======================================================================]
50
51# use pkg-config to get the directories and then use these values
52# in the find_path() and find_library() calls
53find_package(PkgConfig QUIET)
54PKG_CHECK_MODULES(PC_LIBXML QUIET libxml-2.0)
55
56find_path(LIBXML2_INCLUDE_DIR NAMES libxml/xpath.h
57 HINTS
58 ${PC_LIBXML_INCLUDEDIR}
59 ${PC_LIBXML_INCLUDE_DIRS}
60 PATH_SUFFIXES libxml2
61 )
62
63# CMake 3.9 and below used 'LIBXML2_LIBRARIES' as the name of
64# the cache entry storing the find_library result. Use the
65# value if it was set by the project or user.
66if(DEFINED LIBXML2_LIBRARIES AND NOT DEFINED LIBXML2_LIBRARY)
67 set(LIBXML2_LIBRARY ${LIBXML2_LIBRARIES})
68endif()
69
70find_library(LIBXML2_LIBRARY NAMES xml2 libxml2
71 HINTS
72 ${PC_LIBXML_LIBDIR}
73 ${PC_LIBXML_LIBRARY_DIRS}
74 )
75
76find_program(LIBXML2_XMLLINT_EXECUTABLE xmllint)
77# for backwards compat. with KDE 4.0.x:
78set(XMLLINT_EXECUTABLE "${LIBXML2_XMLLINT_EXECUTABLE}")
79
80if(LIBXML2_INCLUDE_DIR AND EXISTS "${LIBXML2_INCLUDE_DIR}/libxml/xmlversion.h")
81 file(STRINGS "${LIBXML2_INCLUDE_DIR}/libxml/xmlversion.h" libxml2_version_str
82 REGEX "^#define[\t ]+LIBXML_DOTTED_VERSION[\t ]+\".*\"")
83
84 string(REGEX REPLACE "^#define[\t ]+LIBXML_DOTTED_VERSION[\t ]+\"([^\"]*)\".*" "\\1"
85 LIBXML2_VERSION_STRING "${libxml2_version_str}")
86 unset(libxml2_version_str)
87endif()
88
89set(LIBXML2_INCLUDE_DIRS ${LIBXML2_INCLUDE_DIR})
90set(LIBXML2_LIBRARIES ${LIBXML2_LIBRARY})
91
92# Did we find the same installation as pkg-config?
93# If so, use additional information from it.
94unset(LIBXML2_DEFINITIONS)
95foreach(libxml2_pc_lib_dir IN LISTS PC_LIBXML_LIBDIR PC_LIBXML_LIBRARY_DIRS)
96 if (LIBXML2_LIBRARY MATCHES "^${libxml2_pc_lib_dir}")
97 list(APPEND LIBXML2_INCLUDE_DIRS ${PC_LIBXML_INCLUDE_DIRS})
98 set(LIBXML2_DEFINITIONS ${PC_LIBXML_CFLAGS_OTHER})
99 break()
100 endif()
101endforeach()
102
103include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
104FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibXml2
105 REQUIRED_VARS LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR
106 VERSION_VAR LIBXML2_VERSION_STRING)
107
108mark_as_advanced(LIBXML2_INCLUDE_DIR LIBXML2_LIBRARY LIBXML2_XMLLINT_EXECUTABLE)
109
110if(LibXml2_FOUND AND NOT TARGET LibXml2::LibXml2)
111 add_library(LibXml2::LibXml2 UNKNOWN IMPORTED)
112 set_target_properties(LibXml2::LibXml2 PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${LIBXML2_INCLUDE_DIRS}")
113 set_target_properties(LibXml2::LibXml2 PROPERTIES INTERFACE_COMPILE_OPTIONS "${LIBXML2_DEFINITIONS}")
114 set_property(TARGET LibXml2::LibXml2 APPEND PROPERTY IMPORTED_LOCATION "${LIBXML2_LIBRARY}")
115endif()
116
117if(LIBXML2_XMLLINT_EXECUTABLE AND NOT TARGET LibXml2::xmllint)
118 add_executable(LibXml2::xmllint IMPORTED)
119 set_target_properties(LibXml2::xmllint PROPERTIES IMPORTED_LOCATION "${LIBXML2_XMLLINT_EXECUTABLE}")
120endif()