master
cmake 52 lines 1.96 KB
Raw
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 # Extra tools for working with FetchContent on older CMake
3
4 # FetchContent_MakeAvailable_NoInstall
5 #
6 # Add a sub-project with FetchContent, but with the EXCLUDE_FROM_ALL
7 # argument for the add_subdirectory part.
8 #
9 # CMake 3.28 and newer provide a way to do this with an extra argument
10 # on FetchContent_Declare, but older versions need you to implement
11 # the logic yourself. Once we no longer support CMake versions older
12 # than 3.28, we can get rid of this macro.
13 #
14 # Unlike FetchContent_MakeAvailble, this only accepts a single project
15 # to make available.
16 macro(FetchContent_MakeAvailable_NoInstall name)
17 include(FetchContent)
18
19 if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.28)
20 FetchContent_MakeAvailable(${name})
21 else()
22 FetchContent_GetProperties(${name})
23
24 if(NOT ${name}_POPULATED)
25 FetchContent_Populate(${name})
26 add_subdirectory(${${name}_SOURCE_DIR} ${${name}_BINARY_DIR} EXCLUDE_FROM_ALL)
27 endif()
28 endif()
29 endmacro()
30
31 # NETDATA_PROPAGATE_TOOLCHAIN_ARGS
32 #
33 # Defines a set of CMake flags to be passed to CMAKE_ARGS for
34 # FetchContent_Declare and ExternalProject_Add to ensure that toolchain
35 # configuration propagates correctly to sub-projects.
36 #
37 # This needs to be explicitly included for any sub-project that needs
38 # to be built for the target system.
39 #
40 # This also needs to _NOT_ have any generator expressions, as they are not
41 # supported for the required usage of this variable in CMake 3.30 or newer.
42 set(NETDATA_PROPAGATE_TOOLCHAIN_ARGS
43 "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
44 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}")
45
46 if(DEFINED CMAKE_C_COMPILER_TARGET)
47 set(NETDATA_PROPAGATE_TOOLCHAIN_ARGS "${NETDATA_PROPAGATE_TOOLCHAIN_ARGS} -DCMAKE_C_COMPILER_TARGET=${CMAKE_C_COMPILER_TARGET}")
48 endif()
49
50 if(DEFINED CMAKE_CXX_COMPILER_TARGET)
51 set(NETDATA_PROPAGATE_TOOLCHAIN_ARGS "${NETDATA_PROPAGATE_TOOLCHAIN_ARGS} -DCMAKE_CXX_COMPILER_TARGET=${CMAKE_CXX_COMPILER_TARGET}")
52 endif()