/[gentoo-x86]/eclass/flag-o-matic.eclass
Gentoo

Diff of /eclass/flag-o-matic.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.71 Revision 1.189
1# Copyright 1999-2004 Gentoo Foundation 1# Copyright 1999-2013 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.71 2004/10/01 10:00:36 robbat2 Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.189 2013/09/30 06:39:56 vapier Exp $
4#
5# Author Bart Verwilst <verwilst@gentoo.org>
6 4
7ECLASS=flag-o-matic 5# @ECLASS: flag-o-matic.eclass
8INHERITED="$INHERITED $ECLASS" 6# @MAINTAINER:
7# toolchain@gentoo.org
8# @BLURB: common functions to manipulate and query toolchain flags
9# @DESCRIPTION:
10# This eclass contains a suite of functions to help developers sanely
11# and safely manage toolchain flags in their builds.
9 12
10# Please leave ${IUSE} in this until portage .51 is stable, otherwise 13if [[ ${___ECLASS_ONCE_FLAG_O_MATIC} != "recur -_+^+_- spank" ]] ; then
11# IUSE gets clobbered. 14___ECLASS_ONCE_FLAG_O_MATIC="recur -_+^+_- spank"
12IUSE="${IUSE} debug"
13 15
14# 16inherit eutils toolchain-funcs multilib
15#### filter-flags <flags> ####
16# Remove particular flags from C[XX]FLAGS
17# Matches only complete flags
18#
19#### append-flags <flags> ####
20# Add extra flags to your current C[XX]FLAGS
21#
22#### replace-flags <orig.flag> <new.flag> ###
23# Replace a flag by another one
24#
25#### replace-cpu-flags <old.cpus> <new.cpu> ###
26# Replace march/mcpu flags that specify <old.cpus>
27# with flags that specify <new.cpu>
28#
29#### is-flag <flag> ####
30# Returns "true" if flag is set in C[XX]FLAGS
31# Matches only complete a flag
32#
33#### strip-flags ####
34# Strip C[XX]FLAGS of everything except known
35# good options.
36#
37#### strip-unsupported-flags ####
38# Strip C[XX]FLAGS of any flags not supported by
39# installed version of gcc
40#
41#### get-flag <flag> ####
42# Find and echo the value for a particular flag
43#
44#### replace-sparc64-flags ####
45# Sets mcpu to v8 and uses the original value
46# as mtune if none specified.
47#
48#### filter-mfpmath <math types> ####
49# Remove specified math types from the fpmath specification
50# If the user has -mfpmath=sse,386, running `filter-mfpmath sse`
51# will leave the user with -mfpmath=386
52#
53#### append-ldflags ####
54# Add extra flags to your current LDFLAGS
55#
56#### filter-ldflags <flags> ####
57# Remove particular flags from LDFLAGS
58# Matches only complete flags
59#
60#### etexec-flags ####
61# hooked function for hardened gcc that appends
62# -fno-pic to {C,CXX,LD}FLAGS
63# when a package is filtering -fpic, -fPIC, -fpie, -fPIE
64#
65#### fstack-flags ####
66# hooked function for hardened gcc that appends
67# -fno-stack-protector to {C,CXX,LD}FLAGS
68# when a package is filtering -fstack-protector, -fstack-protector-all
69# notice: modern automatic specs files will also suppress -fstack-protector-all
70# when only -fno-stack-protector is given
71#
72 17
18# Return all the flag variables that our high level funcs operate on.
19all-flag-vars() {
20 echo {C,CPP,CXX,CCAS,F,FC,LD}FLAGS
21}
22
73# C[XX]FLAGS that we allow in strip-flags 23# {C,CPP,CXX,CCAS,F,FC,LD}FLAGS that we allow in strip-flags
24# Note: shell globs and character lists are allowed
74setup-allowed-flags() { 25setup-allowed-flags() {
75 if [ -z "${ALLOWED_FLAGS}" ] ; then 26 ALLOWED_FLAGS="-pipe"
76 export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -fno-stack-protector -fno-pie -fno-unit-at-a-time -pipe -g" 27 ALLOWED_FLAGS+=" -O -O1 -O2 -Os -mcpu -march -mtune"
28 ALLOWED_FLAGS+=" -fstack-protector -fstack-protector-all"
29 ALLOWED_FLAGS+=" -fbounds-checking -fno-strict-overflow"
30 ALLOWED_FLAGS+=" -fno-PIE -fno-pie -fno-unit-at-a-time"
31 ALLOWED_FLAGS+=" -g -g[0-9] -ggdb -ggdb[0-9] -gstabs -gstabs+"
32 ALLOWED_FLAGS+=" -fno-ident -fpermissive"
33 ALLOWED_FLAGS+=" -W* -w"
34
35 # allow a bunch of flags that negate features / control ABI
36 ALLOWED_FLAGS+=" -fno-stack-protector -fno-stack-protector-all \
37 -fno-strict-aliasing -fno-bounds-checking -fstrict-overflow \
38 -fno-omit-frame-pointer"
39 ALLOWED_FLAGS+=" -mregparm -mno-app-regs -mapp-regs -mno-mmx -mno-sse \
40 -mno-sse2 -mno-sse3 -mno-ssse3 -mno-sse4 -mno-sse4.1 -mno-sse4.2 \
41 -mno-avx -mno-aes -mno-pclmul -mno-sse4a -mno-3dnow -mno-popcnt \
42 -mno-abm -mips1 -mips2 -mips3 -mips4 -mips32 -mips64 -mips16 -mplt \
43 -msoft-float -mno-soft-float -mhard-float -mno-hard-float -mfpu \
44 -mieee -mieee-with-inexact -mschedule -mfloat-gprs -mspe -mno-spe \
45 -mtls-direct-seg-refs -mno-tls-direct-seg-refs -mflat -mno-flat \
46 -mno-faster-structs -mfaster-structs -m32 -m64 -mx32 -mabi \
47 -mlittle-endian -mbig-endian -EL -EB -fPIC -mlive-g0 -mcmodel \
48 -mstack-bias -mno-stack-bias -msecure-plt -m*-toc -mfloat-abi \
49 -D* -U*"
50
51 # 4.5
52 ALLOWED_FLAGS+=" -mno-fma4 -mno-movbe -mno-xop -mno-lwp"
53 # 4.6
54 ALLOWED_FLAGS+=" -mno-fsgsbase -mno-rdrnd -mno-f16c -mno-bmi -mno-tbm"
55 # 4.7
56 ALLOWED_FLAGS+=" -mno-avx2 -mno-bmi2 -mno-fma -mno-lzcnt"
57
58 # CPPFLAGS and LDFLAGS
59 ALLOWED_FLAGS+=" -I* -L* -R* -Wl,*"
60
61 export ALLOWED_FLAGS
62 return 0
63}
64
65# inverted filters for hardened compiler. This is trying to unpick
66# the hardened compiler defaults.
67_filter-hardened() {
68 local f
69 for f in "$@" ; do
77 case "${ARCH}" in 70 case "${f}" in
78 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mips32 -mips64 -mips16 -EL -EB -mabi" ;; 71 # Ideally we should only concern ourselves with PIE flags,
79 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC -m64" ;; 72 # not -fPIC or -fpic, but too many places filter -fPIC without
80 x86) ALLOWED_FLAGS="${ALLOWED_FLAGS} -m32" ;; 73 # thinking about -fPIE.
81 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 74 -fPIC|-fpic|-fPIE|-fpie|-Wl,pie|-pie)
82 ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 75 gcc-specs-pie || continue
76 is-flagq -nopie || append-flags -nopie;;
77 -fstack-protector)
78 gcc-specs-ssp || continue
79 is-flagq -fno-stack-protector || append-flags $(test-flags -fno-stack-protector);;
80 -fstack-protector-all)
81 gcc-specs-ssp-to-all || continue
82 is-flagq -fno-stack-protector-all || append-flags $(test-flags -fno-stack-protector-all);;
83 -fno-strict-overflow)
84 gcc-specs-nostrict || continue
85 is-flagq -fstrict-overflow || append-flags $(test-flags -fstrict-overflow);;
83 esac 86 esac
84 fi 87 done
85 # allow a bunch of flags that negate features / control ABI 88}
86 ALLOWED_FLAGS="${ALLOWED_FLAGS} -fno-stack-protector -fno-stack-protector-all" 89
87 case "${ARCH}" in 90# Remove occurrences of strings from variable given in $1
88 x86|amd64|ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow" ;; 91# Strings removed are matched as globs, so for example
92# '-O*' would remove -O1, -O2 etc.
93_filter-var() {
94 local f x var=$1 new=()
95 shift
96
97 for f in ${!var} ; do
98 for x in "$@" ; do
99 # Note this should work with globs like -O*
100 [[ ${f} == ${x} ]] && continue 2
101 done
102 new+=( "${f}" )
103 done
104 eval export ${var}=\""${new[*]}"\"
105}
106
107# @FUNCTION: filter-flags
108# @USAGE: <flags>
109# @DESCRIPTION:
110# Remove particular <flags> from {C,CPP,CXX,CCAS,F,FC,LD}FLAGS. Accepts shell globs.
111filter-flags() {
112 _filter-hardened "$@"
113 local v
114 for v in $(all-flag-vars) ; do
115 _filter-var ${v} "$@"
116 done
117 return 0
118}
119
120# @FUNCTION: filter-lfs-flags
121# @DESCRIPTION:
122# Remove flags that enable Large File Support.
123filter-lfs-flags() {
124 [[ $# -ne 0 ]] && die "filter-lfs-flags takes no arguments"
125 # http://www.gnu.org/s/libc/manual/html_node/Feature-Test-Macros.html
126 # _LARGEFILE_SOURCE: enable support for new LFS funcs (ftello/etc...)
127 # _LARGEFILE64_SOURCE: enable support for 64bit variants (off64_t/fseeko64/etc...)
128 # _FILE_OFFSET_BITS: default to 64bit variants (off_t is defined as off64_t)
129 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
130}
131
132# @FUNCTION: filter-ldflags
133# @USAGE: <flags>
134# @DESCRIPTION:
135# Remove particular <flags> from LDFLAGS. Accepts shell globs.
136filter-ldflags() {
137 _filter-var LDFLAGS "$@"
138 return 0
139}
140
141# @FUNCTION: append-cppflags
142# @USAGE: <flags>
143# @DESCRIPTION:
144# Add extra <flags> to the current CPPFLAGS.
145append-cppflags() {
146 [[ $# -eq 0 ]] && return 0
147 export CPPFLAGS+=" $*"
148 return 0
149}
150
151# @FUNCTION: append-cflags
152# @USAGE: <flags>
153# @DESCRIPTION:
154# Add extra <flags> to the current CFLAGS. If a flag might not be supported
155# with different compilers (or versions), then use test-flags-CC like so:
156# @CODE
157# append-cflags $(test-flags-CC -funky-flag)
158# @CODE
159append-cflags() {
160 [[ $# -eq 0 ]] && return 0
161 # Do not do automatic flag testing ourselves. #417047
162 export CFLAGS+=" $*"
163 return 0
164}
165
166# @FUNCTION: append-cxxflags
167# @USAGE: <flags>
168# @DESCRIPTION:
169# Add extra <flags> to the current CXXFLAGS. If a flag might not be supported
170# with different compilers (or versions), then use test-flags-CXX like so:
171# @CODE
172# append-cxxflags $(test-flags-CXX -funky-flag)
173# @CODE
174append-cxxflags() {
175 [[ $# -eq 0 ]] && return 0
176 # Do not do automatic flag testing ourselves. #417047
177 export CXXFLAGS+=" $*"
178 return 0
179}
180
181# @FUNCTION: append-fflags
182# @USAGE: <flags>
183# @DESCRIPTION:
184# Add extra <flags> to the current {F,FC}FLAGS. If a flag might not be supported
185# with different compilers (or versions), then use test-flags-F77 like so:
186# @CODE
187# append-fflags $(test-flags-F77 -funky-flag)
188# @CODE
189append-fflags() {
190 [[ $# -eq 0 ]] && return 0
191 # Do not do automatic flag testing ourselves. #417047
192 export FFLAGS+=" $*"
193 export FCFLAGS+=" $*"
194 return 0
195}
196
197# @FUNCTION: append-lfs-flags
198# @DESCRIPTION:
199# Add flags that enable Large File Support.
200append-lfs-flags() {
201 [[ $# -ne 0 ]] && die "append-lfs-flags takes no arguments"
202 # see comments in filter-lfs-flags func for meaning of these
203 append-cppflags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
204}
205
206# @FUNCTION: append-ldflags
207# @USAGE: <flags>
208# @DESCRIPTION:
209# Add extra <flags> to the current LDFLAGS.
210append-ldflags() {
211 [[ $# -eq 0 ]] && return 0
212 local flag
213 for flag in "$@"; do
214 [[ ${flag} == -l* ]] && \
215 eqawarn "Appending a library link instruction (${flag}); libraries to link to should not be passed through LDFLAGS"
216 done
217
218 export LDFLAGS="${LDFLAGS} $*"
219 return 0
220}
221
222# @FUNCTION: append-flags
223# @USAGE: <flags>
224# @DESCRIPTION:
225# Add extra <flags> to your current {C,CXX,F,FC}FLAGS.
226append-flags() {
227 [[ $# -eq 0 ]] && return 0
228 case " $* " in
229 *' '-[DIU]*) eqawarn 'please use append-cppflags for preprocessor flags' ;;
230 *' '-L*|\
231 *' '-Wl,*) eqawarn 'please use append-ldflags for linker flags' ;;
89 esac 232 esac
90 ALLOWED_FLAGS="${ALLOWED_FLAGS} -mregparm -mno-app-regs -mapp-regs -msoft-float -mflat -mno-faster-structs -mfaster-structs -mlittle-endian -mbig-endian -mlive-g0 -mcmodel -mno-stack-bias -mstack-bias" 233 append-cflags "$@"
91 234 append-cxxflags "$@"
92 # C[XX]FLAGS that we are think is ok, but needs testing 235 append-fflags "$@"
93 # NOTE: currently -Os have issues with gcc3 and K6* arch's
94 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays"
95 return 0 236 return 0
96} 237}
97 238
98filter-flags() { 239# @FUNCTION: replace-flags
99 local x f fset 240# @USAGE: <old> <new>
100 declare -a new_CFLAGS new_CXXFLAGS 241# @DESCRIPTION:
242# Replace the <old> flag with <new>. Accepts shell globs for <old>.
243replace-flags() {
244 [[ $# != 2 ]] && die "Usage: replace-flags <old flag> <new flag>"
101 245
102 for x in "$@" ; do 246 local f var new
103 case "${x}" in 247 for var in $(all-flag-vars) ; do
104 -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;;
105 -fstack-protector|-fstack-protector-all) fstack-flags;;
106 esac
107 done
108
109 for fset in CFLAGS CXXFLAGS; do
110 # Looping over the flags instead of using a global 248 # Looping over the flags instead of using a global
111 # substitution ensures that we're working with flag atoms. 249 # substitution ensures that we're working with flag atoms.
112 # Otherwise globs like -O* have the potential to wipe out the 250 # Otherwise globs like -O* have the potential to wipe out the
113 # list of flags. 251 # list of flags.
252 new=()
114 for f in ${!fset}; do 253 for f in ${!var} ; do
115 for x in "$@"; do
116 # Note this should work with globs like -O*
117 [[ ${f} == ${x} ]] && continue 2
118 done
119 eval new_${fset}\[\${\#new_${fset}\[@]}]=\${f}
120 done
121 eval export ${fset}=\${new_${fset}\[*]}
122 done
123
124 return 0
125}
126
127filter-lfs-flags() {
128 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
129}
130
131append-lfs-flags() {
132 append-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
133}
134
135append-flags() {
136 export CFLAGS="${CFLAGS} $*"
137 export CXXFLAGS="${CXXFLAGS} $*"
138 [ -n "`is-flag -fno-stack-protector`" -o \
139 -n "`is-flag -fno-stack-protector-all`" ] && fstack-flags
140 return 0
141}
142
143replace-flags() {
144 local f fset
145 declare -a new_CFLAGS new_CXXFLAGS
146
147 for fset in CFLAGS CXXFLAGS; do
148 # Looping over the flags instead of using a global
149 # substitution ensures that we're working with flag atoms.
150 # Otherwise globs like -O* have the potential to wipe out the
151 # list of flags.
152 for f in ${!fset}; do
153 # Note this should work with globs like -O* 254 # Note this should work with globs like -O*
154 [[ ${f} == ${1} ]] && f=${2} 255 [[ ${f} == ${1} ]] && f=${2}
155 eval new_${fset}\[\${\#new_${fset}\[@]}]=\${f} 256 new+=( "${f}" )
156 done
157 eval export ${fset}=\${new_${fset}\[*]}
158 done 257 done
258 eval export ${var}=\""${new[*]}"\"
259 done
159 260
160 return 0 261 return 0
161} 262}
162 263
264# @FUNCTION: replace-cpu-flags
265# @USAGE: <old> <new>
266# @DESCRIPTION:
267# Replace cpu flags (like -march/-mcpu/-mtune) that select the <old> cpu
268# with flags that select the <new> cpu. Accepts shell globs for <old>.
163replace-cpu-flags() { 269replace-cpu-flags() {
164 local newcpu="$#" ; newcpu="${!newcpu}" 270 local newcpu="$#" ; newcpu="${!newcpu}"
165 while [ $# -gt 1 ] ; do 271 while [ $# -gt 1 ] ; do
166 # quote to make sure that no globbing is done (particularly on 272 # quote to make sure that no globbing is done (particularly on
167 # ${oldcpu} prior to calling replace-flags 273 # ${oldcpu}) prior to calling replace-flags
168 replace-flags "-march=${1}" "-march=${newcpu}" 274 replace-flags "-march=${1}" "-march=${newcpu}"
169 replace-flags "-mcpu=${1}" "-mcpu=${newcpu}" 275 replace-flags "-mcpu=${1}" "-mcpu=${newcpu}"
170 replace-flags "-mtune=${1}" "-mtune=${newcpu}" 276 replace-flags "-mtune=${1}" "-mtune=${newcpu}"
171 shift 277 shift
172 done 278 done
173 return 0 279 return 0
174} 280}
175 281
282_is_flagq() {
283 local x var
284 eval var=\""\${$1[*]}"\"
285 for x in ${var} ; do
286 [[ ${x} == $2 ]] && return 0
287 done
288 return 1
289}
290
291# @FUNCTION: is-flagq
292# @USAGE: <flag>
293# @DESCRIPTION:
294# Returns shell true if <flag> is in {C,CXX,F,FC}FLAGS, else returns shell false. Accepts shell globs.
295is-flagq() {
296 [[ -n $2 ]] && die "Usage: is-flag <flag>"
297
298 local var
299 for var in $(all-flag-vars) ; do
300 _is_flagq ${var} "$1" && return 0
301 done
302 return 1
303}
304
305# @FUNCTION: is-flag
306# @USAGE: <flag>
307# @DESCRIPTION:
308# Echo's "true" if flag is set in {C,CXX,F,FC}FLAGS. Accepts shell globs.
176is-flag() { 309is-flag() {
177 local x 310 is-flagq "$@" && echo true
178
179 for x in ${CFLAGS} ${CXXFLAGS} ; do
180 # Note this should work with globs like -mcpu=ultrasparc*
181 if [[ ${x} == ${1} ]]; then
182 echo true
183 return 0
184 fi
185 done
186 return 1
187} 311}
188 312
313# @FUNCTION: is-ldflagq
314# @USAGE: <flag>
315# @DESCRIPTION:
316# Returns shell true if <flag> is in LDFLAGS, else returns shell false. Accepts shell globs.
317is-ldflagq() {
318 [[ -n $2 ]] && die "Usage: is-ldflag <flag>"
319 _is_flagq LDFLAGS $1
320}
321
322# @FUNCTION: is-ldflag
323# @USAGE: <flag>
324# @DESCRIPTION:
325# Echo's "true" if flag is set in LDFLAGS. Accepts shell globs.
326is-ldflag() {
327 is-ldflagq "$@" && echo true
328}
329
330# @FUNCTION: filter-mfpmath
331# @USAGE: <math types>
332# @DESCRIPTION:
333# Remove specified math types from the fpmath flag. For example, if the user
334# has -mfpmath=sse,386, running `filter-mfpmath sse` will leave the user with
335# -mfpmath=386.
189filter-mfpmath() { 336filter-mfpmath() {
190 local orig_mfpmath new_math prune_math 337 local orig_mfpmath new_math prune_math
191 338
192 # save the original -mfpmath flag 339 # save the original -mfpmath flag
193 orig_mfpmath="`get-flag -mfpmath`" 340 orig_mfpmath=$(get-flag -mfpmath)
194 # get the value of the current -mfpmath flag 341 # get the value of the current -mfpmath flag
195 new_math=" `get-flag mfpmath | tr , ' '` " 342 new_math=$(get-flag mfpmath)
343 # convert "both" to something we can filter
344 new_math=${new_math/both/387,sse}
345 new_math=" ${new_math//[,+]/ } "
196 # figure out which math values are to be removed 346 # figure out which math values are to be removed
197 prune_math="" 347 prune_math=""
198 for prune_math in "$@" ; do 348 for prune_math in "$@" ; do
199 new_math="${new_math/ ${prune_math} / }" 349 new_math=${new_math/ ${prune_math} / }
200 done 350 done
201 new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`" 351 new_math=$(echo ${new_math})
352 new_math=${new_math// /,}
202 353
203 if [ -z "${new_math}" ] ; then 354 if [[ -z ${new_math} ]] ; then
204 # if we're removing all user specified math values are 355 # if we're removing all user specified math values are
205 # slated for removal, then we just filter the flag 356 # slated for removal, then we just filter the flag
206 filter-flags ${orig_mfpmath} 357 filter-flags ${orig_mfpmath}
207 else 358 else
208 # if we only want to filter some of the user specified 359 # if we only want to filter some of the user specified
210 replace-flags ${orig_mfpmath} -mfpmath=${new_math} 361 replace-flags ${orig_mfpmath} -mfpmath=${new_math}
211 fi 362 fi
212 return 0 363 return 0
213} 364}
214 365
366# @FUNCTION: strip-flags
367# @DESCRIPTION:
368# Strip *FLAGS of everything except known good/safe flags. This runs over all
369# flags returned by all_flag_vars().
215strip-flags() { 370strip-flags() {
216 local x y flag NEW_CFLAGS NEW_CXXFLAGS 371 local x y var
217 372
218 setup-allowed-flags 373 setup-allowed-flags
219 374
220 local NEW_CFLAGS="" 375 set -f # disable pathname expansion
221 local NEW_CXXFLAGS=""
222 376
223 # Allow unstable C[XX]FLAGS if we are using unstable profile ... 377 for var in $(all-flag-vars) ; do
224 if has ~${ARCH} ${ACCEPT_KEYWORDS} ; then 378 local new=()
225 use debug && einfo "Enabling the use of some unstable flags" 379
226 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}" 380 for x in ${!var} ; do
381 local flag=${x%%=*}
382 for y in ${ALLOWED_FLAGS} ; do
383 if [[ -z ${flag%%${y}} ]] ; then
384 new+=( "${x}" )
385 break
386 fi
387 done
388 done
389
390 # In case we filtered out all optimization flags fallback to -O2
391 if _is_flagq ${var} "-O*" && ! _is_flagq new "-O*" ; then
392 new+=( -O2 )
393 fi
394
395 eval export ${var}=\""${new[*]}"\"
396 done
397
398 set +f # re-enable pathname expansion
399
400 return 0
401}
402
403test-flag-PROG() {
404 local comp=$1
405 local lang=$2
406 local flag=$3
407
408 [[ -z ${comp} || -z ${flag} ]] && return 1
409
410 # use -c so we can test the assembler as well
411 local PROG=$(tc-get${comp})
412 if ${PROG} -c -o /dev/null -x${lang} - < /dev/null > /dev/null 2>&1 ; then
413 ${PROG} "${flag}" -c -o /dev/null -x${lang} - < /dev/null \
414 > /dev/null 2>&1
415 else
416 ${PROG} "${flag}" -c -o /dev/null /dev/null > /dev/null 2>&1
227 fi 417 fi
418}
228 419
229 set -f # disable pathname expansion 420# @FUNCTION: test-flag-CC
421# @USAGE: <flag>
422# @DESCRIPTION:
423# Returns shell true if <flag> is supported by the C compiler, else returns shell false.
424test-flag-CC() { test-flag-PROG "CC" c "$1"; }
230 425
231 for x in ${CFLAGS}; do 426# @FUNCTION: test-flag-CXX
232 for y in ${ALLOWED_FLAGS}; do 427# @USAGE: <flag>
233 flag=${x%%=*} 428# @DESCRIPTION:
234 if [ "${flag%%${y}}" = "" ] ; then 429# Returns shell true if <flag> is supported by the C++ compiler, else returns shell false.
235 NEW_CFLAGS="${NEW_CFLAGS} ${x}" 430test-flag-CXX() { test-flag-PROG "CXX" c++ "$1"; }
236 break 431
237 fi 432# @FUNCTION: test-flag-F77
433# @USAGE: <flag>
434# @DESCRIPTION:
435# Returns shell true if <flag> is supported by the Fortran 77 compiler, else returns shell false.
436test-flag-F77() { test-flag-PROG "F77" f77 "$1"; }
437
438# @FUNCTION: test-flag-FC
439# @USAGE: <flag>
440# @DESCRIPTION:
441# Returns shell true if <flag> is supported by the Fortran 90 compiler, else returns shell false.
442test-flag-FC() { test-flag-PROG "FC" f95 "$1"; }
443
444test-flags-PROG() {
445 local comp=$1
446 local flags=()
447 local x
448
449 shift
450
451 [[ -z ${comp} ]] && return 1
452
453 for x ; do
454 test-flag-${comp} "${x}" && flags+=( "${x}" )
238 done 455 done
239 done
240 456
241 for x in ${CXXFLAGS}; do 457 echo "${flags[*]}"
242 for y in ${ALLOWED_FLAGS}; do
243 flag=${x%%=*}
244 if [ "${flag%%${y}}" = "" ] ; then
245 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}"
246 break
247 fi
248 done
249 done
250 458
251 # In case we filtered out all optimization flags fallback to -O2 459 # Just bail if we dont have any flags
252 if [ "${CFLAGS/-O}" != "${CFLAGS}" -a "${NEW_CFLAGS/-O}" = "${NEW_CFLAGS}" ]; then 460 [[ ${#flags[@]} -gt 0 ]]
253 NEW_CFLAGS="${NEW_CFLAGS} -O2"
254 fi
255 if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then
256 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2"
257 fi
258
259 set +f # re-enable pathname expansion
260
261 export CFLAGS="${NEW_CFLAGS}"
262 export CXXFLAGS="${NEW_CXXFLAGS}"
263 return 0
264} 461}
265 462
266test_flag() { 463# @FUNCTION: test-flags-CC
267 local cc=${CC:-gcc} ; cc=${cc%% *} 464# @USAGE: <flags>
268 if ${cc} -S -xc "$@" -o /dev/null /dev/null &>/dev/null; then 465# @DESCRIPTION:
269 printf "%s\n" "$*" 466# Returns shell true if <flags> are supported by the C compiler, else returns shell false.
270 return 0 467test-flags-CC() { test-flags-PROG "CC" "$@"; }
271 fi
272 return 1
273}
274 468
469# @FUNCTION: test-flags-CXX
470# @USAGE: <flags>
471# @DESCRIPTION:
472# Returns shell true if <flags> are supported by the C++ compiler, else returns shell false.
473test-flags-CXX() { test-flags-PROG "CXX" "$@"; }
474
475# @FUNCTION: test-flags-F77
476# @USAGE: <flags>
477# @DESCRIPTION:
478# Returns shell true if <flags> are supported by the Fortran 77 compiler, else returns shell false.
479test-flags-F77() { test-flags-PROG "F77" "$@"; }
480
481# @FUNCTION: test-flags-FC
482# @USAGE: <flags>
483# @DESCRIPTION:
484# Returns shell true if <flags> are supported by the Fortran 90 compiler, else returns shell false.
485test-flags-FC() { test-flags-PROG "FC" "$@"; }
486
487# @FUNCTION: test-flags
488# @USAGE: <flags>
489# @DESCRIPTION:
490# Short-hand that should hopefully work for both C and C++ compiler, but
491# its really only present due to the append-flags() abomination.
492test-flags() { test-flags-CC "$@"; }
493
494# @FUNCTION: test_version_info
495# @USAGE: <version>
496# @DESCRIPTION:
497# Returns shell true if the current C compiler version matches <version>, else returns shell false.
498# Accepts shell globs.
275test_version_info() { 499test_version_info() {
276 local cc=${CC:-gcc} ; cc=${cc%% *}
277 if [[ $(${cc} --version 2>&1) == *$1* ]]; then 500 if [[ $($(tc-getCC) --version 2>&1) == *$1* ]]; then
278 return 0 501 return 0
279 else 502 else
280 return 1 503 return 1
281 fi 504 fi
282} 505}
283 506
507# @FUNCTION: strip-unsupported-flags
508# @DESCRIPTION:
509# Strip {C,CXX,F,FC}FLAGS of any flags not supported by the active toolchain.
284strip-unsupported-flags() { 510strip-unsupported-flags() {
285 local NEW_CFLAGS NEW_CXXFLAGS 511 export CFLAGS=$(test-flags-CC ${CFLAGS})
286 512 export CXXFLAGS=$(test-flags-CXX ${CXXFLAGS})
287 for x in ${CFLAGS} ; do 513 export FFLAGS=$(test-flags-F77 ${FFLAGS})
288 NEW_CFLAGS="${NEW_CFLAGS} `test_flag ${x}`" 514 export FCFLAGS=$(test-flags-FC ${FCFLAGS})
289 done
290 for x in ${CXXFLAGS} ; do
291 NEW_CXXFLAGS="${NEW_CXXFLAGS} `test_flag ${x}`"
292 done
293
294 export CFLAGS="${NEW_CFLAGS}"
295 export CXXFLAGS="${NEW_CXXFLAGS}"
296} 515}
297 516
517# @FUNCTION: get-flag
518# @USAGE: <flag>
519# @DESCRIPTION:
520# Find and echo the value for a particular flag. Accepts shell globs.
298get-flag() { 521get-flag() {
299 local f findflag="$1" 522 local f var findflag="$1"
300 523
301 # this code looks a little flaky but seems to work for 524 # this code looks a little flaky but seems to work for
302 # everything we want ... 525 # everything we want ...
303 # for example, if CFLAGS="-march=i686": 526 # for example, if CFLAGS="-march=i686":
304 # `get-flag -march` == "-march=i686" 527 # `get-flag -march` == "-march=i686"
305 # `get-flag march` == "i686" 528 # `get-flag march` == "i686"
306 for f in ${CFLAGS} ${CXXFLAGS} ; do 529 for var in $(all-flag-vars) ; do
530 for f in ${!var} ; do
307 if [ "${f/${findflag}}" != "${f}" ] ; then 531 if [ "${f/${findflag}}" != "${f}" ] ; then
308 printf "%s\n" "${f/-${findflag}=}" 532 printf "%s\n" "${f/-${findflag}=}"
309 return 0 533 return 0
310 fi 534 fi
535 done
311 done 536 done
312 return 1 537 return 1
313} 538}
314 539
315has_hardened() { 540# @FUNCTION: has_m64
316 test_version_info Hardened && return 0 541# @DESCRIPTION:
317 # the specs file wont exist unless gcc has GCC_SPECS support 542# This doesn't test if the flag is accepted, it tests if the flag actually
318 [ -f "${GCC_SPECS}" -a "${GCC_SPECS}" != "${GCC_SPECS/hardened/}" ] && \ 543# WORKS. Non-multilib gcc will take both -m32 and -m64. If the flag works
319 return 0 544# return code is 0, else the return code is 1.
320 return 1
321}
322
323has_pic() {
324 [ "${CFLAGS/-fPIC}" != "${CFLAGS}" ] && return 0
325 [ "${CFLAGS/-fpic}" != "${CFLAGS}" ] && return 0
326 test_version_info pie && return 0
327 return 1
328}
329
330has_pie() {
331 [ "${CFLAGS/-fPIE}" != "${CFLAGS}" ] && return 0
332 [ "${CFLAGS/-fpie}" != "${CFLAGS}" ] && return 0
333 test_version_info pie && return 0
334 return 1
335}
336
337has_ssp() {
338 [ "${CFLAGS/-fstack-protector}" != "${CFLAGS}" ] && return 0
339 test_version_info ssp && return 0
340 return 1
341}
342
343has_m64() { 545has_m64() {
546 eqawarn "${FUNCNAME}: don't use this anymore"
547
344 # this doesnt test if the flag is accepted, it tests if the flag 548 # this doesnt test if the flag is accepted, it tests if the flag
345 # actually -WORKS-. non-multilib gcc will take both -m32 and -m64! 549 # actually -WORKS-. non-multilib gcc will take both -m32 and -m64!
346 # please dont replace this function with test_flag in some future 550 # please dont replace this function with test_flag in some future
347 # clean-up! 551 # clean-up!
348 temp=`mktemp` 552
553 local temp="$(emktemp)"
349 echo "int main() { return(0); }" > ${temp}.c 554 echo "int main() { return(0); }" > "${temp}".c
350 ${CC/ .*/} -m64 -o /dev/null ${temp}.c > /dev/null 2>&1 555 MY_CC=$(tc-getCC)
351 ret=$? 556 ${MY_CC/ .*/} -m64 -o "$(emktemp)" "${temp}".c > /dev/null 2>&1
557 local ret=$?
352 rm -f ${temp}.c 558 rm -f "${temp}".c
353 [ "$ret" != "1" ] && return 0 559 [[ ${ret} != 1 ]] && return 0
354 return 1 560 return 1
355} 561}
356 562
357has_m32() { 563has_m32() {
358 # this doesnt test if the flag is accepted, it tests if the flag 564 die "${FUNCNAME}: don't use this anymore"
359 # actually -WORKS-. non-multilib gcc will take both -m32 and -m64!
360 # please dont replace this function with test_flag in some future
361 # clean-up!
362 temp=`mktemp`
363 echo "int main() { return(0); }" > ${temp}.c
364 ${CC/ .*/} -m32 -o /dev/null ${temp}.c > /dev/null 2>&1
365 ret=$?
366 rm -f ${temp}.c
367 [ "$ret" != "1" ] && return 0
368 return 1
369} 565}
370 566
567# @FUNCTION: replace-sparc64-flags
568# @DESCRIPTION:
569# Sets mcpu to v8 and uses the original value as mtune if none specified.
371replace-sparc64-flags() { 570replace-sparc64-flags() {
372 local SPARC64_CPUS="ultrasparc v9" 571 local SPARC64_CPUS="ultrasparc3 ultrasparc v9"
373 572
374 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then 573 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
375 for x in ${SPARC64_CPUS}; do 574 for x in ${SPARC64_CPUS}; do
376 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}" 575 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
377 done 576 done
378 else 577 else
379 for x in ${SPARC64_CPUS}; do 578 for x in ${SPARC64_CPUS}; do
380 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" 579 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
381 done 580 done
382 fi 581 fi
383 582
384 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then 583 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
385 for x in ${SPARC64_CPUS}; do 584 for x in ${SPARC64_CPUS}; do
386 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}" 585 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
387 done 586 done
388 else 587 else
389 for x in ${SPARC64_CPUS}; do 588 for x in ${SPARC64_CPUS}; do
390 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" 589 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
391 done 590 done
392 fi 591 fi
393 592
394 export CFLAGS CXXFLAGS 593 export CFLAGS CXXFLAGS
395} 594}
396 595
596# @FUNCTION: append-libs
597# @USAGE: <libs>
598# @DESCRIPTION:
599# Add extra <libs> to the current LIBS.
397append-ldflags() { 600append-libs() {
398 export LDFLAGS="${LDFLAGS} $*" 601 [[ $# -eq 0 ]] && return 0
399 return 0 602 local flag
400}
401
402filter-ldflags() {
403 local x
404
405 # we do this fancy spacing stuff so as to not filter
406 # out part of a flag ... we want flag atoms ! :D
407 LDFLAGS=" ${LDFLAGS} "
408 for x in "$@" ; do 603 for flag in "$@"; do
409 LDFLAGS="${LDFLAGS// ${x} / }" 604 [[ ${flag} == -l* ]] && flag=${flag#-l}
605 export LIBS="${LIBS} -l${flag}"
410 done 606 done
411 LDFLAGS="${LDFLAGS:1:${#LDFLAGS}-2}"
412 export LDFLAGS
413 return 0
414}
415 607
416etexec-flags() { 608 return 0
417 # if you're not using a hardened compiler you wont need this 609}
418 # PIC/no-pic kludge in the first place.
419 has_hardened || return 0
420 # this kludge breaks on amd64, and probably other -fPIC dependant
421 # archs.
422 use amd64 && return 0
423 610
424 if has_pie || has_pic; then 611# @FUNCTION: raw-ldflags
425 [ -z "`is-flag -fno-pic`" ] && 612# @USAGE: [flags]
426 export CFLAGS="${CFLAGS} `test_flag -fno-pic`" 613# @DESCRIPTION:
427 [ -z "`is-flag -nopie`" ] && 614# Turn C style ldflags (-Wl,-foo) into straight ldflags - the results
428 export CFLAGS="${CFLAGS} `test_flag -nopie`" 615# are suitable for passing directly to 'ld'; note LDFLAGS is usually passed
616# to gcc where it needs the '-Wl,'.
617#
618# If no flags are specified, then default to ${LDFLAGS}.
619raw-ldflags() {
620 local x input="$@"
621 [[ -z ${input} ]] && input=${LDFLAGS}
622 set --
623 for x in ${input} ; do
624 case ${x} in
625 -Wl,*)
626 x=${x#-Wl,}
627 set -- "$@" ${x//,/ }
628 ;;
629 *) # Assume it's a compiler driver flag, so throw it away #441808
630 ;;
631 esac
632 done
633 echo "$@"
634}
635
636# @FUNCTION: no-as-needed
637# @RETURN: Flag to disable asneeded behavior for use with append-ldflags.
638no-as-needed() {
639 case $($(tc-getLD) -v 2>&1 </dev/null) in
640 *GNU*) # GNU ld
641 echo "-Wl,--no-as-needed" ;;
642 esac
643}
644
429 fi 645fi
430 return 0
431}
432
433fstack-flags() {
434 if has_ssp; then
435 [ -z "`is-flag -fno-stack-protector`" ] &&
436 export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`"
437 fi
438 return 0
439}

Legend:
Removed from v.1.71  
changed lines
  Added in v.1.189

  ViewVC Help
Powered by ViewVC 1.1.20