Cleanup around the timeout code.
[awesomized/libmemcached] / m4 / ax_harden_compiler_flags.m4
1 # ===========================================================================
2 # https://github.com/BrianAker/ddm4/
3 # ===========================================================================
4 #
5 # SYNOPSIS
6 #
7 # AX_HARDEN_COMPILER_FLAGS() AX_HARDEN_LINKER_FLAGS()
8 #
9 # DESCRIPTION
10 #
11 # Any compiler flag that "hardens" or tests code. C99 is assumed.
12 #
13 # NOTE: Implementation based on AX_APPEND_FLAG.
14 #
15 # LICENSE
16 #
17 # Copyright (C) 2012 Brian Aker
18 # All rights reserved.
19 #
20 # Redistribution and use in source and binary forms, with or without
21 # modification, are permitted provided that the following conditions are
22 # met:
23 #
24 # * Redistributions of source code must retain the above copyright
25 # notice, this list of conditions and the following disclaimer.
26 #
27 # * Redistributions in binary form must reproduce the above
28 # copyright notice, this list of conditions and the following disclaimer
29 # in the documentation and/or other materials provided with the
30 # distribution.
31 #
32 # * The names of its contributors may not be used to endorse or
33 # promote products derived from this software without specific prior
34 # written permission.
35 #
36 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
39 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
40 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
43 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
44 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
45 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
46 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47
48 # The Following flags are not checked for
49 # -Wdeclaration-after-statement is counter to C99
50 # _APPEND_COMPILE_FLAGS_ERROR([-std=c++11]) -- Not ready yet
51 # _APPEND_COMPILE_FLAGS_ERROR([-pedantic]) --
52 # ? _APPEND_COMPILE_FLAGS_ERROR([-Wlong-long]) -- Don't turn on for
53 # compatibility issues memcached_stat_st
54
55 #serial 7
56
57 AC_DEFUN([_WARNINGS_AS_ERRORS],
58 [AC_CACHE_CHECK([if all warnings into errors],[ac_cv_warnings_as_errors],
59 [AS_IF([test "x$ac_cv_vcs_checkout" = xyes],[ac_cv_warnings_as_errors=yes],
60 [ac_cv_warnings_as_errors=no])
61 ])
62 ])
63
64 # Note: Should this be LIBS or LDFLAGS?
65 AC_DEFUN([_APPEND_LINK_FLAGS_ERROR],
66 [AC_REQUIRE([AX_APPEND_LINK_FLAGS])
67 AX_APPEND_LINK_FLAGS([$1],[LDFLAGS],[-Werror])
68 ])
69
70 AC_DEFUN([_APPEND_COMPILE_FLAGS_ERROR],
71 [AC_REQUIRE([AX_APPEND_COMPILE_FLAGS])
72 AX_APPEND_COMPILE_FLAGS([$1],,[-Werror])
73 ])
74
75 # Everything above this does the heavy lifting, while what follows does the specifics.
76
77 AC_DEFUN([_HARDEN_LINKER_FLAGS],
78 [_APPEND_LINK_FLAGS_ERROR([-z relro -z now])
79 #_APPEND_LINK_FLAGS_ERROR([-pie])
80 AS_IF([test "x$ac_cv_warnings_as_errors" = xyes],
81 [AX_APPEND_LINK_FLAGS([-Werror])])
82 ])
83
84 AC_DEFUN([_HARDEN_CC_COMPILER_FLAGS],
85 [AC_LANG_PUSH([C])dnl
86
87 AS_IF([test "x$ax_enable_debug" = xyes],
88 [CFLAGS=''
89 _APPEND_COMPILE_FLAGS_ERROR([-ggdb])
90 _APPEND_COMPILE_FLAGS_ERROR([-g])
91 _APPEND_COMPILE_FLAGS_ERROR([-O0])],
92 [_APPEND_COMPILE_FLAGS_ERROR([-g])
93 _APPEND_COMPILE_FLAGS_ERROR([-O2])])
94
95 AS_IF([test "x$ac_cv_vcs_checkout" = xyes],
96 [_APPEND_COMPILE_FLAGS_ERROR([-fstack-check])],
97 [_APPEND_COMPILE_FLAGS_ERROR([-Wno-pragmas])])
98
99 _APPEND_COMPILE_FLAGS_ERROR([-Wall])
100 _APPEND_COMPILE_FLAGS_ERROR([-Wextra])
101 _APPEND_COMPILE_FLAGS_ERROR([-Wunknown-pragmas])
102 _APPEND_COMPILE_FLAGS_ERROR([-Wthis-test-should-fail])
103 _APPEND_COMPILE_FLAGS_ERROR([-std=c99])
104 # Anything below this comment please keep sorted.
105 _APPEND_COMPILE_FLAGS_ERROR([--param=ssp-buffer-size=1])
106 # _APPEND_COMPILE_FLAGS_ERROR([-Wmissing-format-attribute])
107 _APPEND_COMPILE_FLAGS_ERROR([-Wno-attributes])
108 _APPEND_COMPILE_FLAGS_ERROR([-Waddress])
109 _APPEND_COMPILE_FLAGS_ERROR([-Warray-bounds])
110 _APPEND_COMPILE_FLAGS_ERROR([-Wbad-function-cast])
111 # Not in use -Wc++-compat
112 _APPEND_COMPILE_FLAGS_ERROR([-Wchar-subscripts])
113 _APPEND_COMPILE_FLAGS_ERROR([-Wcomment])
114 _APPEND_COMPILE_FLAGS_ERROR([-Wfloat-equal])
115 _APPEND_COMPILE_FLAGS_ERROR([-Wformat-security])
116 _APPEND_COMPILE_FLAGS_ERROR([-Wformat=2])
117 _APPEND_COMPILE_FLAGS_ERROR([-Wlogical-op])
118 _APPEND_COMPILE_FLAGS_ERROR([-Wmaybe-uninitialized])
119 _APPEND_COMPILE_FLAGS_ERROR([-Wmissing-field-initializers])
120 AS_IF([test "x$MINGW" = xyes],
121 [_APPEND_COMPILE_FLAGS_ERROR([-Wno-missing-noreturn])],
122 [_APPEND_COMPILE_FLAGS_ERROR([-Wmissing-noreturn])])
123 _APPEND_COMPILE_FLAGS_ERROR([-Wmissing-prototypes])
124 _APPEND_COMPILE_FLAGS_ERROR([-Wnested-externs])
125 _APPEND_COMPILE_FLAGS_ERROR([-Wnormalized=id])
126 _APPEND_COMPILE_FLAGS_ERROR([-Wold-style-definition])
127 _APPEND_COMPILE_FLAGS_ERROR([-Woverride-init])
128 _APPEND_COMPILE_FLAGS_ERROR([-Wpointer-arith])
129 _APPEND_COMPILE_FLAGS_ERROR([-Wpointer-sign])
130 AS_IF([test "x$MINGW" = xyes],
131 [_APPEND_COMPILE_FLAGS_ERROR([-Wno-suggest-attribute=const])
132 _APPEND_COMPILE_FLAGS_ERROR([-Wno-suggest-attribute=noreturn])
133 _APPEND_COMPILE_FLAGS_ERROR([-Wno-suggest-attribute=pure])
134 _APPEND_COMPILE_FLAGS_ERROR([-Wno-redundant-decls])],
135 [_APPEND_COMPILE_FLAGS_ERROR([-Wredundant-decls])])
136
137 _APPEND_COMPILE_FLAGS_ERROR([-Wshadow])
138 _APPEND_COMPILE_FLAGS_ERROR([-Wshorten-64-to-32])
139 _APPEND_COMPILE_FLAGS_ERROR([-Wsign-compare])
140 _APPEND_COMPILE_FLAGS_ERROR([-Wstrict-overflow=1])
141 _APPEND_COMPILE_FLAGS_ERROR([-Wstrict-prototypes])
142 _APPEND_COMPILE_FLAGS_ERROR([-Wswitch-enum])
143 _APPEND_COMPILE_FLAGS_ERROR([-Wundef])
144 _APPEND_COMPILE_FLAGS_ERROR([-Wunused])
145 _APPEND_COMPILE_FLAGS_ERROR([-Wunused-result])
146 _APPEND_COMPILE_FLAGS_ERROR([-Wunused-variable])
147 _APPEND_COMPILE_FLAGS_ERROR([-Wwrite-strings])
148 _APPEND_COMPILE_FLAGS_ERROR([-floop-parallelize-all])
149 _APPEND_COMPILE_FLAGS_ERROR([-fwrapv])
150 _APPEND_COMPILE_FLAGS_ERROR([-fmudflapt])
151 _APPEND_COMPILE_FLAGS_ERROR([-pipe])
152
153 AS_IF([test "x$ax_enable_debug" = xno],
154 [AS_IF([test "x$ac_cv_vcs_checkout" = xyes],
155 [AS_IF([test "x${target_os}" != "xmingw"],
156 [AS_IF([test "x$ac_c_gcc_recent" = xyes],
157 [_APPEND_COMPILE_FLAGS_ERROR([-D_FORTIFY_SOURCE=2])
158 #_APPEND_COMPILE_FLAGS_ERROR([-Wstack-protector])
159 #_APPEND_COMPILE_FLAGS_ERROR([-fstack-protector])
160 _APPEND_COMPILE_FLAGS_ERROR([-fstack-protector-all])
161 ])])])])
162
163 AS_IF([test "x$ac_cv_warnings_as_errors" = xyes],
164 [AX_APPEND_FLAG([-Werror])])
165
166 AC_LANG_POP([C])
167 ])
168
169 AC_DEFUN([_HARDEN_CXX_COMPILER_FLAGS],
170 [AC_LANG_PUSH([C++])
171 AC_REQUIRE([_APPEND_COMPILE_FLAGS_ERROR])
172
173 AS_IF([test "x$ax_enable_debug" = xyes],
174 [CXXFLAGS=''
175 _APPEND_COMPILE_FLAGS_ERROR([-ggdb])
176 _APPEND_COMPILE_FLAGS_ERROR([-g])
177 _APPEND_COMPILE_FLAGS_ERROR([-O0])],
178 [_APPEND_COMPILE_FLAGS_ERROR([-g])
179 _APPEND_COMPILE_FLAGS_ERROR([-O2])])
180
181 AS_IF([test "x$ac_cv_vcs_checkout" = xyes],
182 [_APPEND_COMPILE_FLAGS_ERROR([-fstack-check])],
183 [_APPEND_COMPILE_FLAGS_ERROR([-Wno-pragmas])])
184
185 _APPEND_COMPILE_FLAGS_ERROR([-Wall])
186 _APPEND_COMPILE_FLAGS_ERROR([-Wextra])
187 _APPEND_COMPILE_FLAGS_ERROR([-Wunknown-pragmas])
188 _APPEND_COMPILE_FLAGS_ERROR([-Wthis-test-should-fail])
189 # Anything below this comment please keep sorted.
190 _APPEND_COMPILE_FLAGS_ERROR([--param=ssp-buffer-size=1])
191 # _APPEND_COMPILE_FLAGS_ERROR([-Wmissing-format-attribute])
192 _APPEND_COMPILE_FLAGS_ERROR([-Wno-attributes])
193 _APPEND_COMPILE_FLAGS_ERROR([-Waddress])
194 _APPEND_COMPILE_FLAGS_ERROR([-Warray-bounds])
195 _APPEND_COMPILE_FLAGS_ERROR([-Wchar-subscripts])
196 _APPEND_COMPILE_FLAGS_ERROR([-Wcomment])
197 _APPEND_COMPILE_FLAGS_ERROR([-Wctor-dtor-privacy])
198 _APPEND_COMPILE_FLAGS_ERROR([-Wfloat-equal])
199 _APPEND_COMPILE_FLAGS_ERROR([-Wformat=2])
200 _APPEND_COMPILE_FLAGS_ERROR([-Wmaybe-uninitialized])
201 _APPEND_COMPILE_FLAGS_ERROR([-Wmissing-field-initializers])
202 _APPEND_COMPILE_FLAGS_ERROR([-Wlogical-op])
203 _APPEND_COMPILE_FLAGS_ERROR([-Wnon-virtual-dtor])
204 _APPEND_COMPILE_FLAGS_ERROR([-Wnormalized=id])
205 _APPEND_COMPILE_FLAGS_ERROR([-Woverloaded-virtual])
206 _APPEND_COMPILE_FLAGS_ERROR([-Wpointer-arith])
207 AS_IF([test "x$MINGW" = xyes],
208 [_APPEND_COMPILE_FLAGS_ERROR([-Wno-suggest-attribute=const])
209 _APPEND_COMPILE_FLAGS_ERROR([-Wno-missing-noreturn])
210 _APPEND_COMPILE_FLAGS_ERROR([-Wmissing-noreturn])
211 _APPEND_COMPILE_FLAGS_ERROR([-Wno-suggest-attribute=noreturn])
212 _APPEND_COMPILE_FLAGS_ERROR([-Wno-error=suggest-attribute=noreturn])
213 _APPEND_COMPILE_FLAGS_ERROR([-Wno-redundant-decls])],
214 [_APPEND_COMPILE_FLAGS_ERROR([-Wredundant-decls])])
215 _APPEND_COMPILE_FLAGS_ERROR([-Wshadow])
216 _APPEND_COMPILE_FLAGS_ERROR([-Wshorten-64-to-32])
217 _APPEND_COMPILE_FLAGS_ERROR([-Wsign-compare])
218 _APPEND_COMPILE_FLAGS_ERROR([-Wstrict-overflow=1])
219 _APPEND_COMPILE_FLAGS_ERROR([-Wswitch-enum])
220 _APPEND_COMPILE_FLAGS_ERROR([-Wundef])
221 _APPEND_COMPILE_FLAGS_ERROR([-Wc++11-compat])
222 _APPEND_COMPILE_FLAGS_ERROR([-Wunused])
223 _APPEND_COMPILE_FLAGS_ERROR([-Wunused-result])
224 _APPEND_COMPILE_FLAGS_ERROR([-Wunused-variable])
225 _APPEND_COMPILE_FLAGS_ERROR([-Wwrite-strings])
226 _APPEND_COMPILE_FLAGS_ERROR([-Wformat-security])
227 _APPEND_COMPILE_FLAGS_ERROR([-floop-parallelize-all])
228 _APPEND_COMPILE_FLAGS_ERROR([-fwrapv])
229 _APPEND_COMPILE_FLAGS_ERROR([-fmudflapt])
230 _APPEND_COMPILE_FLAGS_ERROR([-pipe])
231
232 AS_IF([test "x$ax_enable_debug" = xno],
233 [AS_IF([test "x$ac_cv_vcs_checkout" = xyes],
234 [AS_IF([test "x${target_os}" != "xmingw"],
235 [AS_IF([test "x$ac_c_gcc_recent" = xyes],
236 [_APPEND_COMPILE_FLAGS_ERROR([-D_FORTIFY_SOURCE=2])
237 #_APPEND_COMPILE_FLAGS_ERROR([-Wstack-protector])
238 #_APPEND_COMPILE_FLAGS_ERROR([-fstack-protector])
239 _APPEND_COMPILE_FLAGS_ERROR([-fstack-protector-all])
240 ])])])])
241
242 AS_IF([test "x$ac_cv_warnings_as_errors" = xyes],
243 [AX_APPEND_FLAG([-Werror])])
244 AC_LANG_POP([C++])
245 ])
246
247 # All of the heavy lifting happens in _HARDEN_LINKER_FLAGS,
248 # _HARDEN_CC_COMPILER_FLAGS, _HARDEN_CXX_COMPILER_FLAGS
249 AC_DEFUN([AX_HARDEN_COMPILER_FLAGS],
250 [AC_PREREQ([2.63])dnl
251 AC_REQUIRE([_WARNINGS_AS_ERRORS])
252 AC_REQUIRE([AX_APPEND_LINK_FLAGS])
253 AC_REQUIRE([AX_COMPILER_VERSION])
254 AC_REQUIRE([AX_DEBUG])
255 AC_REQUIRE([AX_VCS_CHECKOUT])
256
257 AC_REQUIRE([gl_VISIBILITY])
258 AS_IF([test -n "$CFLAG_VISIBILITY"],[CPPFLAGS="$CPPFLAGS $CFLAG_VISIBILITY"])
259
260 AC_REQUIRE([_HARDEN_LINKER_FLAGS])
261 AC_REQUIRE([_HARDEN_CC_COMPILER_FLAGS])
262 AC_REQUIRE([_HARDEN_CXX_COMPILER_FLAGS])
263 ])