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