FindLibXml2.cmake 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. This module defines :prop_tgt:`IMPORTED` target ``LibXml2::LibXml2``, if
  10. libxml2 has been found.
  11. Result variables
  12. ^^^^^^^^^^^^^^^^
  13. This module will set the following variables in your project:
  14. ``LibXml2_FOUND``
  15. true if libxml2 headers and libraries were found
  16. ``LIBXML2_INCLUDE_DIR``
  17. the directory containing LibXml2 headers
  18. ``LIBXML2_INCLUDE_DIRS``
  19. list of the include directories needed to use LibXml2
  20. ``LIBXML2_LIBRARIES``
  21. LibXml2 libraries to be linked
  22. ``LIBXML2_DEFINITIONS``
  23. the compiler switches required for using LibXml2
  24. ``LIBXML2_XMLLINT_EXECUTABLE``
  25. path to the XML checking tool xmllint coming with LibXml2
  26. ``LIBXML2_VERSION_STRING``
  27. the version of LibXml2 found (since CMake 2.8.8)
  28. Cache variables
  29. ^^^^^^^^^^^^^^^
  30. The following cache variables may also be set:
  31. ``LIBXML2_INCLUDE_DIR``
  32. the directory containing LibXml2 headers
  33. ``LIBXML2_LIBRARY``
  34. path to the LibXml2 library
  35. #]=======================================================================]
  36. # use pkg-config to get the directories and then use these values
  37. # in the find_path() and find_library() calls
  38. find_package(PkgConfig QUIET)
  39. PKG_CHECK_MODULES(PC_LIBXML QUIET libxml-2.0)
  40. set(LIBXML2_DEFINITIONS ${PC_LIBXML_CFLAGS_OTHER})
  41. find_path(LIBXML2_INCLUDE_DIR NAMES libxml/xpath.h
  42. HINTS
  43. ${PC_LIBXML_INCLUDEDIR}
  44. ${PC_LIBXML_INCLUDE_DIRS}
  45. PATH_SUFFIXES libxml2
  46. )
  47. # CMake 3.9 and below used 'LIBXML2_LIBRARIES' as the name of
  48. # the cache entry storing the find_library result. Use the
  49. # value if it was set by the project or user.
  50. if(DEFINED LIBXML2_LIBRARIES AND NOT DEFINED LIBXML2_LIBRARY)
  51. set(LIBXML2_LIBRARY ${LIBXML2_LIBRARIES})
  52. endif()
  53. find_library(LIBXML2_LIBRARY NAMES xml2 libxml2
  54. HINTS
  55. ${PC_LIBXML_LIBDIR}
  56. ${PC_LIBXML_LIBRARY_DIRS}
  57. )
  58. find_program(LIBXML2_XMLLINT_EXECUTABLE xmllint)
  59. # for backwards compat. with KDE 4.0.x:
  60. set(XMLLINT_EXECUTABLE "${LIBXML2_XMLLINT_EXECUTABLE}")
  61. if(PC_LIBXML_VERSION)
  62. set(LIBXML2_VERSION_STRING ${PC_LIBXML_VERSION})
  63. elseif(LIBXML2_INCLUDE_DIR AND EXISTS "${LIBXML2_INCLUDE_DIR}/libxml/xmlversion.h")
  64. file(STRINGS "${LIBXML2_INCLUDE_DIR}/libxml/xmlversion.h" libxml2_version_str
  65. REGEX "^#define[\t ]+LIBXML_DOTTED_VERSION[\t ]+\".*\"")
  66. string(REGEX REPLACE "^#define[\t ]+LIBXML_DOTTED_VERSION[\t ]+\"([^\"]*)\".*" "\\1"
  67. LIBXML2_VERSION_STRING "${libxml2_version_str}")
  68. unset(libxml2_version_str)
  69. endif()
  70. set(LIBXML2_INCLUDE_DIRS ${LIBXML2_INCLUDE_DIR} ${PC_LIBXML_INCLUDE_DIRS})
  71. set(LIBXML2_LIBRARIES ${LIBXML2_LIBRARY})
  72. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  73. FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibXml2
  74. REQUIRED_VARS LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR
  75. VERSION_VAR LIBXML2_VERSION_STRING)
  76. mark_as_advanced(LIBXML2_INCLUDE_DIR LIBXML2_LIBRARY LIBXML2_XMLLINT_EXECUTABLE)
  77. if(LibXml2_FOUND AND NOT TARGET LibXml2::LibXml2)
  78. add_library(LibXml2::LibXml2 UNKNOWN IMPORTED)
  79. set_target_properties(LibXml2::LibXml2 PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${LIBXML2_INCLUDE_DIRS}")
  80. set_property(TARGET LibXml2::LibXml2 APPEND PROPERTY IMPORTED_LOCATION "${LIBXML2_LIBRARY}")
  81. endif()