1dnl Copyright 2010 Google Inc. 2dnl All rights reserved. 3dnl 4dnl Redistribution and use in source and binary forms, with or without 5dnl modification, are permitted provided that the following conditions are 6dnl met: 7dnl 8dnl * Redistributions of source code must retain the above copyright 9dnl notice, this list of conditions and the following disclaimer. 10dnl * Redistributions in binary form must reproduce the above copyright 11dnl notice, this list of conditions and the following disclaimer in the 12dnl documentation and/or other materials provided with the distribution. 13dnl * Neither the name of Google Inc. nor the names of its contributors 14dnl may be used to endorse or promote products derived from this software 15dnl without specific prior written permission. 16dnl 17dnl THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18dnl "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19dnl LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20dnl A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21dnl OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22dnl SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23dnl LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24dnl DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25dnl THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26dnl (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27dnl OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29dnl \file compiler-flags.m4 30dnl 31dnl Macros to check for the existence of compiler flags. The macros in this 32dnl file support both C and C++. 33dnl 34dnl Be aware that, in order to detect a flag accurately, we may need to enable 35dnl strict warning checking in the compiler (i.e. enable -Werror). Some 36dnl compilers, e.g. Clang, report unknown -W flags as warnings unless -Werror is 37dnl selected. This fact would confuse the flag checks below because we would 38dnl conclude that a flag is valid while in reality it is not. To resolve this, 39dnl the macros below will pass -Werror to the compiler along with any other flag 40dnl being checked. 41 42 43dnl Checks for a compiler flag and sets a result variable. 44dnl 45dnl This is an auxiliary macro for the implementation of _KYUA_FLAG. 46dnl 47dnl \param 1 The shell variable containing the compiler name. Used for 48dnl reporting purposes only. C or CXX. 49dnl \param 2 The shell variable containing the flags for the compiler. 50dnl CFLAGS or CXXFLAGS. 51dnl \param 3 The name of the compiler flag to check for. 52dnl \param 4 The shell variable to set with the result of the test. Will 53dnl be set to 'yes' if the flag is valid, 'no' otherwise. 54dnl \param 5 Additional, optional flags to pass to the C compiler while 55dnl looking for the flag in $3. We use this here to pass -Werror to the 56dnl flag checks (unless we are checking for -Werror already). 57AC_DEFUN([_KYUA_FLAG_AUX], [ 58 if test x"${$4-unset}" = xunset; then 59 AC_MSG_CHECKING(whether ${$1} supports $3) 60 saved_flags="${$2}" 61 $4=no 62 $2="${$2} $5 $3" 63 AC_LINK_IFELSE([AC_LANG_PROGRAM([], [return 0;])], 64 AC_MSG_RESULT(yes) 65 $4=yes, 66 AC_MSG_RESULT(no)) 67 $2="${saved_flags}" 68 fi 69]) 70 71 72dnl Checks for a compiler flag and appends it to a result variable. 73dnl 74dnl \param 1 The shell variable containing the compiler name. Used for 75dnl reporting purposes only. CC or CXX. 76dnl \param 2 The shell variable containing the flags for the compiler. 77dnl CFLAGS or CXXFLAGS. 78dnl \param 3 The name of the compiler flag to check for. 79dnl \param 4 The shell variable to which to append $3 if the flag is valid. 80AC_DEFUN([_KYUA_FLAG], [ 81 _KYUA_FLAG_AUX([$1], [$2], [-Werror], [kyua_$1_has_werror]) 82 if test "$3" = "-Werror"; then 83 found=${kyua_$1_has_werror} 84 else 85 found=unset 86 if test ${kyua_$1_has_werror} = yes; then 87 _KYUA_FLAG_AUX([$1], [$2], [$3], [found], [-Werror]) 88 else 89 _KYUA_FLAG_AUX([$1], [$2], [$3], [found], []) 90 fi 91 fi 92 if test ${found} = yes; then 93 $4="${$4} $3" 94 fi 95]) 96 97 98dnl Checks for a C compiler flag and appends it to a variable. 99dnl 100dnl \pre The current language is C. 101dnl 102dnl \param 1 The name of the compiler flag to check for. 103dnl \param 2 The shell variable to which to append $1 if the flag is valid. 104AC_DEFUN([KYUA_CC_FLAG], [ 105 AC_LANG_ASSERT([C]) 106 _KYUA_FLAG([CC], [CFLAGS], [$1], [$2]) 107]) 108 109 110dnl Checks for a C++ compiler flag and appends it to a variable. 111dnl 112dnl \pre The current language is C++. 113dnl 114dnl \param 1 The name of the compiler flag to check for. 115dnl \param 2 The shell variable to which to append $1 if the flag is valid. 116AC_DEFUN([KYUA_CXX_FLAG], [ 117 AC_LANG_ASSERT([C++]) 118 _KYUA_FLAG([CXX], [CXXFLAGS], [$1], [$2]) 119]) 120 121 122dnl Checks for a set of C compiler flags and appends them to CFLAGS. 123dnl 124dnl The checks are performed independently and only when all the checks are 125dnl done, the output variable is modified. 126dnl 127dnl \param 1 Whitespace-separated list of C flags to check. 128AC_DEFUN([KYUA_CC_FLAGS], [ 129 AC_LANG_PUSH([C]) 130 valid_cflags= 131 for f in $1; do 132 KYUA_CC_FLAG(${f}, valid_cflags) 133 done 134 if test -n "${valid_cflags}"; then 135 CFLAGS="${CFLAGS} ${valid_cflags}" 136 fi 137 AC_LANG_POP([C]) 138]) 139 140 141dnl Checks for a set of C++ compiler flags and appends them to CXXFLAGS. 142dnl 143dnl The checks are performed independently and only when all the checks are 144dnl done, the output variable is modified. 145dnl 146dnl \pre The current language is C++. 147dnl 148dnl \param 1 Whitespace-separated list of C flags to check. 149AC_DEFUN([KYUA_CXX_FLAGS], [ 150 AC_LANG_PUSH([C++]) 151 valid_cxxflags= 152 for f in $1; do 153 KYUA_CXX_FLAG(${f}, valid_cxxflags) 154 done 155 if test -n "${valid_cxxflags}"; then 156 CXXFLAGS="${CXXFLAGS} ${valid_cxxflags}" 157 fi 158 AC_LANG_POP([C++]) 159]) 160