NetdataFetchContentExtra.cmake 2.0 KB

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