]> eyrie.org Git - kerberos/krb5-strength.git/blob - m4/cc-flags.m4
New upstream version 3.1
[kerberos/krb5-strength.git] / m4 / cc-flags.m4
1 dnl Check whether the compiler supports particular flags.
2 dnl
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.
6 dnl
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.)
11 dnl
12 dnl Depends on RRA_PROG_CC_CLANG.
13 dnl
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/>.
16 dnl
17 dnl Copyright 2016 Russ Allbery <eagle@eyrie.org>
18 dnl Copyright 2006, 2009, 2016
19 dnl     by Internet Systems Consortium, Inc. ("ISC")
20 dnl
21 dnl Permission to use, copy, modify, and 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.
24 dnl
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.
32
33 dnl Used to build the result cache name.
34 AC_DEFUN([_RRA_PROG_CC_FLAG_CACHE],
35 [translit([rra_cv_compiler_c_$1], [-=], [__])])
36
37 dnl Check whether a given flag is supported by the complier.
38 AC_DEFUN([RRA_PROG_CC_FLAG],
39 [AC_REQUIRE([AC_PROG_CC])
40  AC_MSG_CHECKING([if $CC supports $1])
41  AC_CACHE_VAL([_RRA_PROG_CC_FLAG_CACHE([$1])],
42     [save_CFLAGS=$CFLAGS
43      AS_CASE([$1],
44         [-Wno-*], [CFLAGS="$CFLAGS `echo "$1" | sed 's/-Wno-/-W/'`"],
45         [*],      [CFLAGS="$CFLAGS $1"])
46      AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [int foo = 0;])],
47         [_RRA_PROG_CC_FLAG_CACHE([$1])=yes],
48         [_RRA_PROG_CC_FLAG_CACHE([$1])=no])
49      CFLAGS=$save_CFLAGS])
50  AC_MSG_RESULT([$_RRA_PROG_CC_FLAG_CACHE([$1])])
51  AS_IF([test x"$_RRA_PROG_CC_FLAG_CACHE([$1])" = xyes], [$2], [$3])])
52
53 dnl Determine the full set of viable warning flags for the current compiler.
54 dnl
55 dnl This is based partly on personal preference and is a fairly aggressive set
56 dnl of warnings.  Desirable warnings that can't be turned on due to other
57 dnl problems:
58 dnl
59 dnl   -Wconversion       http://bugs.debian.org/488884 (htons warnings)
60 dnl   -Wsign-conversion  Too much noise from ssize_t and flag variables
61 dnl   -Wstack-protector  Too many false positives from small buffers
62 dnl
63 dnl Last checked against gcc 6.1.0 (2016-09-25).  -D_FORTIFY_SOURCE=2 enables
64 dnl warn_unused_result attribute markings on glibc functions on Linux, which
65 dnl catches a few more issues.  Add -O2 because gcc won't find some warnings
66 dnl without optimization turned on.
67 dnl
68 dnl The warnings here are listed in the same order they're listed in the
69 dnl "Preprocessor Options" and "Warning Options" chapters of the GCC manual.
70 dnl
71 dnl Sets WARNINGS_CFLAGS as a substitution variable.
72 AC_DEFUN([RRA_PROG_CC_WARNINGS_FLAGS],
73 [AC_REQUIRE([RRA_PROG_CC_CLANG])
74  AS_IF([test x"$CLANG" = xyes],
75     [WARNINGS_CFLAGS="-Werror"
76      m4_foreach_w([flag],
77         [-Weverything -Wno-padded],
78         [RRA_PROG_CC_FLAG(flag,
79             [WARNINGS_CFLAGS="${WARNINGS_CFLAGS} flag"])])],
80     [WARNINGS_CFLAGS="-g -O2 -D_FORTIFY_SOURCE=2 -Werror"
81      m4_foreach_w([flag],
82         [-fstrict-overflow -fstrict-aliasing -Wcomments -Wendif-labels -Wall
83          -Wextra -Wformat=2 -Wformat-signedness -Wnull-dereference -Winit-self
84          -Wswitch-enum -Wuninitialized -Wstrict-overflow=5
85          -Wmissing-format-attribute -Wduplicated-cond -Wtrampolines
86          -Wfloat-equal -Wdeclaration-after-statement -Wshadow -Wpointer-arith
87          -Wbad-function-cast -Wcast-align -Wwrite-strings -Wdate-time
88          -Wjump-misses-init -Wfloat-conversion -Wlogical-op
89          -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes
90          -Wnormalized=nfc -Wpacked -Wredundant-decls -Wnested-externs -Winline
91          -Wvla],
92         [RRA_PROG_CC_FLAG(flag,
93             [WARNINGS_CFLAGS="${WARNINGS_CFLAGS} flag"])])])
94  AC_SUBST([WARNINGS_CFLAGS])])