Skip to content

Commit

Permalink
FindcJSON: Update to play nicer with cJSON's cmake module as found in…
Browse files Browse the repository at this point in the history
… vcpkg.
  • Loading branch information
rpavlik committed May 23, 2022
1 parent 1fe6e87 commit 3a60283
Showing 1 changed file with 43 additions and 21 deletions.
64 changes: 43 additions & 21 deletions FindcJSON.cmake
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Copyright 2019-2020, Collabora, Ltd.
# Copyright 2019-2022, Collabora, Ltd.
#
# SPDX-License-Identifier: BSL-1.0
#
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
#
# Original Author:
# 2019-2020 Ryan Pavlik <[email protected]>
# 2019-2022 Ryan Pavlik <[email protected]>

#.rst:
# FindcJSON
Expand All @@ -32,31 +34,51 @@ set(CJSON_ROOT_DIR
"${CJSON_ROOT_DIR}"
CACHE PATH "Root to search for cJSON")

find_path(
CJSON_INCLUDE_DIR
NAMES cjson/cJSON.h
PATHS ${CJSON_ROOT_DIR}
PATH_SUFFIXES include)
find_library(
CJSON_LIBRARY
NAMES cjson
PATHS ${CJSON_ROOT_DIR}
PATH_SUFFIXES lib)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(cJSON REQUIRED_VARS CJSON_INCLUDE_DIR
CJSON_LIBRARY)

# Check for CMake config first.
find_package(cJSON QUIET CONFIG)
if(cJSON_FOUND AND TARGET cjson)
# Found config, let's prefer it.
find_package_handle_standard_args(cJSON CONFIG_MODE)
set(CJSON_LIBRARY cjson)

else()
# Manually find
find_path(
CJSON_INCLUDE_DIR
NAMES cjson/cJSON.h
PATHS ${CJSON_ROOT_DIR}
PATH_SUFFIXES include)
find_library(
CJSON_LIBRARY
NAMES cjson
PATHS ${CJSON_ROOT_DIR}
PATH_SUFFIXES lib)

find_package_handle_standard_args(cJSON REQUIRED_VARS CJSON_INCLUDE_DIR
CJSON_LIBRARY)
endif()

if(CJSON_FOUND)
set(CJSON_INCLUDE_DIRS "${CJSON_INCLUDE_DIR}")
set(CJSON_LIBRARIES "${CJSON_LIBRARY}")
if(NOT TARGET cJSON::cJSON)
add_library(cJSON::cJSON UNKNOWN IMPORTED)
if(TARGET "${CJSON_LIBRARY}")
# Alias if we found the config file
add_library(cJSON::cJSON ALIAS cjson)
else()
add_library(cJSON::cJSON UNKNOWN IMPORTED)
set_target_properties(
cJSON::cJSON PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
"${CJSON_INCLUDE_DIR}")
set_target_properties(
cJSON::cJSON PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${CJSON_LIBRARY}")

endif()
endif()
set_target_properties(cJSON::cJSON PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
"${CJSON_INCLUDE_DIR}")
set_target_properties(
cJSON::cJSON PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${CJSON_LIBRARY}")
mark_as_advanced(CJSON_INCLUDE_DIR CJSON_LIBRARY)
endif()

mark_as_advanced(CJSON_ROOT_DIR)

0 comments on commit 3a60283

Please sign in to comment.