From 65a0920ca6b11b4250b5cdcf174eaa9ea55fa6ed Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Wed, 15 Jan 2025 16:09:16 +0000 Subject: [PATCH] cmake: Add `CheckLinkerSupportsPIE` module This new module serves as a wrapper around CMake's `CheckPIESupported` module and addresses an upstream bug: - https://gitlab.kitware.com/cmake/cmake/-/issues/26463 - https://gitlab.kitware.com/cmake/cmake/-/merge_requests/10034 --- CMakeLists.txt | 12 ++-------- cmake/module/CheckLinkerSupportsPIE.cmake | 27 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 cmake/module/CheckLinkerSupportsPIE.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 767ccc57749..4e1a4b81f82 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -186,16 +186,8 @@ string(APPEND CMAKE_CXX_LINK_EXECUTABLE " ${APPEND_LDFLAGS}") set(configure_warnings) -include(CheckPIESupported) -check_pie_supported(OUTPUT_VARIABLE check_pie_output LANGUAGES CXX) -if(CMAKE_CXX_LINK_PIE_SUPPORTED) - set(CMAKE_POSITION_INDEPENDENT_CODE ON) -elseif(NOT WIN32) - # The warning is superfluous for Windows. - message(WARNING "PIE is not supported at link time: ${check_pie_output}") - list(APPEND configure_warnings "Position independent code disabled.") -endif() -unset(check_pie_output) +include(CheckLinkerSupportsPIE) +check_linker_supports_pie(configure_warnings) # The core_interface library aims to encapsulate common build flags. # It is a usage requirement for all targets except for secp256k1, which diff --git a/cmake/module/CheckLinkerSupportsPIE.cmake b/cmake/module/CheckLinkerSupportsPIE.cmake new file mode 100644 index 00000000000..6551033c1c9 --- /dev/null +++ b/cmake/module/CheckLinkerSupportsPIE.cmake @@ -0,0 +1,27 @@ +# Copyright (c) 2024-present The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or https://opensource.org/license/mit/. + +include_guard(GLOBAL) + +function(check_linker_supports_pie warnings) + # Workaround for a bug in the check_pie_supported() function. + # See: + # - https://gitlab.kitware.com/cmake/cmake/-/issues/26463 + # - https://gitlab.kitware.com/cmake/cmake/-/merge_requests/10034 + if(CMAKE_VERSION VERSION_LESS 3.32) + # CMAKE_CXX_COMPILE_OPTIONS_PIE is a list, whereas CMAKE_REQUIRED_FLAGS + # must be a string. Therefore, a proper conversion is required. + list(JOIN CMAKE_CXX_COMPILE_OPTIONS_PIE " " CMAKE_REQUIRED_FLAGS) + endif() + + include(CheckPIESupported) + check_pie_supported(OUTPUT_VARIABLE output LANGUAGES CXX) + if(CMAKE_CXX_LINK_PIE_SUPPORTED) + set(CMAKE_POSITION_INDEPENDENT_CODE ON PARENT_SCOPE) + elseif(NOT WIN32) + # The warning is superfluous for Windows. + message(WARNING "PIE is not supported at link time: ${output}") + set(${warnings} ${${warnings}} "Position independent code disabled." PARENT_SCOPE) + endif() +endfunction()