| 1 | # SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | # Functions and macros for handling of JSON-C |
| 3 | |
| 4 | # Handle bundling of json-c. |
| 5 | # |
| 6 | # This pulls it in as a sub-project using FetchContent functionality. |
| 7 | # |
| 8 | # This needs to be a function and not a macro for variable scoping |
| 9 | # reasons. All the things we care about from the sub-project are exposed |
| 10 | # as targets, which are globally scoped and not function scoped. |
| 11 | function(netdata_bundle_jsonc) |
| 12 | include(FetchContent) |
| 13 | include(NetdataFetchContentExtra) |
| 14 | |
| 15 | message(STATUS "Preparing vendored copy of JSON-C") |
| 16 | |
| 17 | if(ENABLE_BUNDLED_JSONC) |
| 18 | set(FETCHCONTENT_TRY_FIND_PACKAGE_MODE NEVER) |
| 19 | endif() |
| 20 | |
| 21 | set(FETCHCONTENT_FULLY_DISCONNECTED Off) |
| 22 | |
| 23 | # JSON-C supports older versions of CMake than we do, so set |
| 24 | # the correct values for the few policies we actually need. |
| 25 | set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) |
| 26 | |
| 27 | # JSON-C's build system does string comparisons against option |
| 28 | # values instead of treating them as booleans, so we need to use |
| 29 | # proper strings for option values instead of just setting them |
| 30 | # to true or false. |
| 31 | set(DISABLE_BSYMBOLIC ON) |
| 32 | set(DISABLE_WERROR ON) |
| 33 | set(DISABLE_EXTRA_LIBS ON) |
| 34 | set(BUILD_SHARED_LIBS OFF) |
| 35 | set(BUILD_STATIC_LIBS ON) |
| 36 | set(BUILD_APPS OFF) |
| 37 | |
| 38 | set(repo https://github.com/json-c/json-c) |
| 39 | set(tag b4c371fa0cbc4dcbaccc359ce9e957a22988fb34) # json-c-0.17-20230812 |
| 40 | |
| 41 | if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.28) |
| 42 | FetchContent_Declare(json-c |
| 43 | GIT_REPOSITORY ${repo} |
| 44 | GIT_TAG ${tag} |
| 45 | CMAKE_ARGS ${NETDATA_CMAKE_PROPAGATE_TOOLCHAIN_ARGS} |
| 46 | EXCLUDE_FROM_ALL |
| 47 | ) |
| 48 | else() |
| 49 | FetchContent_Declare(json-c |
| 50 | GIT_REPOSITORY ${repo} |
| 51 | GIT_TAG ${tag} |
| 52 | CMAKE_ARGS ${NETDATA_CMAKE_PROPAGATE_TOOLCHAIN_ARGS} |
| 53 | ) |
| 54 | endif() |
| 55 | |
| 56 | FetchContent_MakeAvailable_NoInstall(json-c) |
| 57 | |
| 58 | message(STATUS "Finished preparing vendored copy of JSON-C") |
| 59 | endfunction() |
| 60 | |
| 61 | # Handle setup of json-c for the build. |
| 62 | # |
| 63 | # This will attempt to find json-c using pkg_check_modules. If it finds |
| 64 | # a usable copy, that will be used. If not, it will bundle a vendored copy |
| 65 | # as a sub-project. |
| 66 | # |
| 67 | # Irrespective of how json-c is to be included, library names, |
| 68 | # include directories, and compile definitions will be specified in the |
| 69 | # NETDATA_JSONC_* variables for later use. |
| 70 | macro(netdata_detect_jsonc) |
| 71 | if(NOT ENABLE_BUNDLED_JSONC) |
| 72 | pkg_check_modules(JSONC json-c>=0.14) |
| 73 | endif() |
| 74 | |
| 75 | if(NOT JSONC_FOUND) |
| 76 | set(ENABLE_BUNDLED_JSONC True) |
| 77 | netdata_bundle_jsonc() |
| 78 | set(NETDATA_JSONC_LDFLAGS json-c) |
| 79 | set(NETDATA_JSONC_INCLUDE_DIRS ${PROJECT_BINARY_DIR}/include) |
| 80 | get_target_property(NETDATA_JSONC_CFLAGS_OTHER json-c INTERFACE_COMPILE_DEFINITIONS) |
| 81 | |
| 82 | if(NETDATA_JSONC_CFLAGS_OTHER STREQUAL NETDATA_JSONC_CFLAGS_OTHER-NOTFOUND) |
| 83 | set(NETDATA_JSONC_CFLAGS_OTHER "") |
| 84 | endif() |
| 85 | |
| 86 | add_custom_command( |
| 87 | OUTPUT ${PROJECT_BINARY_DIR}/include/json-c |
| 88 | COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/include |
| 89 | COMMAND ${CMAKE_COMMAND} -E create_symlink ${json-c_BINARY_DIR} ${PROJECT_BINARY_DIR}/include/json-c |
| 90 | COMMENT "Create compatibility symlink for vendored JSON-C headers" |
| 91 | DEPENDS json-c |
| 92 | ) |
| 93 | add_custom_target( |
| 94 | json-c-compat-link |
| 95 | DEPENDS ${PROJECT_BINARY_DIR}/include/json-c |
| 96 | ) |
| 97 | else() |
| 98 | set(NETDATA_JSONC_LDFLAGS ${JSONC_LDFLAGS}) |
| 99 | set(NETDATA_JSONC_CFLAGS_OTHER ${JSONC_CFLAGS_OTHER}) |
| 100 | set(NETDATA_JSONC_INCLUDE_DIRS ${JSONC_INCLUDE_DIRS}) |
| 101 | add_custom_target(json-c-compat-link) |
| 102 | endif() |
| 103 | endmacro() |
| 104 | |
| 105 | # Add json-c as a public link dependency of the specified target. |
| 106 | # |
| 107 | # The specified target must already exist, and the netdata_detect_json-c |
| 108 | # macro must have already been run at least once for this to work correctly. |
| 109 | function(netdata_add_jsonc_to_target _target) |
| 110 | if(ENABLE_BUNDLED_JSONC) |
| 111 | target_include_directories(${_target} BEFORE PUBLIC ${NETDATA_JSONC_INCLUDE_DIRS}) |
| 112 | else() |
| 113 | target_include_directories(${_target} PUBLIC ${NETDATA_JSONC_INCLUDE_DIRS}) |
| 114 | endif() |
| 115 | target_compile_options(${_target} PUBLIC ${NETDATA_JSONC_CFLAGS_OTHER}) |
| 116 | target_link_libraries(${_target} PUBLIC ${NETDATA_JSONC_LDFLAGS}) |
| 117 | add_dependencies(${_target} json-c-compat-link) |
| 118 | endfunction() |