Merge in updates from m4.
[m6w6/libmemcached] / m4 / ax_harden_compiler_flags.m4
1 # ===========================================================================
2 # http://www.gnu.org/software/autoconf-archive/ax_append_flag.html
3 # ===========================================================================
4 #
5 # SYNOPSIS
6 #
7 # AX_HARDEN_COMPILER_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 # AX_APPEND_COMPILE_FLAGS([-std=c++11]) -- Not ready yet
51 # AX_APPEND_COMPILE_FLAGS([-pedantic]) -- ?
52 # AX_APPEND_COMPILE_FLAGS([-Wstack-protector]) -- Issues on 32bit compile
53 # AX_APPEND_COMPILE_FLAGS([-fstack-protector-all]) -- Issues on 32bit compile
54 # AX_APPEND_COMPILE_FLAGS([-Wlong-long]) -- Don't turn on for compatibility issues memcached_stat_st
55
56 #serial 2
57 AC_DEFUN([AX_HARDEN_LINKER_FLAGS], [
58 AC_REQUIRE([AX_CHECK_LINK_FLAG])
59 AC_REQUIRE([AX_VCS_CHECKOUT])
60 AC_REQUIRE([AX_DEBUG])
61
62 AS_IF([test "$ac_cv_vcs_checkout" = yes], [
63 AX_CHECK_LINK_FLAG([-Werror])
64 ])
65 AX_CHECK_LINK_FLAG([-z relro -z now])
66 ])
67
68 AC_DEFUN([AX_HARDEN_C_COMPILER_FLAGS], [
69 AC_REQUIRE([AX_APPEND_COMPILE_FLAGS])
70 AC_REQUIRE([AX_HARDEN_LINKER_FLAGS])
71
72 AC_LANG_PUSH([C])
73 AS_IF([test "$ax_with_debug" = yes], [
74 AX_APPEND_COMPILE_FLAGS([-O0])],[
75 AX_APPEND_COMPILE_FLAGS([-O2])
76 ])
77
78 ac_cv_warnings_as_errors=no
79 AS_IF([test "$ac_cv_vcs_checkout" = yes], [
80 AX_APPEND_COMPILE_FLAGS([-Werror])
81 ac_cv_warnings_as_errors=yes
82 ])
83
84 AX_APPEND_COMPILE_FLAGS([-Wall])
85 AX_APPEND_COMPILE_FLAGS([-Wextra])
86 AX_APPEND_COMPILE_FLAGS([-std=c99])
87 AX_APPEND_COMPILE_FLAGS([-Wbad-function-cast])
88 AX_APPEND_COMPILE_FLAGS([-Wmissing-prototypes])
89 AX_APPEND_COMPILE_FLAGS([-Wnested-externs])
90 AX_APPEND_COMPILE_FLAGS([-Wold-style-definition])
91 AX_APPEND_COMPILE_FLAGS([-Woverride-init])
92 AX_APPEND_COMPILE_FLAGS([-Wstrict-prototypes])
93 AX_APPEND_COMPILE_FLAGS([-Wlogical-op])
94 AC_LANG_POP
95
96 ])
97
98 AC_DEFUN([AX_HARDEN_CC_COMPILER_FLAGS], [
99 AC_REQUIRE([AX_HARDEN_C_COMPILER_FLAGS])
100 AC_LANG_PUSH([C++])
101
102 AS_IF([test "$ax_with_debug" = yes], [
103 AX_APPEND_COMPILE_FLAGS([-O0])],[
104 AX_APPEND_COMPILE_FLAGS([-O2])
105 AX_APPEND_COMPILE_FLAGS([-D_FORTIFY_SOURCE=2])
106 ])
107
108 AS_IF([test "$ac_cv_vcs_checkout" = yes], [
109 AX_APPEND_COMPILE_FLAGS([-Werror])
110 ])
111
112 AX_APPEND_COMPILE_FLAGS([-Wall])
113 AX_APPEND_COMPILE_FLAGS([-Wextra])
114 AX_APPEND_COMPILE_FLAGS([-Wpragmas])
115 AX_APPEND_COMPILE_FLAGS([--paramssp-buffer-size=1])
116 AX_APPEND_COMPILE_FLAGS([-Waddress])
117 AX_APPEND_COMPILE_FLAGS([-Warray-bounds])
118 AX_APPEND_COMPILE_FLAGS([-Wchar-subscripts])
119 AX_APPEND_COMPILE_FLAGS([-Wcomment])
120 AX_APPEND_COMPILE_FLAGS([-Wctor-dtor-privacy])
121 AX_APPEND_COMPILE_FLAGS([-Wfloat-equal])
122 AX_APPEND_COMPILE_FLAGS([-Wformat=2])
123 AX_APPEND_COMPILE_FLAGS([-Wmaybe-uninitialized])
124 AX_APPEND_COMPILE_FLAGS([-Wmissing-field-initializers])
125 AX_APPEND_COMPILE_FLAGS([-Wmissing-noreturn])
126 AX_APPEND_COMPILE_FLAGS([-Wlogical-op])
127 AX_APPEND_COMPILE_FLAGS([-Wnon-virtual-dtor])
128 AX_APPEND_COMPILE_FLAGS([-Wnormalized=id])
129 AX_APPEND_COMPILE_FLAGS([-Woverloaded-virtual])
130 AX_APPEND_COMPILE_FLAGS([-Wpointer-arith])
131 AX_APPEND_COMPILE_FLAGS([-Wredundant-decls])
132 AX_APPEND_COMPILE_FLAGS([-Wshadow])
133 AX_APPEND_COMPILE_FLAGS([-Wshorten-64-to-32])
134 AX_APPEND_COMPILE_FLAGS([-Wsign-compare])
135 AX_APPEND_COMPILE_FLAGS([-Wstrict-overflow=1])
136 AX_APPEND_COMPILE_FLAGS([-Wswitch-enum])
137 AX_APPEND_COMPILE_FLAGS([-Wundef])
138 AX_APPEND_COMPILE_FLAGS([-Wunused-result])
139 AX_APPEND_COMPILE_FLAGS([-Wunused-variable])
140 AX_APPEND_COMPILE_FLAGS([-Wwrite-strings])
141 AX_APPEND_COMPILE_FLAGS([-floop-parallelize-all])
142 AX_APPEND_COMPILE_FLAGS([-fwrapv])
143 AX_APPEND_COMPILE_FLAGS([-ggdb])
144 AC_LANG_POP
145 ])