CheckLinkerSupportsPIE.cmake raw

   1  # Copyright (c) 2024-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  function(check_linker_supports_pie warnings)
   8    # Workaround for a bug in the check_pie_supported() function.
   9    # See:
  10    # - https://gitlab.kitware.com/cmake/cmake/-/issues/26463
  11    # - https://gitlab.kitware.com/cmake/cmake/-/merge_requests/10034
  12    if(CMAKE_VERSION VERSION_LESS 3.32)
  13      # CMAKE_CXX_COMPILE_OPTIONS_PIE is a list, whereas CMAKE_REQUIRED_FLAGS
  14      # must be a string. Therefore, a proper conversion is required.
  15      list(JOIN CMAKE_CXX_COMPILE_OPTIONS_PIE " " CMAKE_REQUIRED_FLAGS)
  16    endif()
  17  
  18    include(CheckPIESupported)
  19    check_pie_supported(OUTPUT_VARIABLE output LANGUAGES CXX)
  20    if(CMAKE_CXX_LINK_PIE_SUPPORTED)
  21      set(CMAKE_POSITION_INDEPENDENT_CODE ON PARENT_SCOPE)
  22    elseif(NOT WIN32)
  23      # The warning is superfluous for Windows.
  24      message(WARNING "PIE is not supported at link time. See the configure log for details.")
  25      set(${warnings} ${${warnings}} "Position independent code disabled." PARENT_SCOPE)
  26    endif()
  27  endfunction()
  28