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