cmake: Increase warning level on MSVC
[quassel.git] / cmake / QuasselCompileSettings.cmake
index 905f669..2914f42 100644 (file)
 # This file contains compile flags and general build configuration for Quassel
 #
-# (C) 2014 by the Quassel Project <devel@quassel-irc.org>
+# (C) 2014-2018 by the Quassel Project <devel@quassel-irc.org>
 #
 # Redistribution and use is allowed according to the terms of the BSD license.
 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
 
+include(QuasselMacros)
+
+# Helper function to check for linker flag support
 include(CheckCXXCompilerFlag)
+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()
+    cmake_pop_check_state()
+endfunction()
 
-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()
+# 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
 
-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()
+set(CMAKE_CXX_VISIBILITY_PRESET hidden)
+set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
+set(CMAKE_POSITION_INDEPENDENT_CODE ON)
 
-# 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)
+# For GCC and Clang, enable a whole bunch of warnings
+if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
+    add_compile_options(
+        -fdiagnostics-color=always
+        -fexceptions
+        -fno-common
+        -fstack-protector-strong
+        -Wall
+        -Wextra
+        -Wcast-align
+        -Wformat-security
+        -Wnon-virtual-dtor
+        -Woverloaded-virtual
+        -Wpedantic
+        -Wundef
+        -Wvla
+        -Werror=return-type
+        -Wno-unknown-pragmas
+        "$<$<NOT:$<CONFIG:Debug>>:-U_FORTIFY_SOURCE;-D_FORTIFY_SOURCE=2>"
+    )
 
-if (NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
-    set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS QT_NO_DEBUG NDEBUG)
-endif()
+    # 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)
+    check_and_set_linker_flag("-Wl,--enable-new-dtags"  ENABLE_NEW_DTAGS LINKER_FLAGS)
+    check_and_set_linker_flag("-Wl,--no-undefined"      NO_UNDEFINED     LINKER_FLAGS)
 
-# Enable various flags on gcc
-if (CMAKE_COMPILER_IS_GNUCXX)
-    if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.8")
-        message(FATAL_ERROR "Your compiler is too old; you need GCC 4.8+, Clang 3.3+, MSVC 19.0+, or any other compiler with full C++11 support.")
-    endif()
+    set(CMAKE_EXE_LINKER_FLAGS    "${LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS}")
+    set(CMAKE_MODULE_LINKER_FLAGS "${LINKER_FLAGS} ${CMAKE_MODULE_LINKER_FLAGS}")
+    set(CMAKE_SHARED_LINKER_FLAGS "${LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS}")
 
-    # 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} -std=c++11 -Wall -Wextra -Wnon-virtual-dtor -fno-strict-aliasing -Wundef -Wcast-align -Wpointer-arith -Wformat-security -fno-check-new -fno-common")
-    #  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")
-
-    check_cxx_compiler_flag(-Woverloaded-virtual CXX_W_OVERLOADED_VIRTUAL)
-    if(CXX_W_OVERLOADED_VIRTUAL)
-        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Woverloaded-virtual")
-    endif()
+elseif(MSVC)
+    # Target Windows Vista
+    add_definitions(-D_WIN32_WINNT=0x0600 -DWINVER=0x0600 -D_WIN32_IE=0x0600)
 
-    # Just for miniz
-    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-function -Wno-undef -fno-strict-aliasing")
+    # Various settings for the Windows API
+    add_definitions(-DWIN32_LEAN_AND_MEAN -DUNICODE -D_UNICODE -D_USE_MATH_DEFINES -DNOMINMAX)
 
-# ... and for Clang
-elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
-    if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "3.3")
-        message(FATAL_ERROR "Your compiler is too old; you need Clang 3.3+, GCC 4.8+, MSVC 19.0+, or any other compiler with full C++11 support.")
-    endif()
+    # Compile options
+    add_compile_options(/EHsc)
 
-    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wnon-virtual-dtor -Wno-long-long -Wundef -Wcast-align -Wchar-subscripts -Wall -W -Wextra -Wpointer-arith -Wformat-security -Woverloaded-virtual -fno-common -Wno-deprecated-register")
-    #  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")
+    # Increase warning level on MSVC
+    # CMake puts /W3 in CMAKE_CXX_FLAGS which will be appended later, so we need to replace
+    message(STATUS "CXX ${CMAKE_CXX_FLAGS} REL ${CMAKE_CXX_FLAGS_RELEASE} DEB ${CMAKE_CXX_FLAGS_DEBUG}")
+    string(REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
 
-    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-function -Wno-undef -fno-strict-aliasing")
+    # Silence annoying/useless warnings
+    #   C4127: conditional expression is constant
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4127")
+    #   C4244: 'identifier': conversion from 't1' to 't2', possible loss of data
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4244")
+    #   C4456: declaration of 'identifier' hides previous local declaration
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4456")
+    #   C4458: declaration of 'identifier' hides class member
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4458")
 
-# For MSVC, at least do a version sanity check
-elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
-    if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "19.0")
-        message(FATAL_ERROR "Your compiler is too old; you need at least Visual Studio 2015 (MSVC 19.0+), GCC 4.8+, Clang 3.3+, or any other compiler with full C++11 support.")
-    endif()
+    # Link against the correct version of the C runtime
+    set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/NODEFAULTLIB:libcmt /DEFAULTLIB:msvcrt ${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
+    set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "/NODEFAULTLIB:libcmt /DEFAULTLIB:msvcrt ${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
+    set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "/NODEFAULTLIB:libcmt /DEFAULTLIB:msvcrt ${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}")
+    set(CMAKE_EXE_LINKER_FLAGS_DEBUG "/NODEFAULTLIB:libcmtd /DEFAULTLIB:msvcrtd ${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
 
-# Unknown/unsupported compiler
 else()
-    message(WARNING "Unknown or unsupported compiler. Make sure to enable C++11 support. Good luck.")
+    # For other compilers, we rely on default settings (unless someone provides a good set of options; patches welcome!)
+    message(WARNING "${CMAKE_CXX_COMPILER_ID} is not a supported C++ compiler.")
 endif()
 
 # Mac build stuff
-if (APPLE AND DEPLOY)
+if (APPLE)
     set(CMAKE_OSX_ARCHITECTURES "x86_64")
-    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mmacosx-version-min=10.9")
-    set(CMAKE_OSX_SYSROOT "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk")
+    add_compile_options(
+        -mmacosx-version-min=10.9
+        -stdlib=libc++
+    )
+    add_definitions(-DQT_MAC_USE_COCOA -D_DARWIN_C_SOURCE)
 endif()
+
+# Optionally, produce clazy warnings
+if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
+    option(ENABLE_CLAZY "Enable Clazy warnings" OFF)
+
+    if(ENABLE_CLAZY)
+        set(CMAKE_CXX_COMPILE_OBJECT "${CMAKE_CXX_COMPILE_OBJECT} -Xclang -load -Xclang ClangLazy${CMAKE_SHARED_LIBRARY_SUFFIX} -Xclang -add-plugin -Xclang clang-lazy")
+    endif()
+endif()
+
+# Append CMAKE_CXX_FLAGS, so our flags can be overwritten externally.
+process_cmake_cxx_flags()