NetdataYAML.cmake 3.2 KB

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