cmake: Consolidate compile settings
authorManuel Nickschas <sputnick@quassel-irc.org>
Thu, 15 Nov 2018 21:41:26 +0000 (22:41 +0100)
committerManuel Nickschas <sputnick@quassel-irc.org>
Sun, 18 Nov 2018 10:06:43 +0000 (11:06 +0100)
Consolidate the various places where we were setting compiler options
in QuasselCompileSettings.cmake. No longer include KDE's settings;
instead, take some inspiration from their setup and incorporate this
in our own, so we not only benefit when building against KDE
Frameworks.

Properly handle CMAKE_CXX_FLAGS and friends; append the flags to the
end of our own list of flags, so one can override what we define.
Clear the variables afterwards to avoid duplication.

Get rid of some MinGW options that have been around for a decade
and are most likely no longer required.

CMakeLists.txt
cmake/QuasselCompileSettings.cmake
cmake/QuasselMacros.cmake

index b5164ca..b086070 100644 (file)
@@ -113,8 +113,7 @@ if (NOT EMBED_DEFAULT)
 endif()
 
 # The following options are not for end-user consumption, so don't list them in the feature summary
 endif()
 
 # The following options are not for end-user consumption, so don't list them in the feature summary
-cmake_dependent_option(DEPLOY "Add required libs to bundle resources and create a dmg. Note: requires Qt to be built with 10.4u SDK" OFF "APPLE" OFF)
-
+cmake_dependent_option(DEPLOY "Add required libs to bundle resources and create a dmg" OFF "APPLE" OFF)
 
 # List of authenticators and the cmake flags to build them
 # (currently that's just LDAP, but more can be added here).
 
 # List of authenticators and the cmake flags to build them
 # (currently that's just LDAP, but more can be added here).
@@ -124,11 +123,25 @@ option(WITH_LDAP "Enable LDAP authentication support if present on system" ON)
 # Setup CMake
 #####################################################################
 
 # Setup CMake
 #####################################################################
 
+# Visibility settings apply to all targets
+if (POLICY CMP0063)
+    cmake_policy(SET CMP0063 NEW)
+endif()
+
 # Let automoc/autouic process generated files
 if (POLICY CMP0071)
     cmake_policy(SET CMP0071 NEW)
 endif()
 
 # Let automoc/autouic process generated files
 if (POLICY CMP0071)
     cmake_policy(SET CMP0071 NEW)
 endif()
 
+set(BUILD_SHARED_LIBS TRUE CACHE BOOL "" FORCE)
+
+# Don't use X11 on OSX
+if (APPLE)
+    set(CMAKE_DISABLE_FIND_PACKAGE_X11 true)
+    set(CMAKE_DISABLE_FIND_PACKAGE_XCB true)
+    set(CMAKE_DISABLE_FIND_PACKAGE_Qt5X11Extras true)
+endif()
+
 # Simplify later checks
 #####################################################################
 
 # Simplify later checks
 #####################################################################
 
@@ -139,7 +152,6 @@ if (WANT_MONO OR WANT_CORE)
     set(BUILD_CORE true)
 endif()
 
     set(BUILD_CORE true)
 endif()
 
-
 # Set up Qt
 #####################################################################
 
 # Set up Qt
 #####################################################################
 
@@ -364,6 +376,9 @@ if (BUILD_TESTING)
         PURPOSE "Required for building unit tests"
     )
     enable_testing()
         PURPOSE "Required for building unit tests"
     )
     enable_testing()
+
+    # GTest messes with CMAKE_CXX_FLAGS, so process them again
+    process_cmake_cxx_flags()
 endif()
 
 # Check for SSL support in Qt
 endif()
 
 # Check for SSL support in Qt
@@ -385,34 +400,17 @@ if (HAVE_SSL)
 endif()
 add_feature_info("SSL support in Qt" HAVE_SSL "Use secure network connections")
 
 endif()
 add_feature_info("SSL support in Qt" HAVE_SSL "Use secure network connections")
 
-# Additional compile settings
-#####################################################################
-
-# Needed to compile with mingw without kde
-if (MINGW AND NOT WITH_KDE)
-    add_definitions(-D_WIN32_WINNT=0x0500)
-    message(STATUS "Added _WIN32_WINNT=0x0500 definition for MinGW")
-    # workaround for bug in mingw gcc 4.0
-    add_definitions(-U__STRICT_ANSI__)
-endif()
-
 # Setup support for KDE Frameworks
 #####################################################################
 
 # Setup support for KDE Frameworks
 #####################################################################
 
-# We want to do this up here, so we have the necessary variables and defines set before
-# compiling anything
-
 if (WITH_KDE)
 if (WITH_KDE)
+    add_definitions(-DHAVE_KDE -DHAVE_KF5)
+    set(WITH_KF5 TRUE)
+
     # If KDE Frameworks are present, they're most probably providing Qt5 integration including icon loading
     set(EMBED_DATA OFF)
 
     include(KDEInstallDirs)
     # If KDE Frameworks are present, they're most probably providing Qt5 integration including icon loading
     set(EMBED_DATA OFF)
 
     include(KDEInstallDirs)
-    include(KDECompilerSettings)
-    include(KDECMakeSettings)
-
-    kde_enable_exceptions()
-    add_definitions(-DHAVE_KDE -DHAVE_KF5)
-    set(WITH_KF5 TRUE)
 endif()
 
 # This needs to come after setting up KDE integration, so we can use KDE-specific paths
 endif()
 
 # This needs to come after setting up KDE integration, so we can use KDE-specific paths
@@ -468,15 +466,10 @@ endif()
 if (WIN32)
     link_libraries(imm32 winmm dbghelp Secur32)  # missing by default :/
     if (MSVC)
 if (WIN32)
     link_libraries(imm32 winmm dbghelp Secur32)  # missing by default :/
     if (MSVC)
-        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /DNOMINMAX")
-        set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBUGINFO "/debug /INCREMENTAL:YES /NODEFAULTLIB:libcmt /DEFAULTLIB:msvcrt")
-        set(CMAKE_EXE_LINKER_FLAGS_DEBUG "/debug /INCREMENTAL:YES /NODEFAULTLIB:libcmt")
-        set(CMAKE_EXE_LINKER_FLAGS_DEBUGFULL "${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
         link_libraries(Version dwmapi shlwapi)
     endif()
 endif()
 
         link_libraries(Version dwmapi shlwapi)
     endif()
 endif()
 
-
 # Generate version information from Git
 #####################################################################
 
 # Generate version information from Git
 #####################################################################
 
index 4c4c8b4..05c5a95 100644 (file)
@@ -5,6 +5,8 @@
 # Redistribution and use is allowed according to the terms of the BSD license.
 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
 
 # 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)
 # Helper function to check for linker flag support
 include(CheckCXXCompilerFlag)
 function(check_and_set_linker_flag flag name outvar)
@@ -29,36 +31,74 @@ 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(
 # 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
         -Wall
-        -Wcast-align
         -Wextra
         -Wextra
+        -Wcast-align
         -Wformat-security
         -Wformat-security
-        -Wno-unknown-pragmas
         -Wnon-virtual-dtor
         -Wnon-virtual-dtor
+        -Woverloaded-virtual
         -Wpedantic
         -Wundef
         -Wpedantic
         -Wundef
-        -fno-common
-        -fstack-protector-strong
+        -Wvla
+        -Werror=return-type
+        -Wno-unknown-pragmas
         "$<$<NOT:$<CONFIG:Debug>>:-U_FORTIFY_SOURCE;-D_FORTIFY_SOURCE=2>"
     )
 
     # Check for and set linker flags
         "$<$<NOT:$<CONFIG:Debug>>:-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)
+    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)
+
+    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}")
+
+elseif(MSVC)
+    # Target Windows Vista
+    add_definitions(-D_WIN32_WINNT=0x0600 -DWINVER=0x0600 -D_WIN32_IE=0x0600)
+
+    # Various settings for the Windows API
+    add_definitions(-DWIN32_LEAN_AND_MEAN -DUNICODE -D_UNICODE -D_USE_MATH_DEFINES -DNOMINMAX)
+
+    # Compile options
+    add_compile_options(-EHsc -W3)
+
+    # 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}")
 
 
-    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!)
 else()
     # 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
 endif()
 
 # Mac build stuff
-if (APPLE AND DEPLOY)
+if (APPLE)
     set(CMAKE_OSX_ARCHITECTURES "x86_64")
     add_compile_options(
         -mmacosx-version-min=10.9
         -stdlib=libc++
     )
     set(CMAKE_OSX_ARCHITECTURES "x86_64")
     add_compile_options(
         -mmacosx-version-min=10.9
         -stdlib=libc++
     )
+    add_definitions(-DQT_MAC_USE_COCOA -D_DARWIN_C_SOURCE)
 endif()
 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()
index 9f576cd..7ab4901 100644 (file)
@@ -262,3 +262,19 @@ function(target_link_if_exists _target)
         endforeach()
     endif()
 endfunction()
         endforeach()
     endif()
 endfunction()
+
+###################################################################################################
+# process_cmake_cxx_flags()
+#
+# Append the options declared CMAKE_CXX_FLAGS and CMAKE_CXX_FLAGS_<BUILD_TYPE> to the global
+# compile options.
+# Unset the variables afterwards to avoid duplication.
+#
+function(process_cmake_cxx_flags)
+    string(TOUPPER ${CMAKE_BUILD_TYPE} upper_build_type)
+    set(cxx_flags "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${upper_build_type}}")
+    separate_arguments(sep_cxx_flags UNIX_COMMAND ${cxx_flags})
+    add_compile_options(${sep_cxx_flags})
+    set(CMAKE_CXX_FLAGS "" PARENT_SCOPE)
+    set(CMAKE_CXX_FLAGS_${upper_build_type} "" PARENT_SCOPE)
+endfunction()