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