FindLibXml2.cmake 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindLibXml2
  5. -----------
  6. Finds the XML processing library (libxml2):
  7. .. code-block:: cmake
  8. find_package(LibXml2 [<version>] [...])
  9. Imported Targets
  10. ^^^^^^^^^^^^^^^^
  11. This module provides the following :ref:`Imported Targets`:
  12. ``LibXml2::LibXml2``
  13. .. versionadded:: 3.12
  14. Target encapsulating the libxml2 library usage requirements, available only if
  15. library is found.
  16. ``LibXml2::xmllint``
  17. .. versionadded:: 3.17
  18. Target encapsulating the xmllint command-line executable, available only if
  19. xmllint executable is found.
  20. Result Variables
  21. ^^^^^^^^^^^^^^^^
  22. This module defines the following variables:
  23. ``LibXml2_FOUND``
  24. .. versionadded:: 3.3
  25. Boolean indicating whether the (requested version of) libxml2 library was
  26. found.
  27. ``LibXml2_VERSION``
  28. .. versionadded:: 4.2
  29. The version of the libxml2 found.
  30. ``LIBXML2_INCLUDE_DIRS``
  31. Include directories needed to use the libxml2 library.
  32. ``LIBXML2_LIBRARIES``
  33. Libraries needed to link against to use the libxml2 library.
  34. ``LIBXML2_DEFINITIONS``
  35. The compiler switches required for using libxml2.
  36. Other Variables
  37. ^^^^^^^^^^^^^^^
  38. ``LibXml2_USE_STATIC_LIBS``
  39. .. versionadded:: 4.3
  40. Set to ``TRUE`` to use static libraries. Default is ``FALSE``.
  41. Cache Variables
  42. ^^^^^^^^^^^^^^^
  43. The following cache variables may also be set:
  44. ``LIBXML2_INCLUDE_DIR``
  45. The include directory containing libxml2 headers.
  46. ``LIBXML2_LIBRARY``
  47. The path to the libxml2 library.
  48. ``LIBXML2_XMLLINT_EXECUTABLE``
  49. The path to the XML checking tool ``xmllint`` coming with libxml2.
  50. Deprecated Variables
  51. ^^^^^^^^^^^^^^^^^^^^
  52. The following variables are provided for backward compatibility:
  53. ``LIBXML2_FOUND``
  54. .. deprecated:: 4.2
  55. Use ``LibXml2_FOUND``, which has the same value.
  56. Boolean indicating whether the (requested version of) libxml2 library was
  57. found.
  58. ``LIBXML2_VERSION_STRING``
  59. .. deprecated:: 4.2
  60. Superseded by the ``LibXml2_VERSION``.
  61. The version of the libxml2 found.
  62. Examples
  63. ^^^^^^^^
  64. Finding the libxml2 library and linking it to a project target:
  65. .. code-block:: cmake
  66. find_package(LibXml2)
  67. target_link_libraries(project_target PRIVATE LibXml2::LibXml2)
  68. #]=======================================================================]
  69. cmake_policy(PUSH)
  70. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  71. # use pkg-config to get the directories and then use these values
  72. # in the find_path() and find_library() calls
  73. find_package(PkgConfig QUIET)
  74. if(PkgConfig_FOUND)
  75. pkg_check_modules(PC_LIBXML QUIET libxml-2.0)
  76. endif()
  77. find_path(LIBXML2_INCLUDE_DIR NAMES libxml/xpath.h
  78. HINTS
  79. ${PC_LIBXML_INCLUDEDIR}
  80. ${PC_LIBXML_INCLUDE_DIRS}
  81. PATH_SUFFIXES libxml2
  82. )
  83. # CMake 3.9 and below used 'LIBXML2_LIBRARIES' as the name of
  84. # the cache entry storing the find_library result. Use the
  85. # value if it was set by the project or user.
  86. if(DEFINED LIBXML2_LIBRARIES AND NOT DEFINED LIBXML2_LIBRARY)
  87. set(LIBXML2_LIBRARY ${LIBXML2_LIBRARIES})
  88. endif()
  89. # Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
  90. if(LibXml2_USE_STATIC_LIBS)
  91. set(_libxml2_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  92. if(WIN32)
  93. list(INSERT CMAKE_FIND_LIBRARY_SUFFIXES 0 .lib .a)
  94. else()
  95. set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
  96. endif()
  97. endif()
  98. find_library(LIBXML2_LIBRARY NAMES xml2 libxml2 libxml2_a
  99. HINTS
  100. ${PC_LIBXML_LIBDIR}
  101. ${PC_LIBXML_LIBRARY_DIRS}
  102. )
  103. # Restore the original find library ordering
  104. if(LibXml2_USE_STATIC_LIBS)
  105. set(CMAKE_FIND_LIBRARY_SUFFIXES ${_libxml2_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
  106. endif()
  107. find_program(LIBXML2_XMLLINT_EXECUTABLE xmllint)
  108. # for backwards compat. with KDE 4.0.x:
  109. set(XMLLINT_EXECUTABLE "${LIBXML2_XMLLINT_EXECUTABLE}")
  110. if(LIBXML2_INCLUDE_DIR AND EXISTS "${LIBXML2_INCLUDE_DIR}/libxml/xmlversion.h")
  111. file(STRINGS "${LIBXML2_INCLUDE_DIR}/libxml/xmlversion.h" libxml2_version_str
  112. REGEX "^#define[\t ]+LIBXML_DOTTED_VERSION[\t ]+\".*\"")
  113. string(REGEX REPLACE "^#define[\t ]+LIBXML_DOTTED_VERSION[\t ]+\"([^\"]*)\".*" "\\1"
  114. LibXml2_VERSION "${libxml2_version_str}")
  115. set(LIBXML2_VERSION_STRING "${LibXml2_VERSION}")
  116. unset(libxml2_version_str)
  117. endif()
  118. set(LIBXML2_INCLUDE_DIRS ${LIBXML2_INCLUDE_DIR})
  119. set(LIBXML2_LIBRARIES ${LIBXML2_LIBRARY})
  120. # Did we find the same installation as pkg-config?
  121. # If so, use additional information from it.
  122. unset(LIBXML2_DEFINITIONS)
  123. foreach(libxml2_pc_lib_dir IN LISTS PC_LIBXML_LIBDIR PC_LIBXML_LIBRARY_DIRS)
  124. if (LIBXML2_LIBRARY MATCHES "^${libxml2_pc_lib_dir}")
  125. list(APPEND LIBXML2_INCLUDE_DIRS ${PC_LIBXML_INCLUDE_DIRS})
  126. set(LIBXML2_DEFINITIONS ${PC_LIBXML_CFLAGS_OTHER})
  127. break()
  128. endif()
  129. endforeach()
  130. include(FindPackageHandleStandardArgs)
  131. find_package_handle_standard_args(LibXml2
  132. REQUIRED_VARS LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR
  133. VERSION_VAR LibXml2_VERSION)
  134. mark_as_advanced(LIBXML2_INCLUDE_DIR LIBXML2_LIBRARY LIBXML2_XMLLINT_EXECUTABLE)
  135. if(LibXml2_FOUND AND NOT TARGET LibXml2::LibXml2)
  136. add_library(LibXml2::LibXml2 UNKNOWN IMPORTED)
  137. set_target_properties(LibXml2::LibXml2 PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${LIBXML2_INCLUDE_DIRS}")
  138. set_target_properties(LibXml2::LibXml2 PROPERTIES INTERFACE_COMPILE_OPTIONS "${LIBXML2_DEFINITIONS}")
  139. set_property(TARGET LibXml2::LibXml2 APPEND PROPERTY IMPORTED_LOCATION "${LIBXML2_LIBRARY}")
  140. endif()
  141. if(LIBXML2_XMLLINT_EXECUTABLE AND NOT TARGET LibXml2::xmllint)
  142. add_executable(LibXml2::xmllint IMPORTED)
  143. set_target_properties(LibXml2::xmllint PROPERTIES IMPORTED_LOCATION "${LIBXML2_XMLLINT_EXECUTABLE}")
  144. endif()
  145. cmake_policy(POP)