ec3e17a4b8354789ea25b0d4a84eb3f3cadb3326
[quassel.git] / cmake / QuasselCompileSettings.cmake
1 # This file contains compile flags and general build configuration for Quassel
2 #
3 # (C) 2014-2018 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 # Helper function to check for linker flag support
9 include(CheckCXXCompilerFlag)
10 function(check_and_set_linker_flag flag name outvar)
11     cmake_push_check_state(RESET)
12     set(CMAKE_REQUIRED_FLAGS "${flag}")
13     check_cxx_compiler_flag("" LINKER_SUPPORTS_${name})
14     if (LINKER_SUPPORTS_${name})
15         set(${outvar} "${${outvar}} ${flag}" PARENT_SCOPE)
16     endif()
17     cmake_pop_check_state()
18 endfunction()
19
20 # General compile settings
21 set(CMAKE_CXX_STANDARD 14)
22 set(CMAKE_CXX_STANDARD_REQUIRED OFF)    # Rely on compile features if standard is not supported
23 set(CMAKE_CXX_EXTENSIONS OFF)           # We like to be standard conform
24 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
25
26 # For GCC and Clang, enable a whole bunch of warnings
27 if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
28     add_compile_options(
29         -Wall
30         -Wcast-align
31         -Wextra
32         -Wformat-security
33         -Wno-unknown-pragmas
34         -Wnon-virtual-dtor
35         -Wpedantic
36         -Wundef
37         -fno-common
38         -fstack-protector-strong
39         -fvisibility=default
40         -fvisibility-inlines-hidden
41         "$<$<NOT:$<CONFIG:Debug>>:-U_FORTIFY_SOURCE;-D_FORTIFY_SOURCE=2>"
42     )
43
44     # Check for and set linker flags
45     check_and_set_linker_flag("-Wl,-z,relro"    RELRO     LINKER_FLAGS)
46     check_and_set_linker_flag("-Wl,-z,now"      NOW       LINKER_FLAGS)
47     check_and_set_linker_flag("-Wl,--as-needed" AS_NEEDED LINKER_FLAGS)
48
49     set(CMAKE_EXE_LINKER_FLAGS    "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}")
50     set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINKER_FLAGS}")
51     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINKER_FLAGS}")
52 else()
53     # For other compilers, we rely on default settings (unless someone provides a good set of options; patches welcome!)
54 endif()
55
56 # Mac build stuff
57 if (APPLE AND DEPLOY)
58     set(CMAKE_OSX_ARCHITECTURES "x86_64")
59     add_compile_options(
60         -mmacosx-version-min=10.9
61         -stdlib=libc++
62     )
63 endif()