disable warning C5250: _BitScanReverse: intrinsic function not declared
[liba.git] / cmake / TargetWarnings.cmake
blob4e967928da554d506ec51e7e6ac9f207acdbd7a3
1 get_cmake_property(ENABLED_LANGUAGES ENABLED_LANGUAGES)
2 include(CheckCXXCompilerFlag)
3 include(CheckCCompilerFlag)
5 function(list_append var)
6   foreach(arg ${ARGN})
7     list(FIND ${var} ${arg} index)
8     if(${index} EQUAL -1)
9       list(APPEND ${var} ${arg})
10     endif()
11   endforeach()
12   set(${var} ${${var}} PARENT_SCOPE)
13 endfunction()
15 macro(warnings_flag_cc)
16   foreach(arg ${ARGN})
17     string(REPLACE "+" "x" var ${arg})
18     string(REGEX REPLACE "[^A-Za-z0-9_-]+" "-" var ${var})
19     list(FIND ENABLED_LANGUAGES C index)
20     if(${index} GREATER -1)
21       check_c_compiler_flag(${arg} cc${var})
22       if(cc${var})
23         list_append(WARNINGS_CC ${arg})
24       endif()
25     endif()
26     set(var)
27   endforeach()
28   set(index)
29 endmacro()
31 macro(warnings_flag_xx)
32   foreach(arg ${ARGN})
33     string(REPLACE "+" "x" var ${arg})
34     string(REGEX REPLACE "[^A-Za-z0-9_-]+" "-" var ${var})
35     list(FIND ENABLED_LANGUAGES CXX index)
36     if(${index} GREATER -1)
37       check_cxx_compiler_flag(${arg} xx${var})
38       if(xx${var})
39         list_append(WARNINGS_XX ${arg})
40       endif()
41     endif()
42     set(var)
43   endforeach()
44   set(index)
45 endmacro()
47 macro(warnings_flag_cx)
48   foreach(arg ${ARGN})
49     string(REPLACE "+" "x" var ${arg})
50     string(REGEX REPLACE "[^A-Za-z0-9_-]+" "-" var ${var})
51     list(FIND ENABLED_LANGUAGES C index)
52     if(${index} GREATER -1)
53       check_c_compiler_flag(${arg} cc${var})
54       if(cc${var})
55         list_append(WARNINGS_CC ${arg})
56       endif()
57     endif()
58     list(FIND ENABLED_LANGUAGES CXX index)
59     if(${index} GREATER -1)
60       check_cxx_compiler_flag(${arg} xx${var})
61       if(xx${var})
62         list_append(WARNINGS_XX ${arg})
63       endif()
64     endif()
65     set(var)
66   endforeach()
67   set(index)
68 endmacro()
70 if(CMAKE_C_COMPILER_ID MATCHES "(ARM|Apple)?[Cc]lang" OR CMAKE_C_COMPILER_ID MATCHES "IntelLLVM" OR
71   CMAKE_CXX_COMPILER_ID MATCHES "(ARM|Apple)?[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES "IntelLLVM")
72   # https://releases.llvm.org/4.0.1/tools/clang/DiagnosticsReference.html
73   # https://clang.llvm.org/docs/DiagnosticsReference.html
74   warnings_flag_cx(-Weverything)
75   if(MINGW)
76     warnings_flag_cx(-Wno-used-but-marked-unused)
77   endif()
78   warnings_flag_cc(-Wno-declaration-after-statement)
79   warnings_flag_xx(-Wno-c++98-compat-pedantic)
80   warnings_flag_cx(-Wno-unsafe-buffer-usage)
81 elseif(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
82   # https://gcc.gnu.org/onlinedocs/gcc-3.0.3/gcc/Warning-Options.html
83   # https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
84   warnings_flag_cx(-pedantic -Wall)
85   warnings_flag_cx(-Wextra) # 3.4+
86   warnings_flag_xx(-Weffc++) # 2.95+
87   warnings_flag_cc(-Winit-self) # 3.4+
88   warnings_flag_cc(-Wc++-compat) # 4.1+
89   warnings_flag_cc(-Wbad-function-cast) # 2.95+
90   warnings_flag_cc(-Wstrict-prototypes) # 2.95+
91   warnings_flag_cc(-Wold-style-definition) # 3.4+
92   warnings_flag_cc(-Wmissing-prototypes) # 2.95+
93   warnings_flag_cc(-Wnested-externs) # 2.95+
94   warnings_flag_cx(-Wdouble-promotion) # 4.6+
95   warnings_flag_cx(-Wnull-dereference) # 6.1+
96   warnings_flag_cx(-Wmissing-include-dirs) # 4.0+
97   warnings_flag_cx(-Wswitch-default) # 3.3+
98   warnings_flag_cx(-Wswitch-enum) # 3.3+
99   warnings_flag_cx(-Walloca) # 7.1+
100   warnings_flag_cx(-Wattributes) # 4.1+
101   warnings_flag_cx(-Wfloat-equal) # 3.0+
102   warnings_flag_cx(-Wshadow) # 2.95+
103   warnings_flag_cx(-Wundef) # 2.95+
104   warnings_flag_cx(-Wunused) # 2.95+
105   warnings_flag_cx(-Wunused-macros) # 7.1+
106   warnings_flag_cx(-Wcast-qual) # 2.95+
107   warnings_flag_cx(-Wcast-align) # 2.95+
108   warnings_flag_cx(-Wconversion) # 2.95+
109   warnings_flag_cx(-Wdate-time) # 4.9+
110   warnings_flag_cx(-Waggregate-return) # 2.95+
111   warnings_flag_cx(-Wmissing-declarations) # 2.95+
112   warnings_flag_cx(-Wpacked) # 3.0+
113   warnings_flag_cx(-Wpadded) # 3.0+
114   warnings_flag_cx(-Wredundant-decls) # 2.95+
115   warnings_flag_cx(-Winline) # 2.95+
116   warnings_flag_cx(-Winvalid-pch) # 3.4+
117   warnings_flag_cx(-Wdisabled-optimization) # 3.0+
118   warnings_flag_cx(-Wstack-protector) # 4.1+
119   warnings_flag_cx(-Wstringop-overflow) # 7.1+
120   warnings_flag_cx(-Wstringop-truncation) # 8.1+
121   warnings_flag_cx(-Walloc-zero) # 7.1+
122   warnings_flag_cx(-Wduplicated-branches) # 7.1+
123   warnings_flag_cx(-Wduplicated-cond) # 6.1+
124   warnings_flag_cx(-Wzero-length-bounds) # 10.1+
125   warnings_flag_cx(-Wtrampolines) # 4.6+
126   warnings_flag_cx(-Wunsafe-loop-optimizations) # 4.1+
127   warnings_flag_cx(-Wno-pedantic-ms-format) # 4.4+
128   warnings_flag_cx(-Wlogical-op) # 4.3+
129 elseif(CMAKE_C_COMPILER_ID MATCHES "MSVC" OR CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
130   # https://learn.microsoft.com/cpp/error-messages/compiler-warnings
131   warnings_flag_cx(/sdl /Wall)
132   warnings_flag_cx(/wd4464) # relative include path contains '..'
133   warnings_flag_cx(/wd4514) # 'function' : unreferenced inline function has been removed
134   warnings_flag_cx(/wd4710) # 'function' : function not inlined
135   warnings_flag_cx(/wd4711) # function 'function' selected for inline expansion
136   warnings_flag_cx(/wd4820) # 'bytes' bytes padding added after construct 'member_name'
137   if(MSVC_VERSION GREATER 1911) # 15.5
138     warnings_flag_cx(/wd5039) # 'function': pointer or reference to potentially throwing function passed to extern C function under -EHc
139   endif()
140   if(MSVC_VERSION GREATER 1913) # 15.7
141     warnings_flag_cx(/wd5045) # Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified
142   endif()
143   if(MSVC_VERSION GREATER 1900) # 15.0
144     warnings_flag_cx(/diagnostics:caret)
145   endif()
146 elseif(CMAKE_C_COMPILER_ID MATCHES "TinyCC")
147   # https://bellard.org/tcc/tcc-doc.html
148   warnings_flag_cc(-Wall -Wwrite-strings)
149 endif()
151 function(target_compile_warnings)
152   if(CMAKE_VERSION VERSION_LESS 3.3)
153     return()
154   endif()
155   foreach(target ${ARGN})
156     target_compile_options(${target} PRIVATE
157       $<$<COMPILE_LANGUAGE:C>:${WARNINGS_CC}>
158       $<$<COMPILE_LANGUAGE:CXX>:${WARNINGS_XX}>
159     )
160   endforeach()
161 endfunction()