packaging: Bump to 3.12.0
[platform/upstream/gnome-common.git] / macros2 / gnome-compiler-flags.m4
1 # gnome-compiler-flags.m4
2 #
3 # serial 2
4 #
5
6 dnl GNOME_COMPILE_WARNINGS
7 dnl Turn on many useful compiler warnings and substitute the result into
8 dnl WARN_CFLAGS
9 dnl For now, only works on GCC
10 dnl Pass the default value of the --enable-compile-warnings configure option as
11 dnl the first argument to the macro, defaulting to 'yes'.
12 dnl Additional warning/error flags can be passed as an optional second argument.
13 dnl
14 dnl For example: GNOME_COMPILE_WARNINGS([maximum],[-Werror=some-flag -Wfoobar])
15 AC_DEFUN([GNOME_COMPILE_WARNINGS],[
16     dnl ******************************
17     dnl More compiler warnings
18     dnl ******************************
19
20     AC_ARG_ENABLE(compile-warnings, 
21                   AS_HELP_STRING([--enable-compile-warnings=@<:@no/minimum/yes/maximum/error@:>@],
22                                  [Turn on compiler warnings]),,
23                   [enable_compile_warnings="m4_default([$1],[yes])"])
24
25     if test "x$GCC" != xyes; then
26         enable_compile_warnings=no
27     fi
28
29     warning_flags=
30     realsave_CFLAGS="$CFLAGS"
31
32     dnl These are warning flags that aren't marked as fatal.  Can be
33     dnl overridden on a per-project basis with -Wno-foo.
34     base_warn_flags=" \
35         -Wall \
36         -Wstrict-prototypes \
37         -Wnested-externs \
38     "
39
40     dnl These compiler flags typically indicate very broken or suspicious
41     dnl code.  Some of them such as implicit-function-declaration are
42     dnl just not default because gcc compiles a lot of legacy code.
43     dnl We choose to make this set into explicit errors.
44     base_error_flags=" \
45         -Werror=missing-prototypes \
46         -Werror=implicit-function-declaration \
47         -Werror=pointer-arith \
48         -Werror=init-self \
49         -Werror=format-security \
50         -Werror=format=2 \
51         -Werror=missing-include-dirs \
52     "
53
54     dnl Additional warning or error flags provided by the module author to
55     dnl allow stricter standards to be imposed on a per-module basis.
56     dnl The author can pass -W or -Werror flags here as they see fit.
57     additional_flags="m4_default([$2],[])"
58
59     case "$enable_compile_warnings" in
60     no)
61         warning_flags=
62         ;;
63     minimum)
64         warning_flags="-Wall"
65         ;;
66     yes|maximum|error)
67         warning_flags="$base_warn_flags $base_error_flags $additional_flags"
68         ;;
69     *)
70         AC_MSG_ERROR(Unknown argument '$enable_compile_warnings' to --enable-compile-warnings)
71         ;;
72     esac
73
74     if test "$enable_compile_warnings" = "error" ; then
75         warning_flags="$warning_flags -Werror"
76     fi
77
78     dnl Check whether GCC supports the warning options
79     for option in $warning_flags; do
80         save_CFLAGS="$CFLAGS"
81         CFLAGS="$CFLAGS $option"
82         AC_MSG_CHECKING([whether gcc understands $option])
83         AC_TRY_COMPILE([], [],
84             has_option=yes,
85             has_option=no,)
86         CFLAGS="$save_CFLAGS"
87         AC_MSG_RESULT([$has_option])
88         if test $has_option = yes; then
89             tested_warning_flags="$tested_warning_flags $option"
90         fi
91         unset has_option
92         unset save_CFLAGS
93     done
94     unset option
95     CFLAGS="$realsave_CFLAGS"
96     AC_MSG_CHECKING(what warning flags to pass to the C compiler)
97     AC_MSG_RESULT($tested_warning_flags)
98
99     AC_ARG_ENABLE(iso-c,
100                   AS_HELP_STRING([--enable-iso-c],
101                                  [Try to warn if code is not ISO C ]),,
102                   [enable_iso_c=no])
103
104     AC_MSG_CHECKING(what language compliance flags to pass to the C compiler)
105     complCFLAGS=
106     if test "x$enable_iso_c" != "xno"; then
107         if test "x$GCC" = "xyes"; then
108         case " $CFLAGS " in
109             *[\ \       ]-ansi[\ \      ]*) ;;
110             *) complCFLAGS="$complCFLAGS -ansi" ;;
111         esac
112         case " $CFLAGS " in
113             *[\ \       ]-pedantic[\ \  ]*) ;;
114             *) complCFLAGS="$complCFLAGS -pedantic" ;;
115         esac
116         fi
117     fi
118     AC_MSG_RESULT($complCFLAGS)
119
120     WARN_CFLAGS="$tested_warning_flags $complCFLAGS"
121     AC_SUBST(WARN_CFLAGS)
122 ])
123
124 dnl For C++, do basically the same thing.
125
126 AC_DEFUN([GNOME_CXX_WARNINGS],[
127   AC_ARG_ENABLE(cxx-warnings,
128                 AS_HELP_STRING([--enable-cxx-warnings=@<:@no/minimum/yes@:>@]
129                                [Turn on compiler warnings.]),,
130                 [enable_cxx_warnings="m4_default([$1],[minimum])"])
131
132   AC_MSG_CHECKING(what warning flags to pass to the C++ compiler)
133   warnCXXFLAGS=
134   if test "x$GXX" != xyes; then
135     enable_cxx_warnings=no
136   fi
137   if test "x$enable_cxx_warnings" != "xno"; then
138     if test "x$GXX" = "xyes"; then
139       case " $CXXFLAGS " in
140       *[\ \     ]-Wall[\ \      ]*) ;;
141       *) warnCXXFLAGS="-Wall -Wno-unused" ;;
142       esac
143
144       ## -W is not all that useful.  And it cannot be controlled
145       ## with individual -Wno-xxx flags, unlike -Wall
146       if test "x$enable_cxx_warnings" = "xyes"; then
147         warnCXXFLAGS="$warnCXXFLAGS -Wshadow -Woverloaded-virtual"
148       fi
149     fi
150   fi
151   AC_MSG_RESULT($warnCXXFLAGS)
152
153    AC_ARG_ENABLE(iso-cxx,
154                  AS_HELP_STRING([--enable-iso-cxx],
155                                 [Try to warn if code is not ISO C++ ]),,
156                  [enable_iso_cxx=no])
157
158    AC_MSG_CHECKING(what language compliance flags to pass to the C++ compiler)
159    complCXXFLAGS=
160    if test "x$enable_iso_cxx" != "xno"; then
161      if test "x$GXX" = "xyes"; then
162       case " $CXXFLAGS " in
163       *[\ \     ]-ansi[\ \      ]*) ;;
164       *) complCXXFLAGS="$complCXXFLAGS -ansi" ;;
165       esac
166
167       case " $CXXFLAGS " in
168       *[\ \     ]-pedantic[\ \  ]*) ;;
169       *) complCXXFLAGS="$complCXXFLAGS -pedantic" ;;
170       esac
171      fi
172    fi
173   AC_MSG_RESULT($complCXXFLAGS)
174
175   WARN_CXXFLAGS="$CXXFLAGS $warnCXXFLAGS $complCXXFLAGS"
176   AC_SUBST(WARN_CXXFLAGS)
177 ])