diff options
author | Marc Chevrier <marc.chevrier@gmail.com> | 2018-10-02 15:34:57 (GMT) |
---|---|---|
committer | Marc Chevrier <marc.chevrier@gmail.com> | 2018-11-11 16:34:09 (GMT) |
commit | c4b4d8b3a67718e29edb5676273e528dab566672 (patch) | |
tree | ddb8839ae8a5a15b5ffe8dd8190cf873841475da /Modules/Compiler | |
parent | 724a0346f7bd424ce0e5db246cee46db9f377a6f (diff) | |
download | CMake-c4b4d8b3a67718e29edb5676273e528dab566672.zip CMake-c4b4d8b3a67718e29edb5676273e528dab566672.tar.gz CMake-c4b4d8b3a67718e29edb5676273e528dab566672.tar.bz2 |
POSITION_INDEPENDENT_CODE: Manage link flags for executables
Fixes: #14983, #16561
Diffstat (limited to 'Modules/Compiler')
-rw-r--r-- | Modules/Compiler/AppleClang-C.cmake | 3 | ||||
-rw-r--r-- | Modules/Compiler/AppleClang-CXX.cmake | 3 | ||||
-rw-r--r-- | Modules/Compiler/Clang.cmake | 20 | ||||
-rw-r--r-- | Modules/Compiler/GNU.cmake | 16 | ||||
-rw-r--r-- | Modules/Compiler/SunPro-C.cmake | 2 | ||||
-rw-r--r-- | Modules/Compiler/SunPro-CXX.cmake | 2 | ||||
-rw-r--r-- | Modules/Compiler/SunPro-Fortran.cmake | 2 |
7 files changed, 48 insertions, 0 deletions
diff --git a/Modules/Compiler/AppleClang-C.cmake b/Modules/Compiler/AppleClang-C.cmake index a48adec..8754951 100644 --- a/Modules/Compiler/AppleClang-C.cmake +++ b/Modules/Compiler/AppleClang-C.cmake @@ -1,6 +1,9 @@ include(Compiler/Clang) __compiler_clang(C) +set(CMAKE_C_LINK_OPTIONS_PIE ${CMAKE_C_COMPILE_OPTIONS_PIE} -Xlinker -pie) +set(CMAKE_C_LINK_OPTIONS_NO_PIE -Xlinker -no_pie) + if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.0) set(CMAKE_C90_STANDARD_COMPILE_OPTION "-std=c90") set(CMAKE_C90_EXTENSION_COMPILE_OPTION "-std=gnu90") diff --git a/Modules/Compiler/AppleClang-CXX.cmake b/Modules/Compiler/AppleClang-CXX.cmake index e5fd647..54c1388 100644 --- a/Modules/Compiler/AppleClang-CXX.cmake +++ b/Modules/Compiler/AppleClang-CXX.cmake @@ -1,6 +1,9 @@ include(Compiler/Clang) __compiler_clang(CXX) +set(CMAKE_CXX_LINK_OPTIONS_PIE ${CMAKE_CXX_COMPILE_OPTIONS_PIE} -Xlinker -pie) +set(CMAKE_CXX_LINK_OPTIONS_NO_PIE -Xlinker -no_pie) + if(NOT "x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC") set(CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN "-fvisibility-inlines-hidden") endif() diff --git a/Modules/Compiler/Clang.cmake b/Modules/Compiler/Clang.cmake index e23470b..7cee9c7 100644 --- a/Modules/Compiler/Clang.cmake +++ b/Modules/Compiler/Clang.cmake @@ -21,6 +21,26 @@ else() macro(__compiler_clang lang) __compiler_gnu(${lang}) set(CMAKE_${lang}_COMPILE_OPTIONS_PIE "-fPIE") + # Link options for PIE are already set in 'Compiler/GNU.cmake' + # but clang may require alternate syntax on some platforms + if (NOT CMAKE_${lang}_FLAG_PIE) + cmake_check_compiler_flag(${lang} "${CMAKE_${lang}_COMPILE_OPTIONS_PIE};-Xlinker;-pie" + CMAKE_${lang}_FLAG_XLINKER_PIE) + if (CMAKE_${lang}_FLAG_XLINKER_PIE) + set(CMAKE_${lang}_LINK_OPTIONS_PIE ${CMAKE_${lang}_COMPILE_OPTIONS_PIE} "-Xlinker" "-pie") + else() + set(CMAKE_${lang}_LINK_OPTIONS_PIE "") + endif() + endif() + if (NOT CMAKE_${lang}_FLAG_NO_PIE) + cmake_check_compiler_flag(${lang} "-Xlinker;-no_pie" + CMAKE_${lang}_FLAG_XLINKER_NO_PIE) + if (CMAKE_${lang}_FLAG_XLINKER_NO_PIE) + set(CMAKE_${lang}_LINK_OPTIONS_NO_PIE "-Xlinker" "-no_pie") + else() + set(CMAKE_${lang}_LINK_OPTIONS_NO_PIE "") + endif() + endif() set(CMAKE_INCLUDE_SYSTEM_FLAG_${lang} "-isystem ") set(CMAKE_${lang}_COMPILE_OPTIONS_VISIBILITY "-fvisibility=") if(CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.4.0) diff --git a/Modules/Compiler/GNU.cmake b/Modules/Compiler/GNU.cmake index 4491d4b..688a1b5 100644 --- a/Modules/Compiler/GNU.cmake +++ b/Modules/Compiler/GNU.cmake @@ -9,6 +9,7 @@ endif() set(__COMPILER_GNU 1) include(Compiler/CMakeCommonCompilerMacros) +include(Internal/CMakeCheckCompilerFlag) macro(__compiler_gnu lang) # Feature flags. @@ -16,6 +17,21 @@ macro(__compiler_gnu lang) set(CMAKE_${lang}_COMPILE_OPTIONS_PIC "-fPIC") if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.4) set(CMAKE_${lang}_COMPILE_OPTIONS_PIE "-fPIE") + # Support of PIE at link stage depends on various elements : platform, compiler, linker + # so the easiest way is to check if compiler supports these flags + cmake_check_compiler_flag(${lang} "${CMAKE_${lang}_COMPILE_OPTIONS_PIE};-pie" + CMAKE_${lang}_FLAG_PIE) + if (CMAKE_${lang}_FLAG_PIE) + set(CMAKE_${lang}_LINK_OPTIONS_PIE ${CMAKE_${lang}_COMPILE_OPTIONS_PIE} "-pie") + else() + set(CMAKE_${lang}_LINK_OPTIONS_PIE "") + endif() + cmake_check_compiler_flag(${lang} "-no-pie" CMAKE_${lang}_FLAG_NO_PIE) + if (CMAKE_${lang}_FLAG_NO_PIE) + set(CMAKE_${lang}_LINK_OPTIONS_NO_PIE "-no-pie") + else() + set(CMAKE_${lang}_LINK_OPTIONS_NO_PIE "") + endif() endif() if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.0) set(CMAKE_${lang}_COMPILE_OPTIONS_VISIBILITY "-fvisibility=") diff --git a/Modules/Compiler/SunPro-C.cmake b/Modules/Compiler/SunPro-C.cmake index 1988aae..75b8fe6 100644 --- a/Modules/Compiler/SunPro-C.cmake +++ b/Modules/Compiler/SunPro-C.cmake @@ -7,6 +7,8 @@ set(CMAKE_C_VERBOSE_FLAG "-#") set(CMAKE_C_COMPILE_OPTIONS_PIC -KPIC) set(CMAKE_C_COMPILE_OPTIONS_PIE "") +set(CMAKE_C_LINK_OPTIONS_PIE "") +set(CMAKE_C_LINK_OPTIONS_NO_PIE "") set(CMAKE_SHARED_LIBRARY_C_FLAGS "-KPIC") set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-G") set(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "-R") diff --git a/Modules/Compiler/SunPro-CXX.cmake b/Modules/Compiler/SunPro-CXX.cmake index f463be3..662ac30 100644 --- a/Modules/Compiler/SunPro-CXX.cmake +++ b/Modules/Compiler/SunPro-CXX.cmake @@ -7,6 +7,8 @@ set(CMAKE_CXX_VERBOSE_FLAG "-v") set(CMAKE_CXX_COMPILE_OPTIONS_PIC -KPIC) set(CMAKE_CXX_COMPILE_OPTIONS_PIE "") +set(CMAKE_CXX_LINK_OPTIONS_PIE "") +set(CMAKE_CXX_LINK_OPTIONS_NO_PIE "") set(CMAKE_SHARED_LIBRARY_CXX_FLAGS "-KPIC") set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS "-G") set(CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG "-R") diff --git a/Modules/Compiler/SunPro-Fortran.cmake b/Modules/Compiler/SunPro-Fortran.cmake index d3a415f..e110253 100644 --- a/Modules/Compiler/SunPro-Fortran.cmake +++ b/Modules/Compiler/SunPro-Fortran.cmake @@ -4,6 +4,8 @@ set(CMAKE_Fortran_FORMAT_FREE_FLAG "-free") set(CMAKE_Fortran_COMPILE_OPTIONS_PIC "-KPIC") set(CMAKE_Fortran_COMPILE_OPTIONS_PIE "") +set(CMAKE_Fortran_LINK_OPTIONS_PIE "") +set(CMAKE_Fortran_LINK_OPTIONS_NO_PIE "") set(CMAKE_SHARED_LIBRARY_Fortran_FLAGS "-KPIC") set(CMAKE_SHARED_LIBRARY_CREATE_Fortran_FLAGS "-G") set(CMAKE_SHARED_LIBRARY_RUNTIME_Fortran_FLAG "-R") |