NetdataYAML.cmake 2.8 KB

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