configure: allow adding compiler-specific warning flags
[babeltrace.git] / m4 / ax_compiler_flags_cflags.m4
CommitLineData
02bb4fcc
SM
1# =============================================================================
2# https://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# Copyright (c) 2017, 2018 Reini Urban <rurban@cpan.org>
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 17
30
31AC_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])
36
37 # Variable names
38 m4_define([ax_warn_cflags_variable],
39 [m4_normalize(ifelse([$1],,[WARN_CFLAGS],[$1]))])
40
41 AC_LANG_PUSH([C])
42
43 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
44 [#ifndef __cplusplus
45 #error "no C++"
46 #endif]])],
47 [ax_compiler_cxx=yes;],
48 [ax_compiler_cxx=no;])
49
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
54 # conditionally.
55 AX_CHECK_COMPILE_FLAG([-Werror=unknown-warning-option],[
56 ax_compiler_flags_test="-Werror=unknown-warning-option"
57 ],[
58 ax_compiler_flags_test=""
59 ])
60
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"
64 ],[
65 ax_compiler_no_suggest_attribute_flags=""
66 ])
67
68 # Base flags
69 AX_APPEND_COMPILE_FLAGS([ dnl
70 -fno-strict-aliasing dnl
71 $3 dnl
72 ],ax_warn_cflags_variable,[$ax_compiler_flags_test])
73
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
78 -Wnested-externs 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])
86 fi
87
88 # "yes" flags
89 AX_APPEND_COMPILE_FLAGS([ dnl
90 -Wall dnl
91 -Wextra dnl
92 -Wundef dnl
93 -Wwrite-strings dnl
94 -Wpointer-arith dnl
95 -Wmissing-declarations dnl
96 -Wredundant-decls dnl
97 -Wno-unused-parameter dnl
98 -Wno-missing-field-initializers dnl
99 -Wformat=2 dnl
100 -Wcast-align dnl
101 -Wformat-nonliteral dnl
102 -Wformat-security dnl
103 -Wsign-compare dnl
104 -Wstrict-aliasing dnl
105 -Wshadow dnl
106 -Winline dnl
107 -Wpacked dnl
108 -Wmissing-format-attribute dnl
109 -Wmissing-noreturn dnl
110 -Winit-self dnl
111 -Wredundant-decls dnl
112 -Wmissing-include-dirs dnl
113 -Wunused-but-set-variable dnl
114 -Warray-bounds dnl
115 -Wreturn-type dnl
116 -Wswitch-enum dnl
117 -Wswitch-default dnl
118 -Wduplicated-cond dnl
119 -Wduplicated-branches dnl
120 -Wlogical-op dnl
121 -Wrestrict dnl
122 -Wnull-dereference dnl
123 -Wdouble-promotion dnl
124 $4 dnl
125 $5 dnl
126 $6 dnl
127 $7 dnl
128 ],ax_warn_cflags_variable,[$ax_compiler_flags_test])
129 ])
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
133 #
134 # suggest-attribute=format is disabled because it gives too many false
135 # positives
136 AX_APPEND_FLAG([-Werror],ax_warn_cflags_variable)
137
138 AX_APPEND_COMPILE_FLAGS([ dnl
139 [$ax_compiler_no_suggest_attribute_flags] dnl
140 ],ax_warn_cflags_variable,[$ax_compiler_flags_test])
141 ])
142
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
148 AS_CASE([$flag],
149 [-Wno-*=*],[],
150 [-Wno-*],[
151 AX_APPEND_COMPILE_FLAGS([-Wno-error=$(AS_ECHO([$flag]) | $SED 's/^-Wno-//')],
152 ax_warn_cflags_variable,
153 [$ax_compiler_flags_test])
154 ])
155 done
156
157 AC_LANG_POP([C])
158
159 # Substitute the variables
160 AC_SUBST(ax_warn_cflags_variable)
161])dnl AX_COMPILER_FLAGS
This page took 0.027594 seconds and 4 git commands to generate.