b499a18f0a937b66fc41ab930efa03c81e336a93
[quassel.git] / cmake / QuasselCompileSettings.cmake
1 # This file contains compile flags and general build configuration for Quassel
2 #
3 # (C) 2014 by the Quassel Project <devel@quassel-irc.org>
4 #
5 # Redistribution and use is allowed according to the terms of the BSD license.
6 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
7
8 include(CheckCXXCompilerFlag)
9
10 if (CMAKE_CONFIGURATION_TYPES)
11     set(CMAKE_CONFIGURATION_TYPES Release RelWithDebInfo Debug Debugfull Profile)
12     set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING "These are the configuration types we support" FORCE)
13 endif()
14
15 if(NOT CMAKE_BUILD_TYPE)
16     set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo Debug Debugfull Profile" FORCE)
17 endif()
18
19 # Qt debug flags
20 set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_DEBUG QT_DEBUG)
21 set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_DEBUGFULL QT_DEBUG)
22 set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_RELEASE QT_NO_DEBUG NDEBUG)
23 set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_RELWITHDEBINFO QT_NO_DEBUG NDEBUG)
24 set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_PROFILE QT_NO_DEBUG NDEBUG)
25 set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_MINSIZEREL QT_NO_DEBUG NDEBUG)
26
27 if (NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
28     set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS QT_NO_DEBUG NDEBUG)
29 endif()
30
31 # Enable various flags on gcc
32 if (CMAKE_COMPILER_IS_GNUCXX)
33     if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.8")
34         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.")
35     endif()
36
37     # Let's just hope that all gccs support these options and skip the tests...
38     # -fno-strict-aliasing is needed apparently for Qt < 4.6
39     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")
40     #  set(CMAKE_CXX_FLAGS_RELEASE          "-O2")   # use CMake default
41     #  set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-g -O2")  # use CMake default
42     set(CMAKE_CXX_FLAGS_DEBUG             "-g -ggdb -O2 -fno-reorder-blocks -fno-schedule-insns -fno-inline")
43     set(CMAKE_CXX_FLAGS_DEBUGFULL         "-g3 -ggdb -fno-inline")
44     set(CMAKE_CXX_FLAGS_PROFILE           "-g3 -ggdb -fno-inline -ftest-coverage -fprofile-arcs")
45
46     check_cxx_compiler_flag(-Woverloaded-virtual CXX_W_OVERLOADED_VIRTUAL)
47     if(CXX_W_OVERLOADED_VIRTUAL)
48         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Woverloaded-virtual")
49     endif()
50
51     # Just for miniz
52     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-function -Wno-undef -fno-strict-aliasing")
53
54 # ... and for Clang
55 elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
56     if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "3.3")
57         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.")
58     endif()
59
60     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")
61     #  set(CMAKE_CXX_FLAGS_RELEASE        "-O2 -DNDEBUG -DQT_NO_DEBUG")     # Use CMake default
62     #  set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g -DNDEBUG -DQT_NO_DEBUG")  # Use CMake default
63     set(CMAKE_CXX_FLAGS_DEBUG          "-g -O2 -fno-inline")
64     set(CMAKE_CXX_FLAGS_DEBUGFULL      "-g3 -fno-inline")
65     set(CMAKE_CXX_FLAGS_PROFILE        "-g3 -fno-inline -ftest-coverage -fprofile-arcs")
66
67     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-function -Wno-undef -fno-strict-aliasing")
68
69 # For MSVC, at least do a version sanity check...
70 elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
71     if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "19.0")
72         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.")
73     endif()
74
75     # ... and enable exception handling (required for STL types)
76     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")
77
78 # Unknown/unsupported compiler
79 else()
80     message(WARNING "Unknown or unsupported compiler. Make sure to enable C++11 support. Good luck.")
81 endif()
82
83 # Mac build stuff
84 if (APPLE AND DEPLOY)
85     set(CMAKE_OSX_ARCHITECTURES "x86_64")
86     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mmacosx-version-min=10.9 -stdlib=libc++")
87 endif()