3 dnl Copyright (C) 2008-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 dnl From Simon Josefsson
12 # gl_COMPILER_OPTION_IF(OPTION, [IF-SUPPORTED], [IF-NOT-SUPPORTED],
13 # [PROGRAM = AC_LANG_PROGRAM()])
14 # -----------------------------------------------------------------
15 # Check if the compiler supports OPTION when compiling PROGRAM.
17 # The effects of this macro depend on the current language (_AC_LANG).
18 AC_DEFUN([gl_COMPILER_OPTION_IF],
20 AS_VAR_PUSHDEF([gl_Warn], [gl_cv_warn_[]_AC_LANG_ABBREV[]_$1])dnl
21 AS_VAR_PUSHDEF([gl_Flags], [_AC_LANG_PREFIX[]FLAGS])dnl
23 [m4_pushdef([gl_Positive], m4_bpatsubst([$1], [^-Wno-], [-W]))],
26 -Wno-*) gl_positive=-W`expr "X$gl_positive" : 'X-Wno-\(.*\)'` ;;
28 m4_pushdef([gl_Positive], [$gl_positive])])dnl
29 AC_CACHE_CHECK([whether _AC_LANG compiler handles $1], [gl_Warn], [
30 gl_saved_compiler_FLAGS="$gl_Flags"
31 AS_VAR_APPEND(m4_defn([gl_Flags]),
32 [" $gl_unknown_warnings_are_errors ]m4_defn([gl_Positive])["])
33 AC_LINK_IFELSE([m4_default([$4], [AC_LANG_PROGRAM([[]])])],
34 [AS_VAR_SET([gl_Warn], [yes])],
35 [AS_VAR_SET([gl_Warn], [no])])
36 gl_Flags="$gl_saved_compiler_FLAGS"
38 AS_VAR_IF(gl_Warn, [yes], [$2], [$3])
39 m4_popdef([gl_Positive])dnl
40 AS_VAR_POPDEF([gl_Flags])dnl
41 AS_VAR_POPDEF([gl_Warn])dnl
44 # gl_UNKNOWN_WARNINGS_ARE_ERRORS
45 # ------------------------------
46 # Clang doesn't complain about unknown warning options unless one also
47 # specifies -Wunknown-warning-option -Werror. Detect this.
49 # The effects of this macro depend on the current language (_AC_LANG).
50 AC_DEFUN([gl_UNKNOWN_WARNINGS_ARE_ERRORS],
51 [_AC_LANG_DISPATCH([$0], _AC_LANG, $@)])
53 # Specialization for _AC_LANG = C. This macro can be AC_REQUIREd.
54 AC_DEFUN([gl_UNKNOWN_WARNINGS_ARE_ERRORS(C)],
57 gl_UNKNOWN_WARNINGS_ARE_ERRORS_IMPL
61 # Specialization for _AC_LANG = C++. This macro can be AC_REQUIREd.
62 AC_DEFUN([gl_UNKNOWN_WARNINGS_ARE_ERRORS(C++)],
65 gl_UNKNOWN_WARNINGS_ARE_ERRORS_IMPL
69 # Specialization for _AC_LANG = Objective C. This macro can be AC_REQUIREd.
70 AC_DEFUN([gl_UNKNOWN_WARNINGS_ARE_ERRORS(Objective C)],
72 AC_LANG_PUSH([Objective C])
73 gl_UNKNOWN_WARNINGS_ARE_ERRORS_IMPL
74 AC_LANG_POP([Objective C])
77 AC_DEFUN([gl_UNKNOWN_WARNINGS_ARE_ERRORS_IMPL],
78 [gl_COMPILER_OPTION_IF([-Werror -Wunknown-warning-option],
79 [gl_unknown_warnings_are_errors='-Wunknown-warning-option -Werror'],
80 [gl_unknown_warnings_are_errors=])])
82 # gl_WARN_ADD(OPTION, [VARIABLE = WARN_CFLAGS/WARN_CXXFLAGS],
83 # [PROGRAM = AC_LANG_PROGRAM()])
84 # -----------------------------------------------------------
85 # Adds OPTION to VARIABLE (which defaults to WARN_CFLAGS or WARN_CXXFLAGS)
86 # if the compiler supports it when compiling PROGRAM.
88 # If VARIABLE is a variable name, AC_SUBST it.
90 # The effects of this macro depend on the current language (_AC_LANG).
92 # Example: gl_WARN_ADD([-Wparentheses]).
93 AC_DEFUN([gl_WARN_ADD],
94 [AC_REQUIRE([gl_UNKNOWN_WARNINGS_ARE_ERRORS(]_AC_LANG[)])
95 gl_COMPILER_OPTION_IF([$1],
96 [AS_VAR_APPEND(m4_if([$2], [], [[WARN_]_AC_LANG_PREFIX[FLAGS]], [[$2]]), [" $1"])],
100 [AS_LITERAL_IF([$2], [AC_SUBST([$2])])],
101 [AC_SUBST([WARN_]_AC_LANG_PREFIX[FLAGS])])dnl
105 # gl_CC_INHIBIT_WARNINGS
106 # sets and substitutes a variable GL_CFLAG_INHIBIT_WARNINGS, to a $(CC) option
107 # that reverts all preceding -W* options, if available.
108 # This is expected to be '-w' at least on gcc, clang, AIX xlc, xlclang, Sun cc,
109 # "compile cl" (MSVC), "compile clang-cl" (MSVC-compatible clang). Or it can be
111 AC_DEFUN([gl_CC_INHIBIT_WARNINGS],
113 AC_REQUIRE([AC_PROG_CC])
114 AC_CACHE_CHECK([for C compiler option to inhibit all warnings],
117 echo 'int dummy;' > conftest.c
118 AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS -c conftest.c 2>conftest1.err]) >/dev/null
119 AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS -w -c conftest.c 2>conftest2.err]) >/dev/null
120 if test $? = 0 && test `wc -l < conftest1.err` = `wc -l < conftest2.err`; then
121 gl_cv_cc_winhibit='-w'
123 gl_cv_cc_winhibit=none
127 case "$gl_cv_cc_winhibit" in
128 none) GL_CFLAG_INHIBIT_WARNINGS='' ;;
130 GL_CFLAG_INHIBIT_WARNINGS="$gl_cv_cc_winhibit"
131 dnl If all warnings are inhibited, there's no point in having the GCC
132 dnl analyzer enabled. This saves RAM requirements and CPU consumption.
133 gl_WARN_ADD([-fno-analyzer], [GL_CFLAG_INHIBIT_WARNINGS])
136 AC_SUBST([GL_CFLAG_INHIBIT_WARNINGS])
139 # gl_CXX_INHIBIT_WARNINGS
140 # sets and substitutes a variable GL_CXXFLAG_INHIBIT_WARNINGS, to a $(CC) option
141 # that reverts all preceding -W* options, if available.
142 AC_DEFUN([gl_CXX_INHIBIT_WARNINGS],
144 dnl Requires AC_PROG_CXX or gl_PROG_ANSI_CXX.
145 if test -n "$CXX" && test "$CXX" != no; then
146 AC_CACHE_CHECK([for C++ compiler option to inhibit all warnings],
147 [gl_cv_cxx_winhibit],
149 echo 'int dummy;' > conftest.cc
150 AC_TRY_COMMAND([${CXX-c++} $CXXFLAGS $CPPFLAGS -c conftest.cc 2>conftest1.err]) >/dev/null
151 AC_TRY_COMMAND([${CXX-c++} $CXXFLAGS $CPPFLAGS -w -c conftest.cc 2>conftest2.err]) >/dev/null
152 if test $? = 0 && test `wc -l < conftest1.err` = `wc -l < conftest2.err`; then
153 gl_cv_cxx_winhibit='-w'
155 gl_cv_cxx_winhibit=none
159 case "$gl_cv_cxx_winhibit" in
160 none) GL_CXXFLAG_INHIBIT_WARNINGS='' ;;
162 GL_CXXFLAG_INHIBIT_WARNINGS="$gl_cv_cxx_winhibit"
163 dnl If all warnings are inhibited, there's no point in having the GCC
164 dnl analyzer enabled. This saves RAM requirements and CPU consumption.
165 gl_WARN_ADD([-fno-analyzer], [GL_CXXFLAG_INHIBIT_WARNINGS])
169 GL_CXXFLAG_INHIBIT_WARNINGS=''
171 AC_SUBST([GL_CXXFLAG_INHIBIT_WARNINGS])