NetdataJSONC.cmake 4.1 KB

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