Move bundling of JSON-C to CMake. (#17207)
* Move bundling of JSON-C to CMake. * Fix JSON-C build option passing. * Force CMake policy 77 to new behavior for JSON-C build. This is required to ensure that options we set are propagated correctly into the sub-project. * Fix include path selection and handling for bundled JSON-C. * Fix include ordering again. * Fixup JSON header compat link handling.
Austin S. Hemmelgarn committed
Apr 10, 2024 at 08:30 UTC
0f5b137471a7a2f56cb2700394b0cd198c8787e1
5 files changed
+106
-87
CMakeLists.txt
+4
-18
@@ -271,15 +271,17 @@ if(NOT HAVE_LOG10)
271
endif()
272
273
#
274
-# Custom modules
274
+# Custom Modules
275
#
276
277
+include(NetdataJSONC)
278
include(NetdataYAML)
279
280
#
281
# Checks from custom modules
282
#
283
284
+netdata_detect_jsonc()
285
netdata_detect_libyaml()
286
287
#
@@ -1511,23 +1513,7 @@ endif()
1513
# judy
1514
target_link_libraries(libnetdata PUBLIC judy)
1515
1514
-# json-c
1515
-if(ENABLE_BUNDLED_JSONC)
1516
- add_library(jsonc STATIC IMPORTED)
1517
- set_property(TARGET jsonc PROPERTY
1518
- IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/externaldeps/jsonc/libjson-c.a")
1519
-
1520
- target_include_directories(libnetdata BEFORE PUBLIC "${CMAKE_SOURCE_DIR}/externaldeps/jsonc")
1521
- target_link_libraries(libnetdata PUBLIC jsonc)
1522
-else()
1523
- pkg_check_modules(JSONC REQUIRED json-c)
1524
- target_include_directories(libnetdata BEFORE PUBLIC ${JSONC_INCLUDE_DIRS})
1525
- target_compile_definitions(libnetdata PUBLIC ${JSONC_CFLAGS_OTHER})
1526
- target_link_libraries(libnetdata PUBLIC ${JSONC_LDFLAGS})
1527
-endif()
1528
-
1529
-# message(FATAL_ERROR "jsonc libraries: ${JSONC_LIBRARIES}")
1530
-# message(FATAL_ERROR "jsonc ldflags: ${JSONC_LDFLAGS}")
1516
+netdata_add_jsonc_to_target(libnetdata)
1517
1518
netdata_add_libyaml_to_target(libnetdata)
1519
netdata-installer.sh
-67
@@ -556,73 +556,6 @@ fi
556
557
trap build_error EXIT
558
559
-# -----------------------------------------------------------------------------
560
-build_jsonc() {
561
- env_cmd=''
562
-
563
- if [ -z "${DONT_SCRUB_CFLAGS_EVEN_THOUGH_IT_MAY_BREAK_THINGS}" ]; then
564
- env_cmd="env CFLAGS='-fPIC -pipe' CXXFLAGS='-fPIC -pipe' LDFLAGS="
565
- fi
566
-
567
- cd "${1}" > /dev/null || exit 1
568
- run eval "${env_cmd} ${cmake} ${CMAKE_OPTS} -DBUILD_SHARED_LIBS=OFF -DDISABLE_WERROR=On ."
569
- run eval "${env_cmd} ${cmake} --build . --parallel ${JOBS} -- ${BUILD_OPTS}"
570
- cd - > /dev/null || return 1
571
-}
572
-
573
-copy_jsonc() {
574
- target_dir="${PWD}/externaldeps/jsonc"
575
-
576
- run mkdir -p "${target_dir}" "${target_dir}/json-c" || return 1
577
-
578
- run cp "${1}/libjson-c.a" "${target_dir}/libjson-c.a" || return 1
579
- # shellcheck disable=SC2086
580
- run cp ${1}/*.h "${target_dir}/json-c" || return 1
581
-}
582
-
583
-bundle_jsonc() {
584
- # If --build-json-c flag or not json-c on system, then bundle our own json-c
585
- if [ -z "${NETDATA_BUILD_JSON_C}" ] && pkg-config json-c; then
586
- NETDATA_BUILD_JSON_C=0
587
- return 0
588
- fi
589
-
590
- [ -n "${GITHUB_ACTIONS}" ] && echo "::group::Bundling JSON-C."
591
-
592
- progress "Prepare JSON-C"
593
-
594
- JSONC_PACKAGE_VERSION="$(cat packaging/jsonc.version)"
595
-
596
- tmp="$(mktemp -d -t netdata-jsonc-XXXXXX)"
597
- JSONC_PACKAGE_BASENAME="json-c-${JSONC_PACKAGE_VERSION}.tar.gz"
598
-
599
- if fetch_and_verify "jsonc" \
600
- "https://github.com/json-c/json-c/archive/${JSONC_PACKAGE_BASENAME}" \
601
- "${JSONC_PACKAGE_BASENAME}" \
602
- "${tmp}" \
603
- "${NETDATA_LOCAL_TARBALL_OVERRIDE_JSONC}"; then
604
- if run tar --no-same-owner -xf "${tmp}/${JSONC_PACKAGE_BASENAME}" -C "${tmp}" &&
605
- build_jsonc "${tmp}/json-c-json-c-${JSONC_PACKAGE_VERSION}" &&
606
- copy_jsonc "${tmp}/json-c-json-c-${JSONC_PACKAGE_VERSION}" &&
607
- rm -rf "${tmp}"; then
608
- run_ok "JSON-C built and prepared."
609
- NETDATA_BUILD_JSON_C=1
610
- else
611
- run_failed "Failed to build JSON-C, Netdata Cloud support will be disabled in this build."
612
- NETDATA_BUILD_JSON_C=0
613
- ENABLE_CLOUD=0
614
- fi
615
- else
616
- run_failed "Unable to fetch sources for JSON-C, Netdata Cloud support will be disabled in this build."
617
- NETDATA_BUILD_JSON_C=0
618
- ENABLE_CLOUD=0
619
- fi
620
-
621
- [ -n "${GITHUB_ACTIONS}" ] && echo "::endgroup::"
622
-}
623
-
624
-bundle_jsonc
625
-
559
# -----------------------------------------------------------------------------
560
561
get_kernel_version() {
packaging/cmake/Modules/NetdataJSONC.cmake
new
+102
@@ -0,0 +1,102 @@
1
+# Functions and macros for handling of JSON-C
2
+#
3
+# Copyright (c) 2024 Netdata Inc.
4
+# SPDX-License-Identifier: GPL-3.0-or-later
5
+
6
+# Handle bundling of json-c.
7
+#
8
+# This pulls it in as a sub-project using FetchContent functionality.
9
+#
10
+# This needs to be a function and not a macro for variable scoping
11
+# reasons. All the things we care about from the sub-project are exposed
12
+# as targets, which are globally scoped and not function scoped.
13
+function(netdata_bundle_jsonc)
14
+ include(FetchContent)
15
+ include(NetdataFetchContentExtra)
16
+
17
+ message(STATUS "Preparing vendored copy of JSON-C")
18
+
19
+ if(ENABLE_BUNDLED_JSONC)
20
+ set(FETCHCONTENT_TRY_FIND_PACKAGE_MODE NEVER)
21
+ endif()
22
+
23
+ set(FETCHCONTENT_FULLY_DISCONNECTED Off)
24
+
25
+ # JSON-C supports older versions of CMake than we do, so set
26
+ # the correct values for the few policies we actually need.
27
+ set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
28
+
29
+ # JSON-C's build system does string comparisons against option
30
+ # values instead of treating them as booleans, so we need to use
31
+ # proper strings for option values instead of just setting them
32
+ # to true or false.
33
+ set(DISABLE_BSYMBOLIC ON)
34
+ set(DISABLE_WERROR ON)
35
+ set(DISABLE_EXTRA_LIBS ON)
36
+ set(BUILD_SHARED_LIBS OFF)
37
+ set(BUILD_STATIC_LIBS ON)
38
+ set(BUILD_APPS OFF)
39
+
40
+ FetchContent_Declare(json-c
41
+ GIT_REPOSITORY https://github.com/json-c/json-c
42
+ GIT_TAG b4c371fa0cbc4dcbaccc359ce9e957a22988fb34 # json-c-0.17-20230812
43
+ )
44
+
45
+ FetchContent_MakeAvailable_NoInstall(json-c)
46
+
47
+ message(STATUS "Finished preparing vendored copy of JSON-C")
48
+endfunction()
49
+
50
+# Handle setup of json-c for the build.
51
+#
52
+# This will attempt to find json-c using pkg_check_modules. If it finds
53
+# a usable copy, that will be used. If not, it will bundle a vendored copy
54
+# as a sub-project.
55
+#
56
+# Irrespective of how json-c is to be included, library names,
57
+# include directories, and compile definitions will be specified in the
58
+# NETDATA_JSONC_* variables for later use.
59
+macro(netdata_detect_jsonc)
60
+ if(NOT ENABLE_BUNDLED_JSONC)
61
+ pkg_check_modules(JSONC json-c)
62
+ endif()
63
+
64
+ if(NOT JSONC_FOUND)
65
+ netdata_bundle_jsonc()
66
+ set(NETDATA_JSONC_LDFLAGS json-c)
67
+ set(NETDATA_JSONC_INCLUDE_DIRS ${PROJECT_BINARY_DIR}/include)
68
+ get_target_property(NETDATA_JSONC_CFLAGS_OTHER json-c INTERFACE_COMPILE_DEFINITIONS)
69
+
70
+ if(NETDATA_JSONC_CFLAGS_OTHER STREQUAL NETDATA_JSONC_CFLAGS_OTHER-NOTFOUND)
71
+ set(NETDATA_JSONC_CFLAGS_OTHER "")
72
+ endif()
73
+
74
+ add_custom_command(
75
+ OUTPUT ${PROJECT_BINARY_DIR}/include/json-c
76
+ COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/include
77
+ COMMAND ${CMAKE_COMMAND} -E create_symlink ${json-c_BINARY_DIR} ${PROJECT_BINARY_DIR}/include/json-c
78
+ COMMENT "Create compatibility symlink for vendored JSON-C headers"
79
+ DEPENDS json-c
80
+ )
81
+ add_custom_target(
82
+ json-c-compat-link
83
+ DEPENDS ${PROJECT_BINARY_DIR}/include/json-c
84
+ )
85
+ else()
86
+ set(NETDATA_JSONC_LDFLAGS ${JSONC_LDFLAGS})
87
+ set(NETDATA_JSONC_CFLAGS_OTHER ${JSONC_CFLAGS_OTHER})
88
+ set(NETDATA_JSONC_INCLUDE_DIRS ${JSONC_INCLUDE_DIRS})
89
+ add_custom_target(json-c-compat-link)
90
+ endif()
91
+endmacro()
92
+
93
+# Add json-c as a public link dependency of the specified target.
94
+#
95
+# The specified target must already exist, and the netdata_detect_json-c
96
+# macro must have already been run at least once for this to work correctly.
97
+function(netdata_add_jsonc_to_target _target)
98
+ target_include_directories(${_target} PUBLIC ${NETDATA_JSONC_INCLUDE_DIRS})
99
+ target_compile_definitions(${_target} PUBLIC ${NETDATA_JSONC_CFLAGS_OTHER})
100
+ target_link_libraries(${_target} PUBLIC ${NETDATA_JSONC_LDFLAGS})
101
+ add_dependencies(${_target} json-c-compat-link)
102
+endfunction()
packaging/jsonc.checksums
deleted
-1
@@ -1 +0,0 @@
1
-ec4eb70e0f6c0d707b9b1ec646cf7c860f4abb3562a90ea6e4d78d177fd95303 json-c-0.14-20200419.tar.gz
packaging/jsonc.version
deleted
-1
@@ -1 +0,0 @@
1
-0.14-20200419