From b0351e34b79c0b361623f9bb923dcfd4129344f5 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Mon, 13 Mar 2023 17:24:26 +0000 Subject: [PATCH] [FIXUP] cmake: Rework PIC/PIE logic Must be squashed with the commit 91f28a6a961f962e0408130cbc46d9aa8cc4b84b "cmake: Add root `CMakeLists.txt` file". --- CMakeLists.txt | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 110619e53efff..c9d4fc6dec212 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,16 +52,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) set(configure_warnings) -if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.14) - include(CheckPIESupported) - check_pie_supported(OUTPUT_VARIABLE check_pie_output LANGUAGES CXX) - if(NOT CMAKE_CXX_LINK_PIE_SUPPORTED) - list(APPEND configure_warnings "PIE link options are not supported for executable targets: ${check_pie_output}.") - endif() -else() - list(APPEND configure_warnings "No PIE options will be passed to a linker for executable targets.") -endif() -set(CMAKE_POSITION_INDEPENDENT_CODE ON) if(WIN32) #[=[ @@ -123,6 +113,19 @@ include(cmake/secp256k1.cmake) include(CheckStdFilesystem) check_std_filesystem() +if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.14) + include(CheckPIESupported) + check_pie_supported(OUTPUT_VARIABLE check_pie_output LANGUAGES CXX) + if(CMAKE_CXX_LINK_PIE_SUPPORTED) + set(CMAKE_POSITION_INDEPENDENT_CODE ON) + endif() +else() + check_cxx_source_links_with_flags(-fPIE "int main(){}" COMPILER_SUPPORTS_PIE) + if(COMPILER_SUPPORTS_PIE) + set(CMAKE_POSITION_INDEPENDENT_CODE ON) + endif() +endif() + add_subdirectory(src) message("\n")