/[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.41 Revision 1.191
1# Copyright 1999-2003 Gentoo Technologies, Inc. 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.41 2004/03/16 21:37:09 solar Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.191 2013/10/27 07:21:05 mgorny 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# 13if [[ ${___ECLASS_ONCE_FLAG_O_MATIC} != "recur -_+^+_- spank" ]] ; then
11#### filter-flags <flags> #### 14___ECLASS_ONCE_FLAG_O_MATIC="recur -_+^+_- spank"
12# Remove particular flags from C[XX]FLAGS
13# Matches only complete flags
14#
15#### append-flags <flags> ####
16# Add extra flags to your current C[XX]FLAGS
17#
18#### replace-flags <orig.flag> <new.flag> ###
19# Replace a flag by another one
20#
21#### replace-cpu-flags <new.cpu> <old.cpus> ###
22# Replace march/mcpu flags that specify <old.cpus>
23# with flags that specify <new.cpu>
24#
25#### is-flag <flag> ####
26# Returns "true" if flag is set in C[XX]FLAGS
27# Matches only complete a flag
28#
29#### strip-flags ####
30# Strip C[XX]FLAGS of everything except known
31# good options.
32#
33#### strip-unsupported-flags ####
34# Strip C[XX]FLAGS of any flags not supported by
35# installed version of gcc
36#
37#### get-flag <flag> ####
38# Find and echo the value for a particular flag
39#
40#### replace-sparc64-flags ####
41# Sets mcpu to v8 and uses the original value
42# as mtune if none specified.
43#
44#### filter-mfpmath <math types> ####
45# Remove specified math types from the fpmath specification
46# If the user has -mfpmath=sse,386, running `filter-mfpmath sse`
47# will leave the user with -mfpmath=386
48#
49#### append-ldflags ####
50# Add extra flags to your current LDFLAGS
51#
52#### filter-ldflags <flags> ####
53# Remove particular flags from LDFLAGS
54# Matches only complete flags
55#
56#### etexec-flags ####
57# hooked function for hardened-gcc that appends
58# -yet_exec {C,CXX,LD}FLAGS when hardened-gcc is installed
59# and a package is filtering -fPIC,-fpic, -fPIE, -fpie
60#
61#### fstack-flags ####
62# hooked function for hardened-gcc that appends
63# -yno_propolice to {C,CXX,LD}FLAGS when hardened-gcc is installed
64# and a package is filtering -fstack-protector, -fstack-protector-all
65#
66 15
16inherit eutils toolchain-funcs multilib
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
67# 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
68setup-allowed-flags() { 25setup-allowed-flags() {
69 export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -pipe -g" 26 ALLOWED_FLAGS="-pipe"
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 -frecord-gcc-switches"
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
70 case "${ARCH}" in 70 case "${f}" in
71 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; 71 # Ideally we should only concern ourselves with PIE flags,
72 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 72 # not -fPIC or -fpic, but too many places filter -fPIC without
73 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 73 # thinking about -fPIE.
74 ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 74 -fPIC|-fpic|-fPIE|-fpie|-Wl,pie|-pie)
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);;
86 esac
87 done
88}
89
90# Remove occurrences of strings from variable given in $1
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' ;;
75 esac 232 esac
76 233 append-cflags "$@"
77 # C[XX]FLAGS that we are think is ok, but needs testing 234 append-cxxflags "$@"
78 # NOTE: currently -Os have issues with gcc3 and K6* arch's 235 append-fflags "$@"
79 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays"
80 return 0 236 return 0
81} 237}
82 238
83filter-flags() { 239# @FUNCTION: replace-flags
84 for x in "$@" ; do 240# @USAGE: <old> <new>
85 case "${x}" in 241# @DESCRIPTION:
86 -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;; 242# Replace the <old> flag with <new>. Accepts shell globs for <old>.
87 -fstack-protector|-fstack-protector-all) fstack-flags;;
88 *) ;;
89 esac
90 done
91
92 # we do this fancy spacing stuff so as to not filter
93 # out part of a flag ... we want flag atoms ! :D
94 CFLAGS=" ${CFLAGS} "
95 CXXFLAGS=" ${CXXFLAGS} "
96 for x in "$@" ; do
97 CFLAGS="${CFLAGS// ${x} / }"
98 CXXFLAGS="${CXXFLAGS// ${x} / }"
99 done
100 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
101 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
102 return 0
103}
104
105filter-lfs-flags() {
106 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
107}
108
109append-flags() {
110 export CFLAGS="${CFLAGS} $@"
111 export CXXFLAGS="${CXXFLAGS} $@"
112 [ "`is-flag -fno-stack-protector`" -o "`is-flag -fno-stack-protector-all`" ] && fstack-flags
113 return 0
114}
115
116replace-flags() { 243replace-flags() {
117 # we do this fancy spacing stuff so as to not filter 244 [[ $# != 2 ]] && die "Usage: replace-flags <old flag> <new flag>"
118 # out part of a flag ... we want flag atoms ! :D
119 CFLAGS=" ${CFLAGS} "
120 CXXFLAGS=" ${CXXFLAGS} "
121 CFLAGS="${CFLAGS// ${1} / ${2} }"
122 CXXFLAGS="${CXXFLAGS// ${1} / ${2} }"
123 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
124 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
125 return 0
126}
127 245
246 local f var new
247 for var in $(all-flag-vars) ; do
248 # Looping over the flags instead of using a global
249 # substitution ensures that we're working with flag atoms.
250 # Otherwise globs like -O* have the potential to wipe out the
251 # list of flags.
252 new=()
253 for f in ${!var} ; do
254 # Note this should work with globs like -O*
255 [[ ${f} == ${1} ]] && f=${2}
256 new+=( "${f}" )
257 done
258 eval export ${var}=\""${new[*]}"\"
259 done
260
261 return 0
262}
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>.
128replace-cpu-flags() { 269replace-cpu-flags() {
129 local newcpu="$1" ; shift 270 local newcpu="$#" ; newcpu="${!newcpu}"
130 local oldcpu="" 271 while [ $# -gt 1 ] ; do
131 for oldcpu in "$@" ; do 272 # quote to make sure that no globbing is done (particularly on
273 # ${oldcpu}) prior to calling replace-flags
132 replace-flags -march=${oldcpu} -march=${newcpu} 274 replace-flags "-march=${1}" "-march=${newcpu}"
133 replace-flags -mcpu=${oldcpu} -mcpu=${newcpu} 275 replace-flags "-mcpu=${1}" "-mcpu=${newcpu}"
134 replace-flags -mtune=${oldcpu} -mtune=${newcpu} 276 replace-flags "-mtune=${1}" "-mtune=${newcpu}"
277 shift
135 done 278 done
279 return 0
280}
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
136 return 0 288 return 1
137} 289}
138 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.
139is-flag() { 309is-flag() {
140 for x in ${CFLAGS} ${CXXFLAGS} ; do 310 is-flagq "$@" && echo true
141 if [ "${x}" == "$1" ] ; then
142 echo true
143 return 0
144 fi
145 done
146 return 1
147} 311}
148 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.
149filter-mfpmath() { 336filter-mfpmath() {
337 local orig_mfpmath new_math prune_math
338
150 # save the original -mfpmath flag 339 # save the original -mfpmath flag
151 local orig_mfpmath="`get-flag -mfpmath`" 340 orig_mfpmath=$(get-flag -mfpmath)
152 # get the value of the current -mfpmath flag 341 # get the value of the current -mfpmath flag
153 local 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//[,+]/ } "
154 # figure out which math values are to be removed 346 # figure out which math values are to be removed
155 local prune_math="" 347 prune_math=""
156 for prune_math in "$@" ; do 348 for prune_math in "$@" ; do
157 new_math="${new_math/ ${prune_math} / }" 349 new_math=${new_math/ ${prune_math} / }
158 done 350 done
159 new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`" 351 new_math=$(echo ${new_math})
352 new_math=${new_math// /,}
160 353
161 if [ -z "${new_math}" ] ; then 354 if [[ -z ${new_math} ]] ; then
162 # if we're removing all user specified math values are 355 # if we're removing all user specified math values are
163 # slated for removal, then we just filter the flag 356 # slated for removal, then we just filter the flag
164 filter-flags ${orig_mfpmath} 357 filter-flags ${orig_mfpmath}
165 else 358 else
166 # if we only want to filter some of the user specified 359 # if we only want to filter some of the user specified
168 replace-flags ${orig_mfpmath} -mfpmath=${new_math} 361 replace-flags ${orig_mfpmath} -mfpmath=${new_math}
169 fi 362 fi
170 return 0 363 return 0
171} 364}
172 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().
173strip-flags() { 370strip-flags() {
371 local x y var
372
174 setup-allowed-flags 373 setup-allowed-flags
175 374
176 local NEW_CFLAGS="" 375 set -f # disable pathname expansion
177 local NEW_CXXFLAGS=""
178 376
179 # Allow unstable C[XX]FLAGS if we are using unstable profile ... 377 for var in $(all-flag-vars) ; do
180 if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then 378 local new=()
181 [ `use debug` ] && einfo "Enabling the use of some unstable flags" 379
182 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 if [[ ${!var} != "${new[*]}" ]] ; then
396 einfo "strip-flags: ${var}: changed '${!var}' to '${new[*]}'"
397 fi
398 eval export ${var}=\""${new[*]}"\"
399 done
400
401 set +f # re-enable pathname expansion
402
403 return 0
404}
405
406test-flag-PROG() {
407 local comp=$1
408 local lang=$2
409 local flag=$3
410
411 [[ -z ${comp} || -z ${flag} ]] && return 1
412
413 # use -c so we can test the assembler as well
414 local PROG=$(tc-get${comp})
415 if ${PROG} -c -o /dev/null -x${lang} - < /dev/null > /dev/null 2>&1 ; then
416 ${PROG} "${flag}" -c -o /dev/null -x${lang} - < /dev/null \
417 > /dev/null 2>&1
418 else
419 ${PROG} "${flag}" -c -o /dev/null /dev/null > /dev/null 2>&1
183 fi 420 fi
421}
184 422
185 set -f 423# @FUNCTION: test-flag-CC
424# @USAGE: <flag>
425# @DESCRIPTION:
426# Returns shell true if <flag> is supported by the C compiler, else returns shell false.
427test-flag-CC() { test-flag-PROG "CC" c "$1"; }
186 428
187 for x in ${CFLAGS} 429# @FUNCTION: test-flag-CXX
188 do 430# @USAGE: <flag>
189 for y in ${ALLOWED_FLAGS} 431# @DESCRIPTION:
190 do 432# Returns shell true if <flag> is supported by the C++ compiler, else returns shell false.
191 flag=${x%%=*} 433test-flag-CXX() { test-flag-PROG "CXX" c++ "$1"; }
192 if [ "${flag%%${y}}" = "" ] 434
193 then 435# @FUNCTION: test-flag-F77
194 NEW_CFLAGS="${NEW_CFLAGS} ${x}" 436# @USAGE: <flag>
195 break 437# @DESCRIPTION:
196 fi 438# Returns shell true if <flag> is supported by the Fortran 77 compiler, else returns shell false.
439test-flag-F77() { test-flag-PROG "F77" f77 "$1"; }
440
441# @FUNCTION: test-flag-FC
442# @USAGE: <flag>
443# @DESCRIPTION:
444# Returns shell true if <flag> is supported by the Fortran 90 compiler, else returns shell false.
445test-flag-FC() { test-flag-PROG "FC" f95 "$1"; }
446
447test-flags-PROG() {
448 local comp=$1
449 local flags=()
450 local x
451
452 shift
453
454 [[ -z ${comp} ]] && return 1
455
456 for x ; do
457 test-flag-${comp} "${x}" && flags+=( "${x}" )
197 done 458 done
198 done
199 459
200 for x in ${CXXFLAGS} 460 echo "${flags[*]}"
201 do
202 for y in ${ALLOWED_FLAGS}
203 do
204 flag=${x%%=*}
205 if [ "${flag%%${y}}" = "" ]
206 then
207 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}"
208 break
209 fi
210 done
211 done
212 461
213 # In case we filtered out all optimization flags fallback to -O2 462 # Just bail if we dont have any flags
214 if [ "${CFLAGS/-O}" != "${CFLAGS}" -a "${NEW_CFLAGS/-O}" = "${NEW_CFLAGS}" ]; then 463 [[ ${#flags[@]} -gt 0 ]]
215 NEW_CFLAGS="${NEW_CFLAGS} -O2" 464}
465
466# @FUNCTION: test-flags-CC
467# @USAGE: <flags>
468# @DESCRIPTION:
469# Returns shell true if <flags> are supported by the C compiler, else returns shell false.
470test-flags-CC() { test-flags-PROG "CC" "$@"; }
471
472# @FUNCTION: test-flags-CXX
473# @USAGE: <flags>
474# @DESCRIPTION:
475# Returns shell true if <flags> are supported by the C++ compiler, else returns shell false.
476test-flags-CXX() { test-flags-PROG "CXX" "$@"; }
477
478# @FUNCTION: test-flags-F77
479# @USAGE: <flags>
480# @DESCRIPTION:
481# Returns shell true if <flags> are supported by the Fortran 77 compiler, else returns shell false.
482test-flags-F77() { test-flags-PROG "F77" "$@"; }
483
484# @FUNCTION: test-flags-FC
485# @USAGE: <flags>
486# @DESCRIPTION:
487# Returns shell true if <flags> are supported by the Fortran 90 compiler, else returns shell false.
488test-flags-FC() { test-flags-PROG "FC" "$@"; }
489
490# @FUNCTION: test-flags
491# @USAGE: <flags>
492# @DESCRIPTION:
493# Short-hand that should hopefully work for both C and C++ compiler, but
494# its really only present due to the append-flags() abomination.
495test-flags() { test-flags-CC "$@"; }
496
497# @FUNCTION: test_version_info
498# @USAGE: <version>
499# @DESCRIPTION:
500# Returns shell true if the current C compiler version matches <version>, else returns shell false.
501# Accepts shell globs.
502test_version_info() {
503 if [[ $($(tc-getCC) --version 2>&1) == *$1* ]]; then
504 return 0
505 else
506 return 1
216 fi 507 fi
217 if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then
218 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2"
219 fi
220
221 set +f
222
223 [ `use debug` ] \
224 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \
225 && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\""
226
227 export CFLAGS="${NEW_CFLAGS}"
228 export CXXFLAGS="${NEW_CXXFLAGS}"
229 return 0
230} 508}
231 509
232test_flag() { 510# @FUNCTION: strip-unsupported-flags
233 if gcc -S -xc "$@" -o /dev/null /dev/null >/dev/null 2>&1; then 511# @DESCRIPTION:
234 echo "$@" 512# Strip {C,CXX,F,FC}FLAGS of any flags not supported by the active toolchain.
235 return 0
236 fi
237 return 1
238}
239
240strip-unsupported-flags() { 513strip-unsupported-flags() {
241 for x in ${CFLAGS} ; do 514 export CFLAGS=$(test-flags-CC ${CFLAGS})
242 NEW_CFLAGS=${NEW_CFLAGS}" ""`test_flag ${x}`" 515 export CXXFLAGS=$(test-flags-CXX ${CXXFLAGS})
243 done 516 export FFLAGS=$(test-flags-F77 ${FFLAGS})
244 for x in ${CXXFLAGS} ; do 517 export FCFLAGS=$(test-flags-FC ${FCFLAGS})
245 NEW_CXXFLAGS=${NEW_CXXFLAGS}" ""`test_flag ${x}`"
246 done
247
248 export CFLAGS="${NEW_CFLAGS}"
249 export CXXFLAGS="${NEW_CXXFLAGS}"
250} 518}
251 519
520# @FUNCTION: get-flag
521# @USAGE: <flag>
522# @DESCRIPTION:
523# Find and echo the value for a particular flag. Accepts shell globs.
252get-flag() { 524get-flag() {
525 local f var findflag="$1"
526
253 # this code looks a little flaky but seems to work for 527 # this code looks a little flaky but seems to work for
254 # everything we want ... 528 # everything we want ...
255 # for example, if CFLAGS="-march=i686": 529 # for example, if CFLAGS="-march=i686":
256 # `get-flags -march` == "-march=i686" 530 # `get-flag -march` == "-march=i686"
257 # `get-flags march` == "i686" 531 # `get-flag march` == "i686"
258 local findflag="$1" 532 for var in $(all-flag-vars) ; do
259 for f in ${CFLAGS} ${CXXFLAGS} ; do 533 for f in ${!var} ; do
260 if [ "${f/${findflag}}" != "${f}" ] ; then 534 if [ "${f/${findflag}}" != "${f}" ] ; then
261 echo "${f/-${findflag}=}" 535 printf "%s\n" "${f/-${findflag}=}"
262 return 0 536 return 0
263 fi 537 fi
538 done
264 done 539 done
265 return 1 540 return 1
266} 541}
267 542
543# @FUNCTION: has_m64
544# @DESCRIPTION:
545# This doesn't test if the flag is accepted, it tests if the flag actually
546# WORKS. Non-multilib gcc will take both -m32 and -m64. If the flag works
547# return code is 0, else the return code is 1.
548has_m64() {
549 eqawarn "${FUNCNAME}: don't use this anymore"
550
551 # this doesnt test if the flag is accepted, it tests if the flag
552 # actually -WORKS-. non-multilib gcc will take both -m32 and -m64!
553 # please dont replace this function with test_flag in some future
554 # clean-up!
555
556 local temp="$(emktemp)"
557 echo "int main() { return(0); }" > "${temp}".c
558 MY_CC=$(tc-getCC)
559 ${MY_CC/ .*/} -m64 -o "$(emktemp)" "${temp}".c > /dev/null 2>&1
560 local ret=$?
561 rm -f "${temp}".c
562 [[ ${ret} != 1 ]] && return 0
563 return 1
564}
565
566has_m32() {
567 die "${FUNCNAME}: don't use this anymore"
568}
569
570# @FUNCTION: replace-sparc64-flags
571# @DESCRIPTION:
572# Sets mcpu to v8 and uses the original value as mtune if none specified.
268replace-sparc64-flags() { 573replace-sparc64-flags() {
269 local SPARC64_CPUS="ultrasparc v9" 574 local SPARC64_CPUS="ultrasparc3 ultrasparc v9"
270 575
271 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ] 576 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
272 then
273 for x in ${SPARC64_CPUS} 577 for x in ${SPARC64_CPUS}; do
274 do
275 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}" 578 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
276 done 579 done
277 else 580 else
278 for x in ${SPARC64_CPUS} 581 for x in ${SPARC64_CPUS}; do
279 do
280 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" 582 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
281 done 583 done
282 fi 584 fi
283 585
284 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ] 586 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
285 then
286 for x in ${SPARC64_CPUS} 587 for x in ${SPARC64_CPUS}; do
287 do
288 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}" 588 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
289 done 589 done
290 else 590 else
291 for x in ${SPARC64_CPUS} 591 for x in ${SPARC64_CPUS}; do
292 do
293 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" 592 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
294 done 593 done
295 fi 594 fi
296}
297 595
596 export CFLAGS CXXFLAGS
597}
598
599# @FUNCTION: append-libs
600# @USAGE: <libs>
601# @DESCRIPTION:
602# Add extra <libs> to the current LIBS.
298append-ldflags() { 603append-libs() {
299 LDFLAGS="${LDFLAGS} $@" 604 [[ $# -eq 0 ]] && return 0
300 return 0 605 local flag
301}
302
303filter-ldflags() {
304 # we do this fancy spacing stuff so as to not filter
305 # out part of a flag ... we want flag atoms ! :D
306 LDFLAGS=" ${LDFLAGS} "
307 for x in "$@" ; do 606 for flag in "$@"; do
308 LDFLAGS="${LDFLAGS// ${x} / }" 607 [[ ${flag} == -l* ]] && flag=${flag#-l}
608 export LIBS="${LIBS} -l${flag}"
309 done 609 done
310 LDFLAGS="${LDFLAGS:1:${#LDFLAGS}-2}"
311 return 0
312}
313 610
314etexec-flags() { 611 return 0
315 has_version sys-devel/hardened-gcc 612}
316 if [ $? == 0 ] ; then 613
317 if [ "`is-flag -yet_exec`" != "true" ]; then 614# @FUNCTION: raw-ldflags
318 debug-print ">>> appending flags -yet_exec" 615# @USAGE: [flags]
319 append-flags -yet_exec 616# @DESCRIPTION:
320 append-ldflags -yet_exec 617# Turn C style ldflags (-Wl,-foo) into straight ldflags - the results
321 fi 618# are suitable for passing directly to 'ld'; note LDFLAGS is usually passed
619# to gcc where it needs the '-Wl,'.
620#
621# If no flags are specified, then default to ${LDFLAGS}.
622raw-ldflags() {
623 local x input="$@"
624 [[ -z ${input} ]] && input=${LDFLAGS}
625 set --
626 for x in ${input} ; do
627 case ${x} in
628 -Wl,*)
629 x=${x#-Wl,}
630 set -- "$@" ${x//,/ }
631 ;;
632 *) # Assume it's a compiler driver flag, so throw it away #441808
633 ;;
634 esac
635 done
636 echo "$@"
637}
638
639# @FUNCTION: no-as-needed
640# @RETURN: Flag to disable asneeded behavior for use with append-ldflags.
641no-as-needed() {
642 case $($(tc-getLD) -v 2>&1 </dev/null) in
643 *GNU*) # GNU ld
644 echo "-Wl,--no-as-needed" ;;
645 esac
646}
647
322 fi 648fi
323}
324
325fstack-flags() {
326 has_version sys-devel/hardened-gcc
327 if [ $? == 0 ] ; then
328 if [ "`is-flag -yno_propolice`" != "true" ]; then
329 debug-print ">>> appending flags -yno_propolice"
330 append-flags -yno_propolice
331 append-ldflags -yno_propolice
332 fi
333 fi
334}

Legend:
Removed from v.1.41  
changed lines
  Added in v.1.191

  ViewVC Help
Powered by ViewVC 1.1.20