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