3 dnl Check whether the compiler supports particular flags.
5 dnl Provides RRA_PROG_CC_FLAG and RRA_PROG_LD_FLAG, which checks whether a
6 dnl compiler supports a given flag for either compilation or linking,
7 dnl respectively. If it does, the commands in the second argument are run.
8 dnl If not, the commands in the third argument are run.
10 dnl Provides RRA_PROG_CC_WARNINGS_FLAGS, which checks whether a compiler
11 dnl supports a large set of warning flags and sets the WARNINGS_CFLAGS
12 dnl substitution variable to all of the supported warning flags. (Note that
13 dnl this may be too aggressive for some people.)
15 dnl Depends on RRA_PROG_CC_CLANG.
17 dnl The canonical version of this file is maintained in the rra-c-util
18 dnl package, available at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
20 dnl Copyright 2016-2023 Russ Allbery <eagle@eyrie.org>
21 dnl Copyright 2006, 2009, 2016
22 dnl by Internet Systems Consortium, Inc. ("ISC")
24 dnl Permission to use, copy, modify, and/or distribute this software for any
25 dnl purpose with or without fee is hereby granted, provided that the above
26 dnl copyright notice and this permission notice appear in all copies.
28 dnl THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
29 dnl REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
30 dnl MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
31 dnl SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
32 dnl WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
33 dnl ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
34 dnl IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
36 dnl SPDX-License-Identifier: ISC
38 dnl Used to build the result cache name.
39 AC_DEFUN([_RRA_PROG_CC_FLAG_CACHE],
40 [translit([rra_cv_compiler_c_$1], [-=+,], [____])])
41 AC_DEFUN([_RRA_PROG_LD_FLAG_CACHE],
42 [translit([rra_cv_linker_c_$1], [-=+,], [____])])
44 dnl Check whether a given flag is supported by the compiler when compiling a C
46 AC_DEFUN([RRA_PROG_CC_FLAG],
47 [AC_REQUIRE([AC_PROG_CC])
48 AC_MSG_CHECKING([if $CC supports $1])
49 AC_CACHE_VAL([_RRA_PROG_CC_FLAG_CACHE([$1])],
51 AS_IF([test x"$CLANG" = xyes],
52 [CFLAGS="$CFLAGS -Werror=unknown-warning-option"])
54 [-Wno-*], [CFLAGS="$CFLAGS `AS_ECHO(["$1"]) | sed 's/-Wno-/-W/'`"],
55 [CFLAGS="$CFLAGS $1"])
56 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [int foo = 0;])],
57 [_RRA_PROG_CC_FLAG_CACHE([$1])=yes],
58 [_RRA_PROG_CC_FLAG_CACHE([$1])=no])
60 AC_MSG_RESULT([$_RRA_PROG_CC_FLAG_CACHE([$1])])
61 AS_IF([test x"$_RRA_PROG_CC_FLAG_CACHE([$1])" = xyes], [$2], [$3])])
63 dnl Check whether a given flag is supported by the compiler when linking an
65 AC_DEFUN([RRA_PROG_LD_FLAG],
66 [AC_REQUIRE([AC_PROG_CC])
67 AC_MSG_CHECKING([if $CC supports $1 for linking])
68 AC_CACHE_VAL([_RRA_PROG_LD_FLAG_CACHE([$1])],
69 [save_LDFLAGS=$LDFLAGS
70 AS_IF([test x"$CLANG" = xyes],
71 [LDFLAGS="$LDFLAGS -Werror=unknown-warning-option"])
73 AC_LINK_IFELSE([AC_LANG_PROGRAM([], [int foo = 0;])],
74 [_RRA_PROG_LD_FLAG_CACHE([$1])=yes],
75 [_RRA_PROG_LD_FLAG_CACHE([$1])=no])
76 LDFLAGS=$save_LDFLAGS])
77 AC_MSG_RESULT([$_RRA_PROG_LD_FLAG_CACHE([$1])])
78 AS_IF([test x"$_RRA_PROG_LD_FLAG_CACHE([$1])" = xyes], [$2], [$3])])
80 dnl Determine the full set of viable warning flags for the current compiler.
82 dnl This is based partly on personal preference and is a fairly aggressive set
83 dnl of warnings. Desirable CC warnings that can't be turned on due to other
86 dnl -Wsign-conversion Too many fiddly changes for the benefit
87 dnl -Wstack-protector Too many false positives from small buffers
89 dnl Last checked against gcc 13.2 (2023-12-25). -D_FORTIFY_SOURCE=2 enables
90 dnl warn_unused_result attribute markings on glibc functions on Linux, which
91 dnl catches a few more issues. Add -O2 because gcc won't find some warnings
92 dnl without optimization turned on.
94 dnl For Clang, we try to use -Weverything, but we have to disable some of the
97 dnl -Wcast-qual Some structs require casting away const
98 dnl -Wdisabled-macro-expansion Triggers on libc (sigaction.sa_handler)
99 dnl -Wpadded Not an actual problem
100 dnl -Wreserved-id-macros Autoconf sets several of these normally
101 dnl -Wreserved-identifer False positive with FD_ZERO
102 dnl -Wsign-conversion Too many fiddly changes for the benefit
103 dnl -Wtautological-pointer-compare False positives with for loops
104 dnl -Wundef Conflicts with Autoconf probe results
105 dnl -Wunreachable-code Happens with optional compilation
106 dnl -Wunreachable-code-return Other compilers get confused
107 dnl -Wunsafe-buffer-usage Intended for C++, not applicable to C
108 dnl -Wunused-macros Often used on suppressed branches
109 dnl -Wused-but-marked-unused Happens a lot with conditional code
111 dnl Sets WARNINGS_CFLAGS as a substitution variable.
112 AC_DEFUN([RRA_PROG_CC_WARNINGS_FLAGS],
113 [AC_REQUIRE([RRA_PROG_CC_CLANG])
114 AS_IF([test x"$CLANG" = xyes],
115 [WARNINGS_CFLAGS="-Werror"
117 [-Weverything -Wno-cast-qual -Wno-disabled-macro-expansion -Wno-padded
118 -Wno-sign-conversion -Wno-reserved-id-macro -Wno-reserved-identifier
119 -Wno-tautological-pointer-compare -Wno-undef -Wno-unreachable-code
120 -Wno-unreachable-code-return -Wno-unsafe-buffer-usage
121 -Wno-unused-macros -Wno-used-but-marked-unused],
122 [RRA_PROG_CC_FLAG(flag,
123 [WARNINGS_CFLAGS="${WARNINGS_CFLAGS} flag"])])],
124 [WARNINGS_CFLAGS="-g -O2 -D_FORTIFY_SOURCE=2 -Werror"
126 [-fstrict-overflow -fstrict-aliasing -fstrict-flex-arrays=3 -Wall
127 -Wextra -Wformat=2 -Wformat-overflow=2 -Wformat-signedness
128 -Wformat-truncation=2 -Wnull-dereference -Winit-self -Wswitch-enum
129 -Wstrict-overflow=5 -Wmissing-format-attribute -Walloc-zero
130 -Warith-conversion -Warray-bounds=3 -Wduplicated-branches
131 -Wduplicated-cond -Wtrampolines -Wfloat-equal
132 -Wdeclaration-after-statement -Wshadow -Wpointer-arith
133 -Wbad-function-cast -Wcast-align -Wwrite-strings -Wconversion
134 -Wno-sign-conversion -Wdate-time -Wjump-misses-init
135 -Wlogical-op -Wstrict-prototypes -Wold-style-definition
136 -Wmissing-prototypes -Wmissing-declarations -Wnormalized=nfc
137 -Wpacked -Wredundant-decls -Wrestrict -Wnested-externs -Winline
138 -Wvla -Wuse-after-free=3],
139 [RRA_PROG_CC_FLAG(flag,
140 [WARNINGS_CFLAGS="${WARNINGS_CFLAGS} flag"])])])
141 AC_SUBST([WARNINGS_CFLAGS])])