NetdataJSONC.cmake 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. set(repo https://github.com/json-c/json-c)
  34. set(tag b4c371fa0cbc4dcbaccc359ce9e957a22988fb34) # json-c-0.17-20230812
  35. if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.28)
  36. FetchContent_Declare(json-c
  37. GIT_REPOSITORY ${repo}
  38. GIT_TAG ${tag}
  39. CMAKE_ARGS ${NETDATA_CMAKE_PROPAGATE_TOOLCHAIN_ARGS}
  40. EXCLUDE_FROM_ALL
  41. )
  42. else()
  43. FetchContent_Declare(json-c
  44. GIT_REPOSITORY ${repo}
  45. GIT_TAG ${tag}
  46. CMAKE_ARGS ${NETDATA_CMAKE_PROPAGATE_TOOLCHAIN_ARGS}
  47. )
  48. endif()
  49. FetchContent_MakeAvailable_NoInstall(json-c)
  50. message(STATUS "Finished preparing vendored copy of JSON-C")
  51. endfunction()
  52. # Handle setup of json-c for the build.
  53. #
  54. # This will attempt to find json-c using pkg_check_modules. If it finds
  55. # a usable copy, that will be used. If not, it will bundle a vendored copy
  56. # as a sub-project.
  57. #
  58. # Irrespective of how json-c is to be included, library names,
  59. # include directories, and compile definitions will be specified in the
  60. # NETDATA_JSONC_* variables for later use.
  61. macro(netdata_detect_jsonc)
  62. if(NOT ENABLE_BUNDLED_JSONC)
  63. pkg_check_modules(JSONC json-c>=0.14)
  64. endif()
  65. if(NOT JSONC_FOUND)
  66. set(ENABLE_BUNDLED_JSONC True)
  67. netdata_bundle_jsonc()
  68. set(NETDATA_JSONC_LDFLAGS json-c)
  69. set(NETDATA_JSONC_INCLUDE_DIRS ${PROJECT_BINARY_DIR}/include)
  70. get_target_property(NETDATA_JSONC_CFLAGS_OTHER json-c INTERFACE_COMPILE_DEFINITIONS)
  71. if(NETDATA_JSONC_CFLAGS_OTHER STREQUAL NETDATA_JSONC_CFLAGS_OTHER-NOTFOUND)
  72. set(NETDATA_JSONC_CFLAGS_OTHER "")
  73. endif()
  74. add_custom_command(
  75. OUTPUT ${PROJECT_BINARY_DIR}/include/json-c
  76. COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/include
  77. COMMAND ${CMAKE_COMMAND} -E create_symlink ${json-c_BINARY_DIR} ${PROJECT_BINARY_DIR}/include/json-c
  78. COMMENT "Create compatibility symlink for vendored JSON-C headers"
  79. DEPENDS json-c
  80. )
  81. add_custom_target(
  82. json-c-compat-link
  83. DEPENDS ${PROJECT_BINARY_DIR}/include/json-c
  84. )
  85. else()
  86. set(NETDATA_JSONC_LDFLAGS ${JSONC_LDFLAGS})
  87. set(NETDATA_JSONC_CFLAGS_OTHER ${JSONC_CFLAGS_OTHER})
  88. set(NETDATA_JSONC_INCLUDE_DIRS ${JSONC_INCLUDE_DIRS})
  89. add_custom_target(json-c-compat-link)
  90. endif()
  91. endmacro()
  92. # Add json-c as a public link dependency of the specified target.
  93. #
  94. # The specified target must already exist, and the netdata_detect_json-c
  95. # macro must have already been run at least once for this to work correctly.
  96. function(netdata_add_jsonc_to_target _target)
  97. if(ENABLE_BUNDLED_JSONC)
  98. target_include_directories(${_target} BEFORE PUBLIC ${NETDATA_JSONC_INCLUDE_DIRS})
  99. else()
  100. target_include_directories(${_target} PUBLIC ${NETDATA_JSONC_INCLUDE_DIRS})
  101. endif()
  102. target_compile_options(${_target} PUBLIC ${NETDATA_JSONC_CFLAGS_OTHER})
  103. target_link_libraries(${_target} PUBLIC ${NETDATA_JSONC_LDFLAGS})
  104. add_dependencies(${_target} json-c-compat-link)
  105. endfunction()