1 # ===========================================================================
2 # http://www.gnu.org/software/autoconf-archive/ax_compiler_flags.html
3 # ===========================================================================
7 # AX_COMPILER_FLAGS([CFLAGS-VARIABLE], [LDFLAGS-VARIABLE], [IS-RELEASE], [EXTRA-BASE-CFLAGS], [EXTRA-MINIMUM-CFLAGS], [EXTRA-YES-CFLAGS], [EXTRA-MAXIMUM-CFLAGS], [EXTRA-ERROR-CFLAGS], [EXTRA-BASE-LDFLAGS], [EXTRA-MINIMUM-LDFLAGS], [EXTRA-YES-LDFLAGS], [EXTRA-MAXIMUM-LDFLAGS], [EXTRA-ERROR-LDFLAGS])
11 # Check for the presence of an --enable-compile-warnings option to
12 # configure, defaulting to "error" in normal operation, or "yes" if
13 # IS-RELEASE is equal to "yes". Return the value in the variable
14 # $ax_enable_compile_warnings.
16 # Depending on the value of --enable-compile-warnings, different compiler
17 # warnings are checked to see if they work with the current compiler and,
18 # if so, are appended to CFLAGS-VARIABLE and LDFLAGS-VARIABLE. This
19 # allows a consistent set of baseline compiler warnings to be used across
20 # a code base, irrespective of any warnings enabled locally by individual
21 # developers. By standardising the warnings used by all developers of a
22 # project, the project can commit to a zero-warnings policy, using -Werror
23 # to prevent compilation if new warnings are introduced. This makes
24 # catching bugs which are flagged by warnings a lot easier.
26 # By providing a consistent --enable-compile-warnings argument across all
27 # projects using this macro, continuous integration systems can easily be
28 # configured the same for all projects. Automated systems or build
29 # systems aimed at beginners may want to pass the --disable-Werror
30 # argument to unconditionally prevent warnings being fatal.
32 # --enable-compile-warnings can take the values:
34 # * no: Base compiler warnings only; not even -Wall.
35 # * minimum: The above, plus minimal extra warnings such as -Wall.
36 # * yes: The above, plus a broad range of useful warnings.
37 # * maximum: The above, plus additional warnings which enforce a particular
39 # * error: The above, plus -Werror so that all warnings are fatal.
40 # Use --disable-Werror to override this and disable fatal
43 # The set of flags enabled at each level can be augmented using the
44 # EXTRA-*-CFLAGS and EXTRA-*-LDFLAGS variables. Flags should not be
45 # disabled using these arguments, as the entire point of AX_COMPILER_FLAGS
46 # is to enforce a consistent set of useful compiler warnings on code,
47 # using warnings which have been chosen for low false positive rates. If
48 # a compiler emits false positives for a warning, a #pragma should be used
49 # in the code to disable the warning locally. See:
51 # https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/Diagnostic-Pragmas.html#Diagnostic-Pragmas
53 # IS-RELEASE can be used to disable -Werror when making a release, which
54 # is useful for those hairy moments when you just want to get the release
55 # done as quickly as possible. Set it to "yes" to disable -Werror.
57 # CFLAGS-VARIABLE defaults to WARN_CFLAGS, and LDFLAGS-VARIABLE defaults
58 # to WARN_LDFLAGS. Both variables are AC_SUBST-ed by this macro, but must
59 # be manually added to the CFLAGS and LDFLAGS variables for each target in
62 # If C++ language support is enabled with AC_PROG_CXX, which must occur
63 # before this macro in configure.ac, warning flags for the C++ compiler
64 # are AC_SUBST-ed as WARN_CXXFLAGS, and must be manually added to the
65 # CXXFLAGS variables for each target in the code base. EXTRA-*-CFLAGS can
66 # be used to augment the flags enabled at each level.
68 # Warning flags for g-ir-scanner (from GObject Introspection) are
69 # AC_SUBST-ed as WARN_SCANNERFLAGS. This variable must be manually added
70 # to the SCANNERFLAGS variable for each GIR target in the code base. If
71 # extra g-ir-scanner flags need to be enabled, the AX_COMPILER_FLAGS_GIR
72 # macro must be invoked manually.
74 # AX_COMPILER_FLAGS may add support for other tools in future, in addition
75 # to the compiler and linker. No extra EXTRA-* variables will be added
76 # for those tools, and all extra support will still use the single
77 # --enable-compile-warnings configure option. For finer grained control
78 # over the flags for individual tools, use AX_COMPILER_FLAGS_CFLAGS,
79 # AX_COMPILER_FLAGS_LDFLAGS and AX_COMPILER_FLAGS_* for new tools.
83 # Copyright (c) 2014, 2015 Philip Withnall <philip@tecnocode.co.uk>
84 # Copyright (c) 2015 David King <amigadave@amigadave.com>
86 # Copying and distribution of this file, with or without modification, are
87 # permitted in any medium without royalty provided the copyright notice
88 # and this notice are preserved. This file is offered as-is, without any
93 # _AX_COMPILER_FLAGS_LANG([LANGNAME])
94 m4_defun([_AX_COMPILER_FLAGS_LANG],
95 [m4_ifdef([_AX_COMPILER_FLAGS_LANG_]$1[_enabled], [],
96 [m4_define([_AX_COMPILER_FLAGS_LANG_]$1[_enabled], [])dnl
97 AX_REQUIRE_DEFINED([AX_COMPILER_FLAGS_]$1[FLAGS])])dnl
100 AC_DEFUN([AX_COMPILER_FLAGS],[
101 # C support is enabled by default.
102 _AX_COMPILER_FLAGS_LANG([C])
103 # Only enable C++ support if AC_PROG_CXX is called. The redefinition of
104 # AC_PROG_CXX is so that a fatal error is emitted if this macro is called
105 # before AC_PROG_CXX, which would otherwise cause no C++ warnings to be
107 AC_PROVIDE_IFELSE([AC_PROG_CXX],
108 [_AX_COMPILER_FLAGS_LANG([CXX])],
109 [m4_define([AC_PROG_CXX], defn([AC_PROG_CXX])[_AX_COMPILER_FLAGS_LANG([CXX])])])
110 AX_REQUIRE_DEFINED([AX_COMPILER_FLAGS_LDFLAGS])
112 AC_ARG_ENABLE([compile-warnings],
113 AS_HELP_STRING([--enable-compile-warnings=@<:@no/minimum/yes/maximum/error@:>@],
114 [Enable different levels of compiler warnings]),,
115 [AS_IF([test "$3" = "yes"],
116 [enable_compile_warnings="yes"],
117 [enable_compile_warnings="error"])])
118 AC_ARG_ENABLE([Werror],
119 AS_HELP_STRING([--disable-Werror],
120 [Unconditionally make all compiler warnings non-fatal]),,
121 [enable_Werror=maybe])
123 # Return the user’s chosen warning level
124 AS_IF([test "$enable_Werror" = "no" -a \
125 "$enable_compile_warnings" = "error"],[
126 enable_compile_warnings="maximum"
129 ax_enable_compile_warnings=$enable_compile_warnings
131 AX_COMPILER_FLAGS_CFLAGS([$1],[$3],[$4],[$5],[$6],[$7],[$8])
132 m4_ifdef([_AX_COMPILER_FLAGS_LANG_CXX_enabled],
133 [AX_COMPILER_FLAGS_CXXFLAGS([WARN_CXXFLAGS],[$3],[$4],[$5],[$6],[$7],[$8])])
134 AX_COMPILER_FLAGS_LDFLAGS([$2],[$3],[$9],[$10],[$11],[$12],[$13])
135 AX_COMPILER_FLAGS_GIR([WARN_SCANNERFLAGS],[$3])
136 ])dnl AX_COMPILER_FLAGS