1 # =============================================================================
2 # https://www.gnu.org/software/autoconf-archive/ax_compiler_flags_cflags.html
3 # =============================================================================
7 # AX_COMPILER_FLAGS_CFLAGS([VARIABLE], [IS-RELEASE], [EXTRA-BASE-FLAGS], [EXTRA-YES-FLAGS])
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.
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.
21 # Copyright (c) 2014, 2015 Philip Withnall <philip@tecnocode.co.uk>
22 # Copyright (c) 2017, 2018 Reini Urban <rurban@cpan.org>
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
31 AC_DEFUN([AX_COMPILER_FLAGS_CFLAGS],[
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])
38 m4_define([ax_warn_cflags_variable],
39 [m4_normalize(ifelse([$1],,[WARN_CFLAGS],[$1]))])
43 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
47 [ax_compiler_cxx=yes;],
48 [ax_compiler_cxx=no;])
50 # Always pass -Werror=unknown-warning-option to get Clang to fail on bad
51 # flags, otherwise they are always appended to the warn_cflags variable, and
52 # Clang warns on them for every compilation unit.
53 # If this is passed to GCC, it will explode, so the flag must be enabled
55 AX_CHECK_COMPILE_FLAG([-Werror=unknown-warning-option],[
56 ax_compiler_flags_test="-Werror=unknown-warning-option"
58 ax_compiler_flags_test=""
61 # Check that -Wno-suggest-attribute=format is supported
62 AX_CHECK_COMPILE_FLAG([-Wno-suggest-attribute=format],[
63 ax_compiler_no_suggest_attribute_flags="-Wno-suggest-attribute=format"
65 ax_compiler_no_suggest_attribute_flags=""
69 AX_APPEND_COMPILE_FLAGS([ dnl
70 -fno-strict-aliasing dnl
72 ],ax_warn_cflags_variable,[$ax_compiler_flags_test])
74 AS_IF([test "$ax_enable_compile_warnings" != "no"],[
75 if test "$ax_compiler_cxx" = "no" ; then
76 # C-only flags. Warn in C++
77 AX_APPEND_COMPILE_FLAGS([ dnl
79 -Wmissing-prototypes dnl
80 -Wstrict-prototypes dnl
81 -Wdeclaration-after-statement dnl
82 -Wimplicit-function-declaration dnl
83 -Wold-style-definition dnl
84 -Wjump-misses-init dnl
85 ],ax_warn_cflags_variable,[$ax_compiler_flags_test])
89 AX_APPEND_COMPILE_FLAGS([ dnl
95 -Wmissing-declarations dnl
97 -Wno-unused-parameter dnl
98 -Wno-missing-field-initializers dnl
101 -Wformat-nonliteral dnl
102 -Wformat-security dnl
104 -Wstrict-aliasing dnl
108 -Wmissing-format-attribute dnl
109 -Wmissing-noreturn dnl
111 -Wredundant-decls dnl
112 -Wmissing-include-dirs dnl
113 -Wunused-but-set-variable dnl
118 -Wduplicated-cond dnl
119 -Wduplicated-branches dnl
122 -Wnull-dereference dnl
123 -Wdouble-promotion dnl
128 ],ax_warn_cflags_variable,[$ax_compiler_flags_test])
130 AS_IF([test "$ax_enable_compile_warnings" = "error"],[
131 # "error" flags; -Werror has to be appended unconditionally because
132 # it's not possible to test for
134 # suggest-attribute=format is disabled because it gives too many false
136 AX_APPEND_FLAG([-Werror],ax_warn_cflags_variable)
138 AX_APPEND_COMPILE_FLAGS([ dnl
139 [$ax_compiler_no_suggest_attribute_flags] dnl
140 ],ax_warn_cflags_variable,[$ax_compiler_flags_test])
143 # In the flags below, when disabling specific flags, always add *both*
144 # -Wno-foo and -Wno-error=foo. This fixes the situation where (for example)
145 # we enable -Werror, disable a flag, and a build bot passes CFLAGS=-Wall,
146 # which effectively turns that flag back on again as an error.
147 for flag in $ax_warn_cflags_variable; do
151 AX_APPEND_COMPILE_FLAGS([-Wno-error=$(AS_ECHO([$flag]) | $SED 's/^-Wno-//')],
152 ax_warn_cflags_variable,
153 [$ax_compiler_flags_test])
159 # Substitute the variables
160 AC_SUBST(ax_warn_cflags_variable)
161 ])dnl AX_COMPILER_FLAGS