FindEXPAT.cmake 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. FindEXPAT
  5. ---------
  6. Find the native Expat headers and library.
  7. Expat is a stream-oriented XML parser library written in C.
  8. Imported Targets
  9. ^^^^^^^^^^^^^^^^
  10. This module defines the following :prop_tgt:`IMPORTED` targets:
  11. ``EXPAT::EXPAT``
  12. The Expat ``expat`` library, if found.
  13. Result Variables
  14. ^^^^^^^^^^^^^^^^
  15. This module will set the following variables in your project:
  16. ``EXPAT_INCLUDE_DIRS``
  17. where to find expat.h, etc.
  18. ``EXPAT_LIBRARIES``
  19. the libraries to link against to use Expat.
  20. ``EXPAT_FOUND``
  21. true if the Expat headers and libraries were found.
  22. #]=======================================================================]
  23. find_package(PkgConfig QUIET)
  24. pkg_check_modules(PC_EXPAT QUIET expat)
  25. # Look for the header file.
  26. find_path(EXPAT_INCLUDE_DIR NAMES expat.h HINTS ${PC_EXPAT_INCLUDE_DIRS})
  27. # Look for the library.
  28. find_library(EXPAT_LIBRARY NAMES expat libexpat HINTS ${PC_EXPAT_LIBRARY_DIRS})
  29. if (EXPAT_INCLUDE_DIR AND EXISTS "${EXPAT_INCLUDE_DIR}/expat.h")
  30. file(STRINGS "${EXPAT_INCLUDE_DIR}/expat.h" expat_version_str
  31. REGEX "^#[\t ]*define[\t ]+XML_(MAJOR|MINOR|MICRO)_VERSION[\t ]+[0-9]+$")
  32. unset(EXPAT_VERSION_STRING)
  33. foreach(VPART MAJOR MINOR MICRO)
  34. foreach(VLINE ${expat_version_str})
  35. if(VLINE MATCHES "^#[\t ]*define[\t ]+XML_${VPART}_VERSION[\t ]+([0-9]+)$")
  36. set(EXPAT_VERSION_PART "${CMAKE_MATCH_1}")
  37. if(EXPAT_VERSION_STRING)
  38. string(APPEND EXPAT_VERSION_STRING ".${EXPAT_VERSION_PART}")
  39. else()
  40. set(EXPAT_VERSION_STRING "${EXPAT_VERSION_PART}")
  41. endif()
  42. endif()
  43. endforeach()
  44. endforeach()
  45. endif ()
  46. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  47. FIND_PACKAGE_HANDLE_STANDARD_ARGS(EXPAT
  48. REQUIRED_VARS EXPAT_LIBRARY EXPAT_INCLUDE_DIR
  49. VERSION_VAR EXPAT_VERSION_STRING)
  50. # Copy the results to the output variables and target.
  51. if(EXPAT_FOUND)
  52. set(EXPAT_LIBRARIES ${EXPAT_LIBRARY})
  53. set(EXPAT_INCLUDE_DIRS ${EXPAT_INCLUDE_DIR})
  54. if(NOT TARGET EXPAT::EXPAT)
  55. add_library(EXPAT::EXPAT UNKNOWN IMPORTED)
  56. set_target_properties(EXPAT::EXPAT PROPERTIES
  57. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  58. IMPORTED_LOCATION "${EXPAT_LIBRARY}"
  59. INTERFACE_INCLUDE_DIRECTORIES "${EXPAT_INCLUDE_DIRS}")
  60. endif()
  61. endif()
  62. mark_as_advanced(EXPAT_INCLUDE_DIR EXPAT_LIBRARY)