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