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