0
0
Fork 0
mirror of https://github.com/netdata/netdata.git synced 2025-04-28 06:32:30 +00:00
netdata_netdata/packaging/cmake/Modules/NetdataJSONC.cmake
Austin S. Hemmelgarn e81a8d826c
Ensure that the choice of compiler and target is passed to sub-projects. ()
If not explicitly set using exported environment variables, the choice
of compilers and compiler targets used by CMake does not get propagated
to sub-porjects created with ExternalProject_Add or
FetchContent_Declare.

In most cases this does not matter, because very few people are building
using non-default compilers for their environment, but it can cause
issues in two specific cases:

- If building for the same system that the build is happening on, but
  using a non-default compiler specified using CMake arguments (instead
  of via exported environment variables), sub-projects will still use
  the default compiler for the system, which may result in linking
  errors (or runtime failures even if the link succeeds).
- If cross-compiling and not using the preferred approaches of either a
  toolchain file or exported environment variables, sub-projects may not
  even build for the correct CPU architecture, causing the build to
  fail.

This adds logic to ensure that the compiler and compiler targets get
propagated correctly even if they are just specified on the command
line, thus avoiding the above two possibilities.
2024-05-24 14:13:25 +03:00

103 lines
4.2 KiB
CMake

# Functions and macros for handling of JSON-C
#
# Copyright (c) 2024 Netdata Inc.
# SPDX-License-Identifier: GPL-3.0-or-later
# Handle bundling of json-c.
#
# This pulls it in as a sub-project using FetchContent functionality.
#
# This needs to be a function and not a macro for variable scoping
# reasons. All the things we care about from the sub-project are exposed
# as targets, which are globally scoped and not function scoped.
function(netdata_bundle_jsonc)
include(FetchContent)
include(NetdataFetchContentExtra)
message(STATUS "Preparing vendored copy of JSON-C")
if(ENABLE_BUNDLED_JSONC)
set(FETCHCONTENT_TRY_FIND_PACKAGE_MODE NEVER)
endif()
set(FETCHCONTENT_FULLY_DISCONNECTED Off)
# JSON-C supports older versions of CMake than we do, so set
# the correct values for the few policies we actually need.
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
# JSON-C's build system does string comparisons against option
# values instead of treating them as booleans, so we need to use
# proper strings for option values instead of just setting them
# to true or false.
set(DISABLE_BSYMBOLIC ON)
set(DISABLE_WERROR ON)
set(DISABLE_EXTRA_LIBS ON)
set(BUILD_SHARED_LIBS OFF)
set(BUILD_STATIC_LIBS ON)
set(BUILD_APPS OFF)
FetchContent_Declare(json-c
GIT_REPOSITORY https://github.com/json-c/json-c
GIT_TAG b4c371fa0cbc4dcbaccc359ce9e957a22988fb34 # json-c-0.17-20230812
CMAKE_ARGS ${NETDATA_CMAKE_PROPAGATE_TOOLCHAIN_ARGS}
)
FetchContent_MakeAvailable_NoInstall(json-c)
message(STATUS "Finished preparing vendored copy of JSON-C")
endfunction()
# Handle setup of json-c for the build.
#
# This will attempt to find json-c using pkg_check_modules. If it finds
# a usable copy, that will be used. If not, it will bundle a vendored copy
# as a sub-project.
#
# Irrespective of how json-c is to be included, library names,
# include directories, and compile definitions will be specified in the
# NETDATA_JSONC_* variables for later use.
macro(netdata_detect_jsonc)
if(NOT ENABLE_BUNDLED_JSONC)
pkg_check_modules(JSONC json-c)
endif()
if(NOT JSONC_FOUND)
netdata_bundle_jsonc()
set(NETDATA_JSONC_LDFLAGS json-c)
set(NETDATA_JSONC_INCLUDE_DIRS ${PROJECT_BINARY_DIR}/include)
get_target_property(NETDATA_JSONC_CFLAGS_OTHER json-c INTERFACE_COMPILE_DEFINITIONS)
if(NETDATA_JSONC_CFLAGS_OTHER STREQUAL NETDATA_JSONC_CFLAGS_OTHER-NOTFOUND)
set(NETDATA_JSONC_CFLAGS_OTHER "")
endif()
add_custom_command(
OUTPUT ${PROJECT_BINARY_DIR}/include/json-c
COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/include
COMMAND ${CMAKE_COMMAND} -E create_symlink ${json-c_BINARY_DIR} ${PROJECT_BINARY_DIR}/include/json-c
COMMENT "Create compatibility symlink for vendored JSON-C headers"
DEPENDS json-c
)
add_custom_target(
json-c-compat-link
DEPENDS ${PROJECT_BINARY_DIR}/include/json-c
)
else()
set(NETDATA_JSONC_LDFLAGS ${JSONC_LDFLAGS})
set(NETDATA_JSONC_CFLAGS_OTHER ${JSONC_CFLAGS_OTHER})
set(NETDATA_JSONC_INCLUDE_DIRS ${JSONC_INCLUDE_DIRS})
add_custom_target(json-c-compat-link)
endif()
endmacro()
# Add json-c as a public link dependency of the specified target.
#
# The specified target must already exist, and the netdata_detect_json-c
# macro must have already been run at least once for this to work correctly.
function(netdata_add_jsonc_to_target _target)
target_include_directories(${_target} PUBLIC ${NETDATA_JSONC_INCLUDE_DIRS})
target_compile_options(${_target} PUBLIC ${NETDATA_JSONC_CFLAGS_OTHER})
target_link_libraries(${_target} PUBLIC ${NETDATA_JSONC_LDFLAGS})
add_dependencies(${_target} json-c-compat-link)
endfunction()