FindJsonCpp.cmake 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. FindJsonCpp
  5. -----------
  6. Find JsonCpp includes and library.
  7. Imported Targets
  8. ^^^^^^^^^^^^^^^^
  9. An :ref:`imported target <Imported targets>` named
  10. ``JsonCpp::JsonCpp`` is provided if JsonCpp has been found.
  11. Result Variables
  12. ^^^^^^^^^^^^^^^^
  13. This module defines the following variables:
  14. ``JsonCpp_FOUND``
  15. True if JsonCpp was found, false otherwise.
  16. ``JsonCpp_INCLUDE_DIRS``
  17. Include directories needed to include JsonCpp headers.
  18. ``JsonCpp_LIBRARIES``
  19. Libraries needed to link to JsonCpp.
  20. ``JsonCpp_VERSION_STRING``
  21. The version of JsonCpp found.
  22. May not be set for JsonCpp versions prior to 1.0.
  23. ``JsonCpp_VERSION_MAJOR``
  24. The major version of JsonCpp.
  25. ``JsonCpp_VERSION_MINOR``
  26. The minor version of JsonCpp.
  27. ``JsonCpp_VERSION_PATCH``
  28. The patch version of JsonCpp.
  29. Cache Variables
  30. ^^^^^^^^^^^^^^^
  31. This module uses the following cache variables:
  32. ``JsonCpp_LIBRARY``
  33. The location of the JsonCpp library file.
  34. ``JsonCpp_INCLUDE_DIR``
  35. The location of the JsonCpp include directory containing ``json/json.h``.
  36. The cache variables should not be used by project code.
  37. They may be set by end users to point at JsonCpp components.
  38. #]=======================================================================]
  39. #-----------------------------------------------------------------------------
  40. find_library(JsonCpp_LIBRARY
  41. NAMES jsoncpp
  42. )
  43. mark_as_advanced(JsonCpp_LIBRARY)
  44. find_path(JsonCpp_INCLUDE_DIR
  45. NAMES json/json.h
  46. PATH_SUFFIXES jsoncpp
  47. )
  48. mark_as_advanced(JsonCpp_INCLUDE_DIR)
  49. #-----------------------------------------------------------------------------
  50. # Extract version number if possible.
  51. set(_JsonCpp_H_REGEX "^#[ \t]*define[ \t]+JSONCPP_VERSION_STRING[ \t]+\"(([0-9]+)\\.([0-9]+)\\.([0-9]+)[^\"]*)\".*$")
  52. if(JsonCpp_INCLUDE_DIR AND EXISTS "${JsonCpp_INCLUDE_DIR}/json/version.h")
  53. file(STRINGS "${JsonCpp_INCLUDE_DIR}/json/version.h" _JsonCpp_H REGEX "${_JsonCpp_H_REGEX}")
  54. else()
  55. set(_JsonCpp_H "")
  56. endif()
  57. if(_JsonCpp_H MATCHES "${_JsonCpp_H_REGEX}")
  58. set(JsonCpp_VERSION_STRING "${CMAKE_MATCH_1}")
  59. set(JsonCpp_VERSION_MAJOR "${CMAKE_MATCH_2}")
  60. set(JsonCpp_VERSION_MINOR "${CMAKE_MATCH_3}")
  61. set(JsonCpp_VERSION_PATCH "${CMAKE_MATCH_4}")
  62. else()
  63. set(JsonCpp_VERSION_STRING "")
  64. set(JsonCpp_VERSION_MAJOR "")
  65. set(JsonCpp_VERSION_MINOR "")
  66. set(JsonCpp_VERSION_PATCH "")
  67. endif()
  68. unset(_JsonCpp_H_REGEX)
  69. unset(_JsonCpp_H)
  70. #-----------------------------------------------------------------------------
  71. include(${CMAKE_CURRENT_LIST_DIR}/../../Modules/FindPackageHandleStandardArgs.cmake)
  72. FIND_PACKAGE_HANDLE_STANDARD_ARGS(JsonCpp
  73. FOUND_VAR JsonCpp_FOUND
  74. REQUIRED_VARS JsonCpp_LIBRARY JsonCpp_INCLUDE_DIR
  75. VERSION_VAR JsonCpp_VERSION_STRING
  76. )
  77. set(JSONCPP_FOUND ${JsonCpp_FOUND})
  78. #-----------------------------------------------------------------------------
  79. # Provide documented result variables and targets.
  80. if(JsonCpp_FOUND)
  81. set(JsonCpp_INCLUDE_DIRS ${JsonCpp_INCLUDE_DIR})
  82. set(JsonCpp_LIBRARIES ${JsonCpp_LIBRARY})
  83. if(NOT TARGET JsonCpp::JsonCpp)
  84. add_library(JsonCpp::JsonCpp UNKNOWN IMPORTED)
  85. set_target_properties(JsonCpp::JsonCpp PROPERTIES
  86. IMPORTED_LOCATION "${JsonCpp_LIBRARY}"
  87. INTERFACE_INCLUDE_DIRECTORIES "${JsonCpp_INCLUDE_DIRS}"
  88. IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
  89. )
  90. endif()
  91. endif()