NetdataJSONC.cmake 4.4 KB

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