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

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.190

  ViewVC Help
Powered by ViewVC 1.1.20