NetdataJSONC.cmake 4.8 KB

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