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