NetdataYAML.cmake 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # SPDX-License-Identifier: GPL-3.0-or-later
  2. # Functions and macros for handling of libYAML
  3. # Handle bundling of libyaml.
  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_libyaml)
  11. include(FetchContent)
  12. include(NetdataFetchContentExtra)
  13. if(ENABLE_BUNDLED_LIBYAML)
  14. set(FETCHCONTENT_TRY_FIND_PACKAGE_MODE NEVER)
  15. endif()
  16. set(FETCHCONTENT_FULLY_DISCONNECTED Off)
  17. set(repo https://github.com/yaml/libyaml)
  18. set(tag 2c891fc7a770e8ba2fec34fc6b545c672beb37e6) # v0.2.5
  19. if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.28)
  20. FetchContent_Declare(yaml
  21. GIT_REPOSITORY ${repo}
  22. GIT_TAG ${tag}
  23. CMAKE_ARGS ${NETDATA_CMAKE_PROPAGATE_TOOLCHAIN_ARGS}
  24. EXCLUDE_FROM_ALL
  25. )
  26. else()
  27. FetchContent_Declare(yaml
  28. GIT_REPOSITORY ${repo}
  29. GIT_TAG ${tag}
  30. CMAKE_ARGS ${NETDATA_CMAKE_PROPAGATE_TOOLCHAIN_ARGS}
  31. )
  32. endif()
  33. FetchContent_MakeAvailable_NoInstall(yaml)
  34. endfunction()
  35. # Handle setup of libyaml for the build.
  36. #
  37. # This will attempt to find libyaml using pkg_check_modules. If it finds
  38. # a usable copy, that will be used. If not, it will bundle a vendored copy
  39. # as a sub-project.
  40. #
  41. # Irrespective of how libyaml is to be included, library names,
  42. # include directories, and compile definitions will be specified in the
  43. # NETDATA_YAML_* variables for later use.
  44. macro(netdata_detect_libyaml)
  45. set(HAVE_LIBYAML True)
  46. pkg_check_modules(YAML yaml-0.1)
  47. if(ENABLE_BUNDLED_LIBYAML OR NOT YAML_FOUND)
  48. netdata_bundle_libyaml()
  49. set(ENABLE_BUNDLED_LIBYAML True PARENT_SCOPE)
  50. set(NETDATA_YAML_LDFLAGS yaml)
  51. get_target_property(NETDATA_YAML_INCLUDE_DIRS yaml INTERFACE_INCLUDE_DIRECTORIES)
  52. get_target_property(NETDATA_YAML_CFLAGS_OTHER yaml INTERFACE_COMPILE_DEFINITIONS)
  53. else()
  54. set(NETDATA_YAML_LDFLAGS ${YAML_LDFLAGS})
  55. set(NETDATA_YAML_CFLAGS_OTHER ${YAML_CFLAGS_OTHER})
  56. set(NETDATA_YAML_INCLUDE_DIRS ${YAML_INCLUDE_DIRS})
  57. endif()
  58. endmacro()
  59. # Add libyaml as a public link dependency of the specified target.
  60. #
  61. # The specified target must already exist, and the netdata_detect_libyaml
  62. # macro must have already been run at least once for this to work correctly.
  63. function(netdata_add_libyaml_to_target _target)
  64. if(ENABLE_BUNDLED_LIBYAML)
  65. target_include_directories(${_target} BEFORE PUBLIC ${NETDATA_YAML_INCLUDE_DIRS})
  66. else()
  67. target_include_directories(${_target} PUBLIC ${NETDATA_YAML_INCLUDE_DIRS})
  68. endif()
  69. target_compile_options(${_target} PUBLIC ${NETDATA_YAML_CFLAGS_OTHER})
  70. target_link_libraries(${_target} PUBLIC ${NETDATA_YAML_LDFLAGS})
  71. endfunction()