1# ============================================================================
2#  http://www.gnu.org/software/autoconf-archive/ax_compiler_flags_cflags.html
3# ============================================================================
4#
5# SYNOPSIS
6#
7#   AX_COMPILER_FLAGS_CFLAGS([VARIABLE], [IS-RELEASE], [EXTRA-BASE-FLAGS], [EXTRA-YES-FLAGS])
8#
9# DESCRIPTION
10#
11#   Add warning flags for the C compiler to VARIABLE, which defaults to
12#   WARN_CFLAGS.  VARIABLE is AC_SUBST-ed by this macro, but must be
13#   manually added to the CFLAGS variable for each target in the code base.
14#
15#   This macro depends on the environment set up by AX_COMPILER_FLAGS.
16#   Specifically, it uses the value of $ax_enable_compile_warnings to decide
17#   which flags to enable.
18#
19# LICENSE
20#
21#   Copyright (c) 2014, 2015 Philip Withnall <philip@tecnocode.co.uk>
22#
23#   Copying and distribution of this file, with or without modification, are
24#   permitted in any medium without royalty provided the copyright notice
25#   and this notice are preserved.  This file is offered as-is, without any
26#   warranty.
27
28#serial 11
29
30AC_DEFUN([AX_COMPILER_FLAGS_CFLAGS],[
31    AC_REQUIRE([AC_PROG_SED])
32    AX_REQUIRE_DEFINED([AX_APPEND_COMPILE_FLAGS])
33    AX_REQUIRE_DEFINED([AX_APPEND_FLAG])
34    AX_REQUIRE_DEFINED([AX_CHECK_COMPILE_FLAG])
35
36    # Variable names
37    m4_define(ax_warn_cflags_variable,
38              [m4_normalize(ifelse([$1],,[WARN_CFLAGS],[$1]))])
39
40    AC_LANG_PUSH([C])
41
42    # Always pass -Werror=unknown-warning-option to get Clang to fail on bad
43    # flags, otherwise they are always appended to the warn_cflags variable, and
44    # Clang warns on them for every compilation unit.
45    # If this is passed to GCC, it will explode, so the flag must be enabled
46    # conditionally.
47    AX_CHECK_COMPILE_FLAG([-Werror=unknown-warning-option],[
48        ax_compiler_flags_test="-Werror=unknown-warning-option"
49    ],[
50        ax_compiler_flags_test=""
51    ])
52
53    # Base flags
54    AX_APPEND_COMPILE_FLAGS([ dnl
55        -fno-strict-aliasing dnl
56        $3 dnl
57    ],ax_warn_cflags_variable,[$ax_compiler_flags_test])
58
59    AS_IF([test "$ax_enable_compile_warnings" != "no"],[
60        # "yes" flags
61        AX_APPEND_COMPILE_FLAGS([ dnl
62            -Wall dnl
63            -Wextra dnl
64            -Wundef dnl
65            -Wnested-externs dnl
66            -Wwrite-strings dnl
67            -Wpointer-arith dnl
68            -Wmissing-declarations dnl
69            -Wmissing-prototypes dnl
70            -Wstrict-prototypes dnl
71            -Wredundant-decls dnl
72            -Wno-unused-parameter dnl
73            -Wno-missing-field-initializers dnl
74            -Wdeclaration-after-statement dnl
75            -Wformat=2 dnl
76            -Wold-style-definition dnl
77            -Wcast-align dnl
78            -Wformat-nonliteral dnl
79            -Wformat-security dnl
80            -Wsign-compare dnl
81            -Wstrict-aliasing dnl
82            -Wshadow dnl
83            -Winline dnl
84            -Wpacked dnl
85            -Wmissing-format-attribute dnl
86            -Wmissing-noreturn dnl
87            -Winit-self dnl
88            -Wredundant-decls dnl
89            -Wmissing-include-dirs dnl
90            -Wunused-but-set-variable dnl
91            -Warray-bounds dnl
92            -Wimplicit-function-declaration dnl
93            -Wreturn-type dnl
94            -Wswitch-enum dnl
95            -Wswitch-default dnl
96            $4 dnl
97            $5 dnl
98            $6 dnl
99            $7 dnl
100        ],ax_warn_cflags_variable,[$ax_compiler_flags_test])
101    ])
102    AS_IF([test "$ax_enable_compile_warnings" = "error"],[
103        # "error" flags; -Werror has to be appended unconditionally because
104        # it's not possible to test for
105        #
106        # suggest-attribute=format is disabled because it gives too many false
107        # positives
108        AX_APPEND_FLAG([-Werror],ax_warn_cflags_variable)
109
110        AX_APPEND_COMPILE_FLAGS([ dnl
111            -Wno-suggest-attribute=format dnl
112        ],ax_warn_cflags_variable,[$ax_compiler_flags_test])
113    ])
114
115    # In the flags below, when disabling specific flags, always add *both*
116    # -Wno-foo and -Wno-error=foo. This fixes the situation where (for example)
117    # we enable -Werror, disable a flag, and a build bot passes CFLAGS=-Wall,
118    # which effectively turns that flag back on again as an error.
119    for flag in $ax_warn_cflags_variable; do
120        AS_CASE([$flag],
121                [-Wno-*=*],[],
122                [-Wno-*],[
123                    AX_APPEND_COMPILE_FLAGS([-Wno-error=$(AS_ECHO([$flag]) | $SED 's/^-Wno-//')],
124                                            ax_warn_cflags_variable,
125                                            [$ax_compiler_flags_test])
126                ])
127    done
128
129    AC_LANG_POP([C])
130
131    # Substitute the variables
132    AC_SUBST(ax_warn_cflags_variable)
133])dnl AX_COMPILER_FLAGS
134