0
0
Fork 0
mirror of https://github.com/netdata/netdata.git synced 2025-04-27 14:16:20 +00:00
netdata_netdata/packaging/cmake/Modules/NetdataFetchContentExtra.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

41 lines
1.6 KiB
CMake

# Extra tools for working with FetchContent on older CMake
#
# Copyright (c) 2024 Netdata Inc.
# SPDX-License-Identifier: GPL-3.0-or-later
# FetchContent_MakeAvailable_NoInstall
#
# Add a sub-project with FetchContent, but with the EXCLUDE_FROM_ALL
# argument for the add_subdirectory part.
#
# CMake 3.28 and newer provide a way to do this with an extra argument
# on FetchContent_Declare, but older versions need you to implement
# the logic yourself. Once we no longer support CMake versions older
# than 3.28, we can get rid of this macro.
#
# Unlike FetchContent_MakeAvailble, this only accepts a single project
# to make available.
macro(FetchContent_MakeAvailable_NoInstall name)
include(FetchContent)
FetchContent_GetProperties(${name})
if(NOT ${name}_POPULATED)
FetchContent_Populate(${name})
add_subdirectory(${${name}_SOURCE_DIR} ${${name}_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
endmacro()
# NETDATA_PROPAGATE_TOOLCHAIN_ARGS
#
# Defines a set of CMake flags to be passed to CMAKE_ARGS for
# FetchContent_Declare and ExternalProject_Add to ensure that toolchain
# configuration propagates correctly to sub-projects.
#
# This needs to be explicitly included for any sub-project that needs
# to be built for the target system.
set(NETDATA_PROPAGATE_TOOLCHAIN_ARGS
"-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
$<$<BOOL:${CMAKE_C_COMPILER_TARGET}>:-DCMAKE_C_COMPILER_TARGET=${CMAKE_C_COMPILER_TARGET}
$<$<BOOL:${CMAKE_CXX_COMPILER_TARGET}>:-DCMAKE_CXX_COMPILER_TARGET=${CMAKE_CXX_COMPILER_TARGET}")