Merge lp:~tangent-org/libmemcached/1.0-build/ Build: jenkins-Libmemcached-187
[m6w6/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 AC_DEFUN([_APPEND_LINK_FLAGS_ERROR],
65 [AC_REQUIRE([AX_APPEND_LINK_FLAGS])
66 AX_APPEND_LINK_FLAGS([$1],[LIB],[-Werror])
67 ])
68
69 AC_DEFUN([_APPEND_COMPILE_FLAGS_ERROR],
70 [AC_REQUIRE([AX_APPEND_COMPILE_FLAGS])
71 AX_APPEND_COMPILE_FLAGS([$1],,[-Werror])
72 ])
73
74 # Everything above this does the heavy lifting, while what follows does the specifics.
75
76 AC_DEFUN([_HARDEN_LINKER_FLAGS],
77 [_APPEND_LINK_FLAGS_ERROR([-z relro -z now])
78 _APPEND_LINK_FLAGS_ERROR([-pie])
79 AS_IF([test "x$ac_cv_warnings_as_errors" = xyes],
80 [AX_APPEND_LINK_FLAGS([-Werror])])
81 ])
82
83 AC_DEFUN([_HARDEN_CC_COMPILER_FLAGS],
84 [AC_LANG_PUSH([C])dnl
85
86 AS_IF([test "x$ax_enable_debug" = xyes],
87 [CFLAGS=''
88 _APPEND_COMPILE_FLAGS_ERROR([-ggdb])
89 _APPEND_COMPILE_FLAGS_ERROR([-g])
90 _APPEND_COMPILE_FLAGS_ERROR([-O0])],
91 [_APPEND_COMPILE_FLAGS_ERROR([-g])
92 _APPEND_COMPILE_FLAGS_ERROR([-O2])])
93
94 _APPEND_COMPILE_FLAGS_ERROR([-Wno-pragmas])
95 _APPEND_COMPILE_FLAGS_ERROR([-Wall])
96 _APPEND_COMPILE_FLAGS_ERROR([-Wextra])
97 _APPEND_COMPILE_FLAGS_ERROR([-Wunknown-pragmas])
98 _APPEND_COMPILE_FLAGS_ERROR([-Wthis-test-should-fail])
99 _APPEND_COMPILE_FLAGS_ERROR([-std=c99])
100 # Anything below this comment please keep sorted.
101 _APPEND_COMPILE_FLAGS_ERROR([--param=ssp-buffer-size=1])
102 # _APPEND_COMPILE_FLAGS_ERROR([-Wmissing-format-attribute])
103 _APPEND_COMPILE_FLAGS_ERROR([-Wno-attributes])
104 _APPEND_COMPILE_FLAGS_ERROR([-Waddress])
105 _APPEND_COMPILE_FLAGS_ERROR([-Warray-bounds])
106 _APPEND_COMPILE_FLAGS_ERROR([-Wbad-function-cast])
107 # Not in use -Wc++-compat
108 _APPEND_COMPILE_FLAGS_ERROR([-Wchar-subscripts])
109 _APPEND_COMPILE_FLAGS_ERROR([-Wcomment])
110 _APPEND_COMPILE_FLAGS_ERROR([-Wfloat-equal])
111 _APPEND_COMPILE_FLAGS_ERROR([-Wformat-security])
112 _APPEND_COMPILE_FLAGS_ERROR([-Wformat=2])
113 _APPEND_COMPILE_FLAGS_ERROR([-Wlogical-op])
114 _APPEND_COMPILE_FLAGS_ERROR([-Wmaybe-uninitialized])
115 _APPEND_COMPILE_FLAGS_ERROR([-Wmissing-field-initializers])
116 AS_IF([test "x$MINGW" = xyes],
117 [_APPEND_COMPILE_FLAGS_ERROR([-Wno-missing-noreturn])],
118 [_APPEND_COMPILE_FLAGS_ERROR([-Wmissing-noreturn])])
119 _APPEND_COMPILE_FLAGS_ERROR([-Wmissing-prototypes])
120 _APPEND_COMPILE_FLAGS_ERROR([-Wnested-externs])
121 _APPEND_COMPILE_FLAGS_ERROR([-Wnormalized=id])
122 _APPEND_COMPILE_FLAGS_ERROR([-Wold-style-definition])
123 _APPEND_COMPILE_FLAGS_ERROR([-Woverride-init])
124 _APPEND_COMPILE_FLAGS_ERROR([-Wpointer-arith])
125 _APPEND_COMPILE_FLAGS_ERROR([-Wpointer-sign])
126 AS_IF([test "x$MINGW" = xyes],
127 [_APPEND_COMPILE_FLAGS_ERROR([-Wno-suggest-attribute=const])
128 _APPEND_COMPILE_FLAGS_ERROR([-Wno-suggest-attribute=noreturn])
129 _APPEND_COMPILE_FLAGS_ERROR([-Wno-suggest-attribute=pure])
130 _APPEND_COMPILE_FLAGS_ERROR([-Wno-redundant-decls])],
131 [_APPEND_COMPILE_FLAGS_ERROR([-Wredundant-decls])])
132
133 _APPEND_COMPILE_FLAGS_ERROR([-Wshadow])
134 _APPEND_COMPILE_FLAGS_ERROR([-Wshorten-64-to-32])
135 _APPEND_COMPILE_FLAGS_ERROR([-Wsign-compare])
136 _APPEND_COMPILE_FLAGS_ERROR([-Wstrict-overflow=1])
137 _APPEND_COMPILE_FLAGS_ERROR([-Wstrict-prototypes])
138 _APPEND_COMPILE_FLAGS_ERROR([-Wswitch-enum])
139 _APPEND_COMPILE_FLAGS_ERROR([-Wundef])
140 _APPEND_COMPILE_FLAGS_ERROR([-Wunused])
141 _APPEND_COMPILE_FLAGS_ERROR([-Wunused-result])
142 _APPEND_COMPILE_FLAGS_ERROR([-Wunused-variable])
143 _APPEND_COMPILE_FLAGS_ERROR([-Wwrite-strings])
144 _APPEND_COMPILE_FLAGS_ERROR([-floop-parallelize-all])
145 _APPEND_COMPILE_FLAGS_ERROR([-fwrapv])
146 _APPEND_COMPILE_FLAGS_ERROR([-fmudflapt])
147 _APPEND_COMPILE_FLAGS_ERROR([-pipe])
148
149 AS_IF([test "x$ax_enable_debug" = xno],
150 [AS_IF([test "x$ac_cv_vcs_checkout" = xyes],
151 [_APPEND_COMPILE_FLAGS_ERROR([-fstack-check])
152 AS_IF([test "x$ac_c_gcc_recent" = xyes],
153 [_APPEND_COMPILE_FLAGS_ERROR([-D_FORTIFY_SOURCE=2])
154 # _APPEND_COMPILE_FLAGS_ERROR([-Wstack-protector])
155 _APPEND_COMPILE_FLAGS_ERROR([-fstack-protector])
156 # _APPEND_COMPILE_FLAGS_ERROR([-fstack-protector-all])
157 ])])])
158
159 AS_IF([test "x$ac_cv_vcs_checkout" = xyes],
160 [_APPEND_COMPILE_FLAGS_ERROR([-fstack-check])],
161 [_APPEND_COMPILE_FLAGS_ERROR([-Wno-pragmas])])
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 [_APPEND_COMPILE_FLAGS_ERROR([-fstack-check])
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 ])