X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=cmake%2FQuasselCompileSettings.cmake;h=ec3e17a4b8354789ea25b0d4a84eb3f3cadb3326;hp=1fc81610118a43e7aeb6a86fc6bf0490579ae723;hb=d9e586707522241d628a90466e13722e342cc28a;hpb=84cd3561e97167ffb98ecab0fd2b884ba1d13ada diff --git a/cmake/QuasselCompileSettings.cmake b/cmake/QuasselCompileSettings.cmake index 1fc81610..ec3e17a4 100644 --- a/cmake/QuasselCompileSettings.cmake +++ b/cmake/QuasselCompileSettings.cmake @@ -1,71 +1,63 @@ # This file contains compile flags and general build configuration for Quassel # -# (C) 2014 by the Quassel Project +# (C) 2014-2018 by the Quassel Project # # Redistribution and use is allowed according to the terms of the BSD license. # For details see the accompanying COPYING-CMAKE-SCRIPTS file. +# Helper function to check for linker flag support include(CheckCXXCompilerFlag) - -if(CMAKE_CONFIGURATION_TYPES) - set(CMAKE_CONFIGURATION_TYPES Release RelWithDebInfo Debug Debugfull Profile) - set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING "These are the configuration types we support" FORCE) -endif() - -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo Debug Debugfull Profile" FORCE) -endif() - -# Qt debug flags -set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_DEBUG QT_DEBUG) -set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_DEBUGFULL QT_DEBUG) -set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_RELEASE QT_NO_DEBUG NDEBUG) -set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_RELWITHDEBINFO QT_NO_DEBUG NDEBUG) -set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_PROFILE QT_NO_DEBUG NDEBUG) -set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_MINSIZEREL QT_NO_DEBUG NDEBUG) - -if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE) - set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS QT_NO_DEBUG NDEBUG) -endif() - -# Enable various flags on gcc -if(CMAKE_COMPILER_IS_GNUCXX) - # Let's just hope that all gccs support these options and skip the tests... - # -fno-strict-aliasing is needed apparently for Qt < 4.6 - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ansi -Wall -Wextra -Wnon-virtual-dtor -fno-strict-aliasing") - # set(CMAKE_CXX_FLAGS_RELEASE "-O2") # use CMake default - # set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-g -O2") # use CMake default - set(CMAKE_CXX_FLAGS_DEBUG "-g -ggdb -O2 -fno-reorder-blocks -fno-schedule-insns -fno-inline") - set(CMAKE_CXX_FLAGS_DEBUGFULL "-g3 -ggdb -fno-inline") - set(CMAKE_CXX_FLAGS_PROFILE "-g3 -ggdb -fno-inline -ftest-coverage -fprofile-arcs") - - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ansi -W -Wall -Wextra -Wnon-virtual-dtor -fno-strict-aliasing -Wundef -Wcast-align -Wpointer-arith -Wformat-security -fno-check-new -fno-common") - - check_cxx_compiler_flag(-Woverloaded-virtual CXX_W_OVERLOADED_VIRTUAL) - if(CXX_W_OVERLOADED_VIRTUAL) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Woverloaded-virtual") +function(check_and_set_linker_flag flag name outvar) + cmake_push_check_state(RESET) + set(CMAKE_REQUIRED_FLAGS "${flag}") + check_cxx_compiler_flag("" LINKER_SUPPORTS_${name}) + if (LINKER_SUPPORTS_${name}) + set(${outvar} "${${outvar}} ${flag}" PARENT_SCOPE) endif() - - # Just for miniz - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-function -Wno-undef -fno-strict-aliasing") -endif() - -# ... and for Clang -if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wnon-virtual-dtor -Wno-long-long -Wundef -Wcast-align -Wchar-subscripts -Wall -W -Wextra -Wpointer-arith -Wformat-security -Woverloaded-virtual -fno-common -Werror=return-type") - # set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG -DQT_NO_DEBUG") # Use CMake default - # set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g -DNDEBUG -DQT_NO_DEBUG") # Use CMake default - set(CMAKE_CXX_FLAGS_DEBUG "-g -O2 -fno-inline") - set(CMAKE_CXX_FLAGS_DEBUGFULL "-g3 -fno-inline") - set(CMAKE_CXX_FLAGS_PROFILE "-g3 -fno-inline -ftest-coverage -fprofile-arcs") - - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-function -Wno-undef -fno-strict-aliasing") + cmake_pop_check_state() +endfunction() + +# General compile settings +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED OFF) # Rely on compile features if standard is not supported +set(CMAKE_CXX_EXTENSIONS OFF) # We like to be standard conform +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +# For GCC and Clang, enable a whole bunch of warnings +if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") + add_compile_options( + -Wall + -Wcast-align + -Wextra + -Wformat-security + -Wno-unknown-pragmas + -Wnon-virtual-dtor + -Wpedantic + -Wundef + -fno-common + -fstack-protector-strong + -fvisibility=default + -fvisibility-inlines-hidden + "$<$>:-U_FORTIFY_SOURCE;-D_FORTIFY_SOURCE=2>" + ) + + # Check for and set linker flags + check_and_set_linker_flag("-Wl,-z,relro" RELRO LINKER_FLAGS) + check_and_set_linker_flag("-Wl,-z,now" NOW LINKER_FLAGS) + check_and_set_linker_flag("-Wl,--as-needed" AS_NEEDED LINKER_FLAGS) + + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}") + set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINKER_FLAGS}") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINKER_FLAGS}") +else() + # For other compilers, we rely on default settings (unless someone provides a good set of options; patches welcome!) endif() # Mac build stuff -if(APPLE AND DEPLOY) +if (APPLE AND DEPLOY) set(CMAKE_OSX_ARCHITECTURES "x86_64") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mmacosx-version-min=10.6") - set(CMAKE_OSX_SYSROOT "/Developer/SDKs/MacOSX10.6.sdk/") - add_definitions(-DMAC_10_6_SDK) + add_compile_options( + -mmacosx-version-min=10.9 + -stdlib=libc++ + ) endif()