FindLibXml2.cmake 4.2 KB

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