From 235437354e223550ef92845687560363efbbf82f Mon Sep 17 00:00:00 2001 From: Michael Uleysky Date: Tue, 27 Oct 2015 15:54:54 +1000 Subject: [PATCH] Check and really use linker flags --- CMakeLists.txt | 34 ++++++++++++++++++++++++++++++++- cmake/modules/BuildModule.cmake | 2 +- src/CMakeLists.txt | 2 +- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 49fdd57..5116050 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,9 @@ include_directories(include) # Checking compiler options set(default_options -Wall) +set(linker_options -Wall) include(CheckCXXCompilerFlag) +set(saved_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) # C++11 is required CHECK_CXX_COMPILER_FLAG(-std=c++11 COMPILER_SUPPORTS_CXX11) CHECK_CXX_COMPILER_FLAG(-std=c++0x COMPILER_SUPPORTS_CXX0X) @@ -37,28 +39,58 @@ else() message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.") endif() # Link time optimization check +set(CMAKE_REQUIRED_FLAGS -flto) CHECK_CXX_COMPILER_FLAG(-flto COMPILER_SUPPORTS_FLTO) if(COMPILER_SUPPORTS_FLTO AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug") set(default_options ${default_options} -flto) + set(linker_options ${linker_options} -flto) endif() +unset(CMAKE_REQUIRED_FLAGS) +# Clang can use lto only with gold linker +if(NOT COMPILER_SUPPORTS_FLTO) + set(CMAKE_REQUIRED_FLAGS -flto\ -fuse-ld=gold) + CHECK_CXX_COMPILER_FLAG(-flto\ -fuse-ld=gold COMPILER_SUPPORTS_FLTO_GOLD) + if(COMPILER_SUPPORTS_FLTO_GOLD AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug") + set(default_options ${default_options} -flto) + set(linker_options ${linker_options} -flto\ -fuse-ld=gold) + endif() + unset(CMAKE_REQUIRED_FLAGS) +endif() +# Dwarf-4 support check +set(CMAKE_REQUIRED_FLAGS -gdwarf-4) +CHECK_CXX_COMPILER_FLAG(-gdwarf-4 COMPILER_SUPPORTS_DWARF4) +if(COMPILER_SUPPORTS_DWARF4 AND (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")) + set(default_options ${default_options} -gdwarf-4) + set(linker_options ${linker_options} -gdwarf-4) +endif() +unset(CMAKE_REQUIRED_FLAGS) # -ipo flag check (Intel compiler link time optimization) +set(CMAKE_REQUIRED_FLAGS -ipo) CHECK_CXX_COMPILER_FLAG(-ipo COMPILER_SUPPORTS_IPO) if(COMPILER_SUPPORTS_IPO AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug") set(default_options ${default_options} -ipo) + set(linker_options ${linker_options} -ipo) endif() +unset(CMAKE_REQUIRED_FLAGS) # Default hidden visibility check +set(CMAKE_REQUIRED_FLAGS -fvisibility=hidden) CHECK_CXX_COMPILER_FLAG(-fvisibility=hidden COMPILER_SUPPORTS_HIDDEN) if(COMPILER_SUPPORTS_HIDDEN) set(default_options ${default_options} -fvisibility=hidden) + set(linker_options ${linker_options} -fvisibility=hidden) endif() +unset(CMAKE_REQUIRED_FLAGS) # Export dynamic. Required as modules mast call functions from main programm +set(CMAKE_REQUIRED_FLAGS -Wl,--export-dynamic) CHECK_CXX_COMPILER_FLAG(-Wl,--export-dynamic COMPILER_SUPPORTS_EXPORTDYNAMIC) if(COMPILER_SUPPORTS_EXPORTDYNAMIC) - set(default_options ${default_options} -Wl,--export-dynamic) + set(linker_options ${linker_options} -Wl,--export-dynamic) else() message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no support of --export-dynamic. Please use a different C++ compiler.") endif() +unset(CMAKE_REQUIRED_FLAGS) add_compile_options(${default_options}) +set(CMAKE_REQUIRED_FLAGS ${saved_CMAKE_REQUIRED_FLAGS}) file(GLOB modules modules/*) file(GLOB extramodules extramodules/*) diff --git a/cmake/modules/BuildModule.cmake b/cmake/modules/BuildModule.cmake index bf19be3..9452cca 100644 --- a/cmake/modules/BuildModule.cmake +++ b/cmake/modules/BuildModule.cmake @@ -6,6 +6,6 @@ if(MODULE_${modname}_STATIC) else() include_directories(${MODULE_ADDITIONAL_INCLUDES}) add_library(${modname} MODULE ${srcs} ${MODULE_ADDITIONAL_SOURCES}) - target_link_libraries(${modname} ${MODULE_ADDITIONAL_LIBRARIES}) + target_link_libraries(${modname} ${linker_options} ${MODULE_ADDITIONAL_LIBRARIES}) endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9c44e10..7a5218b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -39,5 +39,5 @@ add_executable(${EXENAME} ${srcs} ${FLEX_LScanner_OUTPUTS} ${BISON_GParser_OUTPU set_source_files_properties(init.cpp PROPERTIES OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/grammatical.h) add_custom_command(OUTPUT grammatical.h COMMAND cp grammatical.hpp grammatical.h DEPENDS grammatical.hpp WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) -target_link_libraries(${EXENAME} ${dl} ${pthread} ${MODULES_LIBS}) +target_link_libraries(${EXENAME} ${linker_options} ${dl} ${pthread} ${MODULES_LIBS}) set_target_properties(${EXENAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)