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

Legend:
Removed from v.1.121  
changed lines
  Added in v.1.173

  ViewVC Help
Powered by ViewVC 1.1.20