NetdataFetchContentExtra.cmake 2.0 KB

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