NetdataYAML.cmake 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. )
  23. FetchContent_MakeAvailable_NoInstall(yaml)
  24. endfunction()
  25. # Handle setup of libyaml for the build.
  26. #
  27. # This will attempt to find libyaml using pkg_check_modules. If it finds
  28. # a usable copy, that will be used. If not, it will bundle a vendored copy
  29. # as a sub-project.
  30. #
  31. # Irrespective of how libyaml is to be included, library names,
  32. # include directories, and compile definitions will be specified in the
  33. # NETDATA_YAML_* variables for later use.
  34. macro(netdata_detect_libyaml)
  35. set(HAVE_LIBYAML True)
  36. pkg_check_modules(YAML yaml-0.1)
  37. if(ENABLE_BUNDLED_LIBYAML OR NOT YAML_FOUND)
  38. netdata_bundle_libyaml()
  39. set(NETDATA_YAML_LDFLAGS yaml)
  40. get_target_property(NETDATA_YAML_INCLUDE_DIRS yaml INTERFACE_INCLUDE_DIRECTORIES)
  41. get_target_property(NETDATA_YAML_CFLAGS_OTHER yaml INTERFACE_COMPILE_DEFINITIONS)
  42. else()
  43. set(NETDATA_YAML_LDFLAGS ${YAML_LDFLAGS})
  44. set(NETDATA_YAML_CFLAGS_OTHER ${YAML_CFLAGS_OTHER})
  45. set(NETDATA_YAML_INCLUDE_DIRS ${YAML_INCLUDE_DIRS})
  46. endif()
  47. endmacro()
  48. # Add libyaml as a public link dependency of the specified target.
  49. #
  50. # The specified target must already exist, and the netdata_detect_libyaml
  51. # macro must have already been run at least once for this to work correctly.
  52. function(netdata_add_libyaml_to_target _target)
  53. target_include_directories(${_target} PUBLIC ${NETDATA_YAML_INCLUDE_DIRS})
  54. target_compile_definitions(${_target} PUBLIC ${NETDATA_YAML_CFLAGS_OTHER})
  55. target_link_libraries(${_target} PUBLIC ${NETDATA_YAML_LDFLAGS})
  56. endfunction()