FindLibXml2.cmake 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. # use pkg-config to get the directories and then use these values
  42. # in the find_path() and find_library() calls
  43. find_package(PkgConfig QUIET)
  44. PKG_CHECK_MODULES(PC_LIBXML QUIET libxml-2.0)
  45. find_path(LIBXML2_INCLUDE_DIR NAMES libxml/xpath.h
  46. HINTS
  47. ${PC_LIBXML_INCLUDEDIR}
  48. ${PC_LIBXML_INCLUDE_DIRS}
  49. PATH_SUFFIXES libxml2
  50. )
  51. # CMake 3.9 and below used 'LIBXML2_LIBRARIES' as the name of
  52. # the cache entry storing the find_library result. Use the
  53. # value if it was set by the project or user.
  54. if(DEFINED LIBXML2_LIBRARIES AND NOT DEFINED LIBXML2_LIBRARY)
  55. set(LIBXML2_LIBRARY ${LIBXML2_LIBRARIES})
  56. endif()
  57. find_library(LIBXML2_LIBRARY NAMES xml2 libxml2 libxml2_a
  58. HINTS
  59. ${PC_LIBXML_LIBDIR}
  60. ${PC_LIBXML_LIBRARY_DIRS}
  61. )
  62. find_program(LIBXML2_XMLLINT_EXECUTABLE xmllint)
  63. # for backwards compat. with KDE 4.0.x:
  64. set(XMLLINT_EXECUTABLE "${LIBXML2_XMLLINT_EXECUTABLE}")
  65. if(LIBXML2_INCLUDE_DIR AND EXISTS "${LIBXML2_INCLUDE_DIR}/libxml/xmlversion.h")
  66. file(STRINGS "${LIBXML2_INCLUDE_DIR}/libxml/xmlversion.h" libxml2_version_str
  67. REGEX "^#define[\t ]+LIBXML_DOTTED_VERSION[\t ]+\".*\"")
  68. string(REGEX REPLACE "^#define[\t ]+LIBXML_DOTTED_VERSION[\t ]+\"([^\"]*)\".*" "\\1"
  69. LIBXML2_VERSION_STRING "${libxml2_version_str}")
  70. unset(libxml2_version_str)
  71. endif()
  72. set(LIBXML2_INCLUDE_DIRS ${LIBXML2_INCLUDE_DIR})
  73. set(LIBXML2_LIBRARIES ${LIBXML2_LIBRARY})
  74. # Did we find the same installation as pkg-config?
  75. # If so, use additional information from it.
  76. unset(LIBXML2_DEFINITIONS)
  77. foreach(libxml2_pc_lib_dir IN LISTS PC_LIBXML_LIBDIR PC_LIBXML_LIBRARY_DIRS)
  78. if (LIBXML2_LIBRARY MATCHES "^${libxml2_pc_lib_dir}")
  79. list(APPEND LIBXML2_INCLUDE_DIRS ${PC_LIBXML_INCLUDE_DIRS})
  80. set(LIBXML2_DEFINITIONS ${PC_LIBXML_CFLAGS_OTHER})
  81. break()
  82. endif()
  83. endforeach()
  84. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  85. FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibXml2
  86. REQUIRED_VARS LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR
  87. VERSION_VAR LIBXML2_VERSION_STRING)
  88. mark_as_advanced(LIBXML2_INCLUDE_DIR LIBXML2_LIBRARY LIBXML2_XMLLINT_EXECUTABLE)
  89. if(LibXml2_FOUND AND NOT TARGET LibXml2::LibXml2)
  90. add_library(LibXml2::LibXml2 UNKNOWN IMPORTED)
  91. set_target_properties(LibXml2::LibXml2 PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${LIBXML2_INCLUDE_DIRS}")
  92. set_target_properties(LibXml2::LibXml2 PROPERTIES INTERFACE_COMPILE_OPTIONS "${LIBXML2_DEFINITIONS}")
  93. set_property(TARGET LibXml2::LibXml2 APPEND PROPERTY IMPORTED_LOCATION "${LIBXML2_LIBRARY}")
  94. endif()
  95. if(LIBXML2_XMLLINT_EXECUTABLE AND NOT TARGET LibXml2::xmllint)
  96. add_executable(LibXml2::xmllint IMPORTED)
  97. set_target_properties(LibXml2::xmllint PROPERTIES IMPORTED_LOCATION "${LIBXML2_XMLLINT_EXECUTABLE}")
  98. endif()