CheckSourceCompilesWithFlags.cmake raw

   1  # Copyright (c) 2023-present The Bitcoin Core developers
   2  # Distributed under the MIT software license, see the accompanying
   3  # file COPYING or https://opensource.org/license/mit/.
   4  
   5  include_guard(GLOBAL)
   6  
   7  #[=[
   8  Check once if C++ source code can be compiled.
   9  
  10  Options:
  11  
  12    CXXFLAGS - A list of additional flags to pass to the compiler.
  13  
  14    LDFLAGS - A list of additional flags to pass to the linker.
  15  
  16    LINK_LIBRARIES - A list of libraries to add to the link command.
  17  
  18  For historical reasons, among the CMake `CMAKE_REQUIRED_*` variables that influence
  19  `check_cxx_source_compiles()`, only `CMAKE_REQUIRED_FLAGS` is a string rather than
  20  a list. Additionally, `target_compile_options()` also expects a list of options.
  21  
  22  The `check_cxx_source_compiles_with_flags()` function handles this case and accepts
  23  `CXXFLAGS` as a list, simplifying the code at the caller site.
  24  
  25  #]=]
  26  function(check_cxx_source_compiles_with_flags source result_var)
  27    cmake_parse_arguments(PARSE_ARGV 2 _ "" "" "CXXFLAGS;LDFLAGS;LINK_LIBRARIES")
  28    list(JOIN __CXXFLAGS " " CMAKE_REQUIRED_FLAGS)
  29    set(CMAKE_REQUIRED_LINK_OPTIONS ${__LDFLAGS})
  30    set(CMAKE_REQUIRED_LIBRARIES ${__LINK_LIBRARIES})
  31    include(CheckCXXSourceCompiles)
  32    check_cxx_source_compiles("${source}" ${result_var})
  33    set(${result_var} ${${result_var}} PARENT_SCOPE)
  34  endfunction()
  35