From c44052f12ff1f373ddfa5f582fbae5a33da46545 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Thu, 25 Sep 2025 15:35:36 +0200 Subject: [PATCH] [cmake,json] add second fallback If both, CMake and pkg-config fail, fall back to find_path and find_library detection --- cmake/JsonDetect.cmake | 89 +++++++++++++++++++++++++++++++----------- 1 file changed, 66 insertions(+), 23 deletions(-) diff --git a/cmake/JsonDetect.cmake b/cmake/JsonDetect.cmake index 4bbb5e8de..d89c0de16 100644 --- a/cmake/JsonDetect.cmake +++ b/cmake/JsonDetect.cmake @@ -1,29 +1,55 @@ -function(detect_package name pkgconf cmake-target) +function(detect_package name) set(options REQUIRED) + set(oneValueArgs HEADER LIBRARY PKGCONFIG CMAKE) 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 + # some distributions only ship pkg-config and skip CMake config files. 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}" - ) + if(arg_PKGCONFIG) + set(module ${arg_PKGCONFIG}) + else() + set(module ${name}) endif() + pkg_check_modules(${name} ${module}) + endif() + + # No CMake nor pkg-config files available, try manual detection + if(NOT ${name}_FOUND) + find_library( + ${name}_LIBRARY NAMES ${arg_LIBRARY} ${arg_PKGCONFIG} ${arg_CMAKE} ${name} ${arg_CMAKE} + PATH_SUFFIXES ${name} ${arg_PKGCONFIG} ${arg_CMAKE} + ) + + find_path(${name}_HEADER NAMES ${arg_HEADER} ${name}.h ${arg_PKGCONFIG}.h ${arg_CMAKE}.h + PATH_SUFFIXES ${name} ${arg_PKGCONFIG} ${arg_CMAKE} + ) + + if(NOT ${name}_LIBRARY STREQUAL "${name}_LIBRARY-NOTFOUND" AND NOT ${name}_HEADER STREQUAL + "${name}_HEADER-NOTFOUND" + ) + set(${name}_LINK_LIBRARIES "${${name}_LIBRARY}") + set(${name}_INCLUDE_DIRS "${${name}_HEADER}") + get_filename_component(${name}_LIB_FILE "${${name}_LIBRARY}" NAME_WE) + set(regex "^${CMAKE_SHARED_LIBRARY_SUFFIX}") + string(REGEX REPLACE ${regex} ${name}_LIBRARIES "${${name}_LIB_FILE}") + endif() + endif() + + if(${name}_FOUND) + # Create imported target cjson + add_library(${arg_CMAKE} SHARED IMPORTED) + set_property(TARGET ${arg_CMAKE} APPEND PROPERTY IMPORTED_CONFIGURATIONS NOCONFIG) + set_target_properties( + ${arg_CMAKE} + 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() @@ -60,16 +86,33 @@ unset(cJSON_FOUND CACHE) unset(jansson_FOUND CACHE) if(NOT WITH_JSON_DISABLED) if(WITH_JANSSON_REQUIRED) - detect_package(jansson jansson jansson::jansson REQUIRED) + detect_package(jansson CMAKE jansson::jansson HEADER jansson.h REQUIRED) elseif(WITH_JSONC_REQUIRED) - detect_package(json-c json-c json-c::json-c REQUIRED) + detect_package(json-c CMAKE json-c::json-c HEADER json-c/json.h REQUIRED) elseif(WITH_CJSON_REQUIRED) - detect_package(cJSON libcjson cjson REQUIRED) + detect_package( + cJSON + PKGCONFIG + libcjson + CMAKE + cjson + HEADER + cjson/cJSON.h + REQUIRED + ) else() # 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) + detect_package(jansson CMAKE jansson::jansson HEADER jansson.h) + detect_package(json-c CMAKE json-c::json-c HEADER json-c/json.h) + detect_package( + cJSON + PKGCONFIG + libcjson + CMAKE + cjson + HEADER + cjson/cJSON.h + ) endif() if(NOT json-c_FOUND AND NOT cJSON_FOUND AND NOT jansson_FOUND)