aboutsummaryrefslogtreecommitdiff
path: root/cmake/CompilerOptions.cmake
blob: dbb37fddffaabd00ba427c9b72848b0bea68bf23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# ==================================================================================
# adapted from
# https://github.com/zhivkopetrov/cmake_helpers.git
# MIT License   Copyright (c) 2020-2023 Zhivko Petrov
#
# some compiler warnings from
# https://lefticus.gitbooks.io/cpp-best-practices/content/02-Use_the_Tools_Available.html
#
# ==================================================================================
# target_enable_warnings

function(target_enable_warnings target)
    if(CMAKE_CXX_COMPILER_ID MATCHES MSVC)
    target_compile_options(
        ${target}
            PRIVATE
                  /W4
                  /WX
    )
    endif()

    if((CMAKE_CXX_COMPILER_ID MATCHES GNU) OR
       (CMAKE_CXX_COMPILER_ID MATCHES Clang))
    target_compile_options(
        ${target}
            PRIVATE
               # compiler warnings
               # generic
              -Wall -Wextra     # reasonable and standard
              -Wpedantic        # non-standard cpp is used
              -Wmisleading-indentation
              -Wlogical-op      # logical op being used instead of bitwise
              -Wundef
              -Wuninitialized
              -Weffc++         # disabled because it would require skeleton changes
               # variables
              -Wshadow          # variable declaration shadows one from parent context
              -Wunused          # anything being unused
               # casts
              -Werror=old-style-cast  # C-style casts
              -Wcast-align      # performance problem casts
              -Wcast-qual
              -Wuseless-cast    # cast to same type
               # conversion
             #-Wconversion       # type conversion may lose data
              -Wsign-conversion  # sign conversion
              -Wdouble-promotion # implicit float to double
               # functions
              -Wunused-parameter
              -Werror=missing-declarations # missing function declaration in header files
              -Werror=redundant-decls
              -Werror=return-type
              -Werror=overloaded-virtual
               # inheritance
              -Wnon-virtual-dtor   # class with virtual functions has non-virt dtor
              -Woverloaded-virtual # overloaded (not override) virtual function
               # branches
              -Wduplicated-cond     # if/else chain has duplicated conditions
              -Wduplicated-branches # if/else chain has duplicated code
               # pointers
              -Wpointer-arith
              -Wnull-dereference
               # style
              -Wformat=2        # security issues around printf
              -Werror=date-time # usage of __DATE__ and __TIME__ macros
              -Werror=missing-field-initializers
               # logic
              -Wlogical-op
              -Wimplicit-fallthrough
              -Wduplicated-cond
              -Wduplicated-branches

               # other flags
              -ffunction-sections       # place each function into its own section
                                        # better ASLR but larger executables
              -fstack-protector-all     # emit check for buffer overflows on all functions
              -fstack-clash-protection  # emit check for stack clash attacks
    )
    endif()

    # add -Werror to release builds
    if((${CMAKE_BUILD_TYPE} MATCHES Release) OR
       (${CMAKE_BUILD_TYPE} MATCHES MinSizeRel))
       # currently disabled
       #target_compile_options(${target} PRIVATE -Werror)
    endif()

endfunction()