CMake Nightly Date Stamp
[kiteware-cmake.git] / Source / Modules / FindJsonCpp.cmake
blob1951b6154afcf7a6c3a5d6d7a2b314c1b66e3d4e
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
5 FindJsonCpp
6 -----------
8 Find JsonCpp includes and library.
10 Imported Targets
11 ^^^^^^^^^^^^^^^^
13 An :ref:`imported target <Imported targets>` named
14 ``JsonCpp::JsonCpp`` is provided if JsonCpp has been found.
16 Result Variables
17 ^^^^^^^^^^^^^^^^
19 This module defines the following variables:
21 ``JsonCpp_FOUND``
22   True if JsonCpp was found, false otherwise.
23 ``JsonCpp_INCLUDE_DIRS``
24   Include directories needed to include JsonCpp headers.
25 ``JsonCpp_LIBRARIES``
26   Libraries needed to link to JsonCpp.
27 ``JsonCpp_VERSION_STRING``
28   The version of JsonCpp found.
29   May not be set for JsonCpp versions prior to 1.0.
30 ``JsonCpp_VERSION_MAJOR``
31   The major version of JsonCpp.
32 ``JsonCpp_VERSION_MINOR``
33   The minor version of JsonCpp.
34 ``JsonCpp_VERSION_PATCH``
35   The patch version of JsonCpp.
37 Cache Variables
38 ^^^^^^^^^^^^^^^
40 This module uses the following cache variables:
42 ``JsonCpp_LIBRARY``
43   The location of the JsonCpp library file.
44 ``JsonCpp_INCLUDE_DIR``
45   The location of the JsonCpp include directory containing ``json/json.h``.
47 The cache variables should not be used by project code.
48 They may be set by end users to point at JsonCpp components.
49 #]=======================================================================]
51 #-----------------------------------------------------------------------------
52 find_library(JsonCpp_LIBRARY
53   NAMES jsoncpp
54   )
55 mark_as_advanced(JsonCpp_LIBRARY)
57 find_path(JsonCpp_INCLUDE_DIR
58   NAMES json/json.h
59   PATH_SUFFIXES jsoncpp
60   )
61 mark_as_advanced(JsonCpp_INCLUDE_DIR)
63 #-----------------------------------------------------------------------------
64 # Extract version number if possible.
65 set(_JsonCpp_H_REGEX "^#[ \t]*define[ \t]+JSONCPP_VERSION_STRING[ \t]+\"(([0-9]+)\\.([0-9]+)\\.([0-9]+)[^\"]*)\".*$")
66 if(JsonCpp_INCLUDE_DIR AND EXISTS "${JsonCpp_INCLUDE_DIR}/json/version.h")
67   file(STRINGS "${JsonCpp_INCLUDE_DIR}/json/version.h" _JsonCpp_H REGEX "${_JsonCpp_H_REGEX}")
68 else()
69   set(_JsonCpp_H "")
70 endif()
71 if(_JsonCpp_H MATCHES "${_JsonCpp_H_REGEX}")
72   set(JsonCpp_VERSION_STRING "${CMAKE_MATCH_1}")
73   set(JsonCpp_VERSION_MAJOR "${CMAKE_MATCH_2}")
74   set(JsonCpp_VERSION_MINOR "${CMAKE_MATCH_3}")
75   set(JsonCpp_VERSION_PATCH "${CMAKE_MATCH_4}")
76 else()
77   set(JsonCpp_VERSION_STRING "")
78   set(JsonCpp_VERSION_MAJOR "")
79   set(JsonCpp_VERSION_MINOR "")
80   set(JsonCpp_VERSION_PATCH "")
81 endif()
82 unset(_JsonCpp_H_REGEX)
83 unset(_JsonCpp_H)
85 #-----------------------------------------------------------------------------
86 include(${CMAKE_CURRENT_LIST_DIR}/../../Modules/FindPackageHandleStandardArgs.cmake)
87 FIND_PACKAGE_HANDLE_STANDARD_ARGS(JsonCpp
88   FOUND_VAR JsonCpp_FOUND
89   REQUIRED_VARS JsonCpp_LIBRARY JsonCpp_INCLUDE_DIR
90   VERSION_VAR JsonCpp_VERSION_STRING
91   )
92 set(JSONCPP_FOUND ${JsonCpp_FOUND})
94 #-----------------------------------------------------------------------------
95 # Provide documented result variables and targets.
96 if(JsonCpp_FOUND)
97   set(JsonCpp_INCLUDE_DIRS ${JsonCpp_INCLUDE_DIR})
98   set(JsonCpp_LIBRARIES ${JsonCpp_LIBRARY})
99   if(NOT TARGET JsonCpp::JsonCpp)
100     add_library(JsonCpp::JsonCpp UNKNOWN IMPORTED)
101     set_target_properties(JsonCpp::JsonCpp PROPERTIES
102       IMPORTED_LOCATION "${JsonCpp_LIBRARY}"
103       INTERFACE_INCLUDE_DIRECTORIES "${JsonCpp_INCLUDE_DIRS}"
104       IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
105       )
106   endif()
107 endif()