]> eyrie.org Git - kerberos/krb5-strength.git/blob - m4/cc-flags.m4
c5a54cbb39c45d8b6ea8358a5025738641871ba8
[kerberos/krb5-strength.git] / m4 / cc-flags.m4
1 # serial 2
2
3 dnl Check whether the compiler supports particular flags.
4 dnl
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.
9 dnl
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.)
14 dnl
15 dnl Depends on RRA_PROG_CC_CLANG.
16 dnl
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/>.
19 dnl
20 dnl Copyright 2016-2023 Russ Allbery <eagle@eyrie.org>
21 dnl Copyright 2006, 2009, 2016
22 dnl     by Internet Systems Consortium, Inc. ("ISC")
23 dnl
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.
27 dnl
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.
35 dnl
36 dnl SPDX-License-Identifier: ISC
37
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], [-=+,], [____])])
43
44 dnl Check whether a given flag is supported by the compiler when compiling a C
45 dnl source file.
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])],
50     [save_CFLAGS=$CFLAGS
51      AS_IF([test x"$CLANG" = xyes],
52         [CFLAGS="$CFLAGS -Werror=unknown-warning-option"])
53      AS_CASE([$1],
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])
59      CFLAGS=$save_CFLAGS])
60  AC_MSG_RESULT([$_RRA_PROG_CC_FLAG_CACHE([$1])])
61  AS_IF([test x"$_RRA_PROG_CC_FLAG_CACHE([$1])" = xyes], [$2], [$3])])
62
63 dnl Check whether a given flag is supported by the compiler when linking an
64 dnl executable.
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"])
72      LDFLAGS="$LDFLAGS $1"
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])])
79
80 dnl Determine the full set of viable warning flags for the current compiler.
81 dnl
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
84 dnl problems:
85 dnl
86 dnl   -Wsign-conversion  Too many fiddly changes for the benefit
87 dnl   -Wstack-protector  Too many false positives from small buffers
88 dnl
89 dnl Last checked against gcc 9.2.1 (2019-09-01).  -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.
93 dnl
94 dnl For Clang, we try to use -Weverything, but we have to disable some of the
95 dnl warnings:
96 dnl
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
110 dnl
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"
116      m4_foreach_w([flag],
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"
125      m4_foreach_w([flag],
126         [-fstrict-overflow -fstrict-aliasing -Wall -Wextra -Wformat=2
127          -Wformat-overflow=2 -Wformat-signedness -Wformat-truncation=2
128          -Wnull-dereference -Winit-self -Wswitch-enum -Wstrict-overflow=5
129          -Wmissing-format-attribute -Walloc-zero -Wduplicated-branches
130          -Wduplicated-cond -Wtrampolines -Wfloat-equal
131          -Wdeclaration-after-statement -Wshadow -Wpointer-arith
132          -Wbad-function-cast -Wcast-align -Wwrite-strings -Wconversion
133          -Wno-sign-conversion -Wdate-time -Wjump-misses-init -Wlogical-op
134          -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes
135          -Wmissing-declarations -Wnormalized=nfc -Wpacked -Wredundant-decls
136          -Wrestrict -Wnested-externs -Winline -Wvla],
137         [RRA_PROG_CC_FLAG(flag,
138             [WARNINGS_CFLAGS="${WARNINGS_CFLAGS} flag"])])])
139  AC_SUBST([WARNINGS_CFLAGS])])