[winpr,json] add jansson support

* Split JSON implementations in separate files
* Simplify JSON detection
* Add jansson implementation
This commit is contained in:
Armin Novak
2025-09-25 12:32:23 +02:00
parent 5313eb403d
commit 8f8cd89f69
9 changed files with 1256 additions and 745 deletions

View File

@@ -1,24 +0,0 @@
# - Try to find JSON-C
# Once done this will define
#
# JSONC_FOUND - JSON-C was found
# JSONC_INCLUDE_DIRS - JSON-C include directories
# JSONC_LIBRARIES - libraries needed for linking
find_package(PkgConfig)
if(PKG_CONFIG_FOUND)
pkg_check_modules(PC_JSONC json-c)
endif()
find_path(JSONC_INCLUDE_DIR NAMES json.h HINTS ${PC_JSONC_INCLUDE_DIRS} PATH_SUFFIXES json-c)
find_library(JSONC_LIBRARY NAMES json-c HINTS ${PC_JSONC_LIBRARY_DIRS})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(JSONC DEFAULT_MSG JSONC_LIBRARY JSONC_INCLUDE_DIR)
if(JSONC_FOUND)
set(JSONC_LIBRARIES ${JSONC_LIBRARY})
set(JSONC_INCLUDE_DIRS ${JSONC_INCLUDE_DIR})
endif()
mark_as_advanced(JSONC_LIBRARY JSONC_INCLUDE_DIR)

View File

@@ -1,59 +1,83 @@
function(detect_package name pkgconf cmake-target)
set(options REQUIRED)
cmake_parse_arguments(PARSE_ARGV 0 arg "${options}" "${oneValueArgs}" "${multiValueArgs}")
find_package(${name})
if(NOT ${name}_FOUND)
# Fallback detection:
# older ubuntu releases did not ship CMake or pkg-config files
# for ${pkgconf}. Be optimistic and try pkg-config and as last resort
# try manual detection
find_package(PkgConfig)
if(PKG_CONFIG_FOUND)
pkg_check_modules(${name} ${pkgconf})
if(${name}_FOUND)
# Create imported target cjson
add_library(${cmake-target} SHARED IMPORTED)
set_property(TARGET ${cmake-target} APPEND PROPERTY IMPORTED_CONFIGURATIONS NOCONFIG)
set_target_properties(
${cmake-target}
PROPERTIES IMPORTED_CONFIGURATIONS NOCONFIG IMPORTED_LINK_INTERFACE_LANGUAGES_NOCONFIG "C"
IMPORTED_LOCATION_NOCONFIG "${${name}_LINK_LIBRARIES}" IMPORTED_SONAME_NOCONFIG
"${${name}_LIBRARIES}"
INTERFACE_INCLUDE_DIRECTORIES "${${name}_INCLUDE_DIRS}"
)
endif()
endif()
endif()
if(NOT ${name}_FOUND AND arg_REQUIRED)
message(FATAL_ERROR "${name} was REQUIRED but not found")
endif()
if(${${name}_FOUND})
set(${name}_FOUND ${${name}_FOUND} CACHE INTERNAL "internal")
else()
unset(${name}_FOUND CACHE)
endif()
endfunction()
include(CMakeDependentOption)
option(WITH_JSON_DISABLED "Build without any JSON support" OFF)
cmake_dependent_option(
WITH_CJSON_REQUIRED "Build with cJSON (fail if not found)" OFF "NOT WITH_JSON_DISABLED;NOT WITH_JSONC_REQUIRED" OFF
WITH_CJSON_REQUIRED "Build with cJSON (fail if not found)" OFF
"NOT WITH_JSON_DISABLED;NOT WITH_JSONC_REQUIRED;NOT WITH_JANSSON_REQUIRED" OFF
)
cmake_dependent_option(
WITH_JSONC_REQUIRED "Build with JSON-C (fail if not found)" OFF "NOT WITH_JSON_DISABLED;NOT WITH_CJSON_REQUIRED" OFF
WITH_JSONC_REQUIRED "Build with JSON-C (fail if not found)" OFF
"NOT WITH_JSON_DISABLED;NOT WITH_CJSON_REQUIRED;NOT WITH_JANSSON_REQUIRED" OFF
)
cmake_dependent_option(
WITH_JANSSON_REQUIRED "Build with JANSSON (fail if not found)" OFF
"NOT WITH_JSON_DISABLED;NOT WITH_CJSON_REQUIRED;NOT WITH_JSONC_REQUIRED" OFF
)
# ensure no package is enabled before the detection starts
unset(json-c_FOUND CACHE)
unset(cJSON_FOUND CACHE)
unset(jansson_FOUND CACHE)
if(NOT WITH_JSON_DISABLED)
if(NOT WITH_JSONC_REQUIRED)
find_package(cJSON)
# Fallback detection:
# older ubuntu releases did not ship CMake or pkg-config files
# for cJSON. Be optimistic and try pkg-config and as last resort
# try manual detection
if(NOT CJSON_FOUND)
find_package(PkgConfig)
if(PKG_CONFIG_FOUND)
pkg_check_modules(CJSON libcjson)
endif()
if(NOT CJSON_FOUND)
find_path(CJSON_INCLUDE_DIRS NAMES cjson/cJSON.h)
find_library(CJSON_LIBRARIES NAMES cjson)
if(NOT "${CJSON_LIBRARIES}" STREQUAL "CJSON_LIBRARIES-NOTFOUND" AND NOT "${CJSON_INCLUDE_DIRS}" STREQUAL
"CJSON_INCLUDE_DIRS-NOTFOUND"
)
set(CJSON_FOUND ON)
endif()
endif()
endif()
if(WITH_CJSON_REQUIRED)
if(NOT CJSON_FOUND)
message(FATAL_ERROR "cJSON was requested but not found")
endif()
endif()
if(WITH_JANSSON_REQUIRED)
detect_package(jansson jansson jansson::jansson REQUIRED)
elseif(WITH_JSONC_REQUIRED)
detect_package(json-c json-c json-c::json-c REQUIRED)
elseif(WITH_CJSON_REQUIRED)
detect_package(cJSON libcjson cjson REQUIRED)
else()
unset(CJSON_FOUND)
# nothing required, so do a non fatal check for all
detect_package(jansson jansson jansson::jansson)
detect_package(json-c json-c json-c::json-c)
detect_package(cJSON libcjson cjson)
endif()
if(WITH_JSONC_REQUIRED)
find_package(JSONC REQUIRED)
elseif(NOT WITH_CJSON_REQUIRED)
find_package(JSONC)
endif()
if(WITH_CJSON_REQUIRED)
unset(JSONC_FOUND)
endif()
if(NOT JSONC_FOUND AND NOT CJSON_FOUND)
if(WITH_CJSON_REQUIRED OR WITH_JSONC_REQUIRED)
message(FATAL_ERROR "cJSON (${WITH_CJSON_REQUIRED}) or json-c (${WITH_JSONC_REQUIRED}) required but not found")
if(NOT json-c_FOUND AND NOT cJSON_FOUND AND NOT jansson_FOUND)
if(WITH_CJSON_REQUIRED OR WITH_JSONC_REQUIRED OR WITH_JANSSON_REQUIRED)
message(
FATAL_ERROR
"cJSON (${WITH_CJSON_REQUIRED}) or json-c (${WITH_JSONC_REQUIRED}) or jansson (${WITH_JANSSON_REQUIRED}) required but not found"
)
endif()
set(WITH_WINPR_JSON OFF CACHE INTERNAL "internal")
message("compiling without JSON support. Install cJSON or json-c to enable")