configure: add macro to check compiler flag '-Wstrict-prototype'
[wmaker-crm.git] / m4 / wm_cflags_check.m4
blobb61b16d4aeadd7b74ade15a4ec2cb87519c649dc
1 # wm_cflags_check.m4 - Macros to check options for the compiler into CFLAGS
3 # Copyright (c) 2013 Christophe CURIS
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 # WM_CFLAGS_CHECK_FIRST
21 # ---------------------
23 # For each option provided, check which one is supported and stops when
24 # found, adding it to CFLAGS. It extends AX_CFLAGS_GCC_OPTION which does
25 # not checks for fallbacks; as a bonus it uses a shared function to
26 # produce smaller configure script.
28 # Usage: WM_CFLAGS_CHECK_FIRST([message], [option alt_option...])
29 #   $1 message: required, message displayed in 'Checking for...'
30 #   $2 option_list: list of options, tested in given order
32 # The first option that works is added to CFLAGS
33 AC_DEFUN([WM_CFLAGS_CHECK_FIRST],
34 [AC_REQUIRE([_WM_SHELLFN_CHKCFLAGS])
35 m4_define([_wm_optlist], m4_split([$2]))dnl
36 AS_VAR_PUSHDEF([VAR], [wm_cv_c_check_compopt[]m4_car(_wm_optlist)])dnl
37 m4_define([_wm_trimmed_optlist], m4_join([ ], _wm_optlist))dnl
38 AC_CACHE_CHECK([CFLAGS for m4_ifnblank($1,$1,m4_car(_wm_optlist))], VAR,
39   [VAR="no, unknown"
40    for wm_option in _wm_trimmed_optlist ; do
41      AS_IF([wm_fn_c_try_compile_cflag "$wm_option"],
42            [VAR="$wm_option" ; break])
43    done])
44 AS_CASE([$VAR],
45   [no,*], [],
46   [AS_IF([echo " $CFLAGS " | grep " $VAR " 2>&1 > /dev/null],
47     [AC_MSG_WARN([option $VAR already present in \$CFLAGS, not appended])],
48     [CFLAGS="$CFLAGS $VAR"]) ])
49 AS_VAR_POPDEF([VAR])dnl
50 m4_undefine([_wm_optlist])dnl
51 m4_undefine([_wm_trimmed_optlist])dnl
55 # _WM_SHELLFN_CHKCFLAGS
56 # ---------------------
57 # (internal shell function)
59 # Create a shell function that tries compiling a simple program with the
60 # specified compiler option. Assumes the current compilation language is
61 # already set to C
62 AC_DEFUN_ONCE([_WM_SHELLFN_CHKCFLAGS],
63 [@%:@ wm_fn_c_try_compile_cflag CC_OPTION
64 @%:@ -----------------------------------
65 @%:@ Try compiling a function using CC_OPTION in the compiler's options
66 wm_fn_c_try_compile_cflag ()
68   wm_save_CFLAGS="$CFLAGS"
69   CFLAGS="$CFLAGS -Werror $[]1"
70   AC_COMPILE_IFELSE(
71     [AC_LANG_PROGRAM([], [])],
72     [wm_retval=0],
73     [wm_retval=1])
74   CFLAGS="$wm_save_CFLAGS"
75   AS_SET_STATUS([$wm_retval])
80 # WM_CFLAGS_GCC_OPTION_UNUSEDMACROS
81 # ---------------------------------
83 # Check if the compiler support '-Wunused-macros'. This would be done
84 # traditionally using AX_CFLAGS_GCC_OPTION(...), but in the present case it
85 # always fail because the test program defines many macro (all the stuff
86 # detected so far) so the warning always trigger, the compilation then fail,
87 # and the option is then marked as never supported.
89 # This macro works around this by using a basic program without any #define
90 AC_DEFUN([WM_CFLAGS_GCC_OPTION_UNUSEDMACROS],
91 [AC_CACHE_CHECK([CFLAGS for gcc -Wunused-macros], [wm_cv_c_checks_compopt_Wunused_macros],
92   [AC_LANG_COMPILER_REQUIRE()dnl
93    AC_LANG_PUSH([C])
94    wm_save_CFLAGS="$CFLAGS"
95    CFLAGS="$wm_save_CFLAGS -pedantic -Werror -Wunused-macros"
96    dnl Similar to AC_LANG_CONFTEST(C), except we do not include the 'confdefs.h'
97    dnl directly in the source to avoid an "unused macro" error, but we include
98    dnl it in the same way we will include the "config.h", so we get in the same
99    dnl condition as WindowMaker's source
100    cat <<_ACEOF >conftest.$ac_ext
101 @%:@include "confdefs.h"
104 main(int argc, char **argv)
106         /* to avoid failing on -Wunused-parameter */
107         (void) argc;
108         (void) argv;
110         return 0;
112 _ACEOF
113    AS_IF([ac_fn_[]_AC_LANG_ABBREV[]_try_compile "$LINENO"],
114      [wm_cv_c_checks_compopt_Wunused_macros="-Wunused-macros"],
115      [wm_cv_c_checks_compopt_Wunused_macros="no"])
116    rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
117    CFLAGS="$wm_save_CFLAGS"
118    AC_LANG_POP([C]) ])
119 # We cannot add this flag now to CFLAGS because it could break all further
120 # detections done by configure, the flag will be added at the end by the
121 # macro WM_CFLAGS_GCC_OPTION_POSTPONED
125 # WM_CFLAGS_GCC_OPTION_STRICTPROTO
126 # --------------------------------
128 # Check if the compiler support '-Wstrict-prototypes'. This would be done
129 # traditionally using AX_CFLAGS_GCC_OPTION(...), but in the present case it
130 # always fail because the test program generated by autoconf always use a
131 # non-compliant prototype (this is needed for portability to avoid possible
132 # function declaration mismatch on 'main').
134 # This macro works around this by providing a strict prototype for 'main'
135 # in this case because we are in a very known case.
136 AC_DEFUN([WM_CFLAGS_GCC_OPTION_STRICTPROTO],
137 [AC_CACHE_CHECK([CFLAGS for gcc -Wstrict-prototypes], [wm_cv_c_checks_compopt_Wstrict_prototypes],
138   [AC_LANG_COMPILER_REQUIRE()dnl
139    AC_LANG_PUSH([C])
140    wm_save_CFLAGS="$CFLAGS"
141    CFLAGS="$wm_save_CFLAGS -pedantic -Werror -Wstrict-prototypes"
142    AC_COMPILE_IFELSE(
143      [AC_LANG_SOURCE([dnl
145 main(int argc, char **argv)
147         /* to avoid failing on -Wunused-parameter */
148         (void) argc;
149         (void) argv;
151         return 0;
152 }])],
153      [wm_cv_c_checks_compopt_Wstrict_prototypes="-Wstrict-prototypes"],
154      [wm_cv_c_checks_compopt_Wstrict_prototypes="no"])
155    CFLAGS="$wm_save_CFLAGS"
156    AC_LANG_POP([C]) ])
157 # We cannot add this flag now to CFLAGS because it could break all further
158 # detections done by configure, the flag will be added at the end by the
159 # macro WM_CFLAGS_GCC_OPTION_POSTPONED
163 # WM_CFLAGS_GCC_OPTION_POSTPONED
164 # ------------------------------
166 # Some options cannot be added to the compilation command as soon as they have
167 # been detected because they can break the detection done in autoconf, because
168 # the template are cannot always be perfect.
169 # This macro takes care of making them effective, so it should be called just
170 # before AC_OUTPUT.
171 AC_DEFUN_ONCE([WM_CFLAGS_GCC_OPTION_POSTPONED],
172 [# WM_CFLAGS_GCC_OPTION_POSTPONED: add compiler flags at the end of the autoconf flow
173 AS_IF([test "x$debug" = "xyes"],
174   [m4_foreach([arg_name], [[Wstrict_prototypes], [Wunused_macros]],
175     [AS_CASE([$wm_cv_c_checks_compopt_[]arg_name],
176       [no*], [],
177       [AS_IF([echo " $CFLAGS " | grep " $wm_cv_c_checks_compopt_[]arg_name " 2>&1 > /dev/null],
178         [AC_MSG_WARN([option $wm_cv_c_checks_compopt_[]arg_name already present in \$CFLAGS, not appended])],
179         [CFLAGS="$CFLAGS $wm_cv_c_checks_compopt_[]arg_name"])dnl
180       ])
181     ])dnl
182   ])dnl