From 6340ed89934ac8878e456c0b346d4c5c8c8fde83 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Mon, 23 Apr 2012 16:26:15 +0200 Subject: [PATCH] Add support for building position-independent executables. --- CMakeLists.txt | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e1cd42993..f8370ffb7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake ) option(WITH_TESTS "Enable building of unit tests" ON) option(WITH_GUI_TESTS "Enable building of GUI tests" OFF) option(WITH_LTO "Enable Link Time Optimization (LTO)" OFF) +option(WITH_PIE "Build as Position-independent executable (PIE)" OFF) set( KEEPASSX_VERSION "0.9.0" ) @@ -80,7 +81,7 @@ endif(CMAKE_SYSTEM_NAME STREQUAL "Linux") if(WITH_LTO) include(CheckCXXCompilerFlag) - if(CMAKE_COMPILER_IS_GNUCXX) + if(CMAKE_COMPILER_IS_GNUCC and CMAKE_COMPILER_IS_GNUCXX) check_cxx_compiler_flag("-flto -fuse-linker-plugin" LTO_AVAILABLE) if(LTO_AVAILABLE) @@ -90,9 +91,18 @@ if(WITH_LTO) endif(LTO_AVAILABLE) else() message(FATAL_ERROR "LTO is only supported with gcc") - endif(CMAKE_COMPILER_IS_GNUCXX) + endif(CMAKE_COMPILER_IS_GNUCC and CMAKE_COMPILER_IS_GNUCXX) endif(WITH_LTO) +if(WITH_PIE) + if((CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG) AND (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANGXX)) + add_gcc_compiler_flags("-fPIE") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIE -pie") + else() + message(FATAL_ERROR "PIE is only supported with gcc and clang") + endif((CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG) AND (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANGXX)) +endif(WITH_PIE) + if( APPLE OR MINGW ) set( PROGNAME KeePassX ) else( APPLE OR MINGW )