summaryrefslogtreecommitdiffstats
path: root/cmake/Modules/FindJSON-C.cmake
blob: e66a01165775db7203fc49c45b215179e84dc6b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Copyright (c) 2018 Ribose Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

#.rst:
# FindJSON-C
# -----------
#
# Find the json-c library.
#
# IMPORTED Targets
# ^^^^^^^^^^^^^^^^
#
# This module defines :prop_tgt:`IMPORTED` targets:
#
# ``JSON-C::JSON-C``
#   The json-c library, if found.
#
# Result variables
# ^^^^^^^^^^^^^^^^
#
# This module defines the following variables:
#
# ::
#
#   JSON-C_FOUND          - true if the headers and library were found
#   JSON-C_INCLUDE_DIRS   - where to find headers
#   JSON-C_LIBRARIES      - list of libraries to link
#   JSON-C_VERSION        - library version that was found, if any

# use pkg-config to get the directories and then use these values
# in the find_path() and find_library() calls
find_package(PkgConfig QUIET)
pkg_check_modules(PC_JSON-C QUIET json-c)

# RHEL-based systems may have json-c12
if (NOT PC_JSON-C_FOUND)
  pkg_check_modules(PC_JSON-C QUIET json-c12)
endif()

# find the headers
find_path(JSON-C_INCLUDE_DIR
  NAMES json_c_version.h
  HINTS
    ${PC_JSON-C_INCLUDEDIR}
    ${PC_JSON-C_INCLUDE_DIRS}
  PATH_SUFFIXES json-c
)

# find the library
find_library(JSON-C_LIBRARY
  NAMES json-c libjson-c json-c12 libjson-c12
  HINTS
    ${PC_JSON-C_LIBDIR}
    ${PC_JSON-C_LIBRARY_DIRS}
)

# determine the version
if(PC_JSON-C_VERSION)
    set(JSON-C_VERSION ${PC_JSON-C_VERSION})
elseif(JSON-C_INCLUDE_DIR AND EXISTS "${JSON-C_INCLUDE_DIR}/json_c_version.h")
    file(STRINGS "${JSON-C_INCLUDE_DIR}/json_c_version.h" _json-c_version_h
      REGEX "^#define[\t ]+JSON_C_VERSION[\t ]+\"[^\"]*\"$")

    string(REGEX REPLACE ".*#define[\t ]+JSON_C_VERSION[\t ]+\"([^\"]*)\".*"
      "\\1" _json-c_version_str "${_json-c_version_h}")
    set(JSON-C_VERSION "${_json-c_version_str}"
                       CACHE INTERNAL "The version of json-c which was detected")
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(JSON-C
  REQUIRED_VARS JSON-C_LIBRARY JSON-C_INCLUDE_DIR JSON-C_VERSION
  VERSION_VAR JSON-C_VERSION
)

if (JSON-C_FOUND)
  set(JSON-C_INCLUDE_DIRS ${JSON-C_INCLUDE_DIR} ${PC_JSON-C_INCLUDE_DIRS})
  set(JSON-C_LIBRARIES ${JSON-C_LIBRARY})
endif()

if (JSON-C_FOUND AND NOT TARGET JSON-C::JSON-C)
  # create the new library target
  add_library(JSON-C::JSON-C UNKNOWN IMPORTED)
  # set the required include dirs for the target
  if (JSON-C_INCLUDE_DIRS)
    set_target_properties(JSON-C::JSON-C
      PROPERTIES
        INTERFACE_INCLUDE_DIRECTORIES "${JSON-C_INCLUDE_DIRS}"
    )
  endif()
  # set the required libraries for the target
  if (EXISTS "${JSON-C_LIBRARY}")
    set_target_properties(JSON-C::JSON-C
      PROPERTIES
        IMPORTED_LINK_INTERFACE_LANGUAGES "C"
        IMPORTED_LOCATION "${JSON-C_LIBRARY}"
    )
  endif()
endif()

mark_as_advanced(JSON-C_INCLUDE_DIR JSON-C_LIBRARY)