FindLibXml2.cmake 4.1 KB

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