/[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.24 Revision 1.64
1# Copyright 1999-2003 Gentoo Technologies, Inc. 1# Copyright 1999-2004 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.24 2003/07/22 12:48:11 aliz Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.64 2004/07/22 15:29:10 agriffis Exp $
4# 4#
5# Author Bart Verwilst <verwilst@gentoo.org> 5# Author Bart Verwilst <verwilst@gentoo.org>
6 6
7ECLASS=flag-o-matic 7ECLASS=flag-o-matic
8INHERITED="$INHERITED $ECLASS" 8INHERITED="$INHERITED $ECLASS"
9
10# Please leave ${IUSE} in this until portage .51 is stable, otherwise
11# IUSE gets clobbered.
12IUSE="${IUSE} debug"
9 13
10# 14#
11#### filter-flags <flags> #### 15#### filter-flags <flags> ####
12# Remove particular flags from C[XX]FLAGS 16# Remove particular flags from C[XX]FLAGS
13# Matches only complete flags 17# Matches only complete flags
16# Add extra flags to your current C[XX]FLAGS 20# Add extra flags to your current C[XX]FLAGS
17# 21#
18#### replace-flags <orig.flag> <new.flag> ### 22#### replace-flags <orig.flag> <new.flag> ###
19# Replace a flag by another one 23# Replace a flag by another one
20# 24#
25#### replace-cpu-flags <new.cpu> <old.cpus> ###
26# Replace march/mcpu flags that specify <old.cpus>
27# with flags that specify <new.cpu>
28#
21#### is-flag <flag> #### 29#### is-flag <flag> ####
22# Returns "true" if flag is set in C[XX]FLAGS 30# Returns "true" if flag is set in C[XX]FLAGS
23# Matches only complete a flag 31# Matches only complete a flag
24# 32#
25#### strip-flags #### 33#### strip-flags ####
26# Strip C[XX]FLAGS of everything except known 34# Strip C[XX]FLAGS of everything except known
27# good options. 35# good options.
28# 36#
37#### strip-unsupported-flags ####
38# Strip C[XX]FLAGS of any flags not supported by
39# installed version of gcc
40#
29#### get-flag <flag> #### 41#### get-flag <flag> ####
30# Find and echo the value for a particular flag 42# Find and echo the value for a particular flag
31# 43#
32#### replace-sparc64-flags #### 44#### replace-sparc64-flags ####
33# Sets mcpu to v8 and uses the original value 45# Sets mcpu to v8 and uses the original value
34# as mtune if none specified. 46# as mtune if none specified.
35# 47#
36 48#### filter-mfpmath <math types> ####
49# Remove specified math types from the fpmath specification
50# If the user has -mfpmath=sse,386, running `filter-mfpmath sse`
51# will leave the user with -mfpmath=386
52#
53#### append-ldflags ####
54# Add extra flags to your current LDFLAGS
55#
56#### filter-ldflags <flags> ####
57# Remove particular flags from LDFLAGS
58# Matches only complete flags
59#
60#### etexec-flags ####
61# hooked function for hardened gcc that appends
62# -fno-pic to {C,CXX,LD}FLAGS
63# when a package is filtering -fpic, -fPIC, -fpie, -fPIE
64#
65#### fstack-flags ####
66# hooked function for hardened gcc that appends
67# -fno-stack-protector to {C,CXX,LD}FLAGS
68# when a package is filtering -fstack-protector, -fstack-protector-all
69# notice: modern automatic specs files will also suppress -fstack-protector-all
70# when only -fno-stack-protector is given
71#
37 72
38# C[XX]FLAGS that we allow in strip-flags 73# C[XX]FLAGS that we allow in strip-flags
74setup-allowed-flags() {
75 if [ -z "${ALLOWED_FLAGS}" ] ; then
39ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -pipe -g" 76 export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -fno-unit-at-a-time -pipe -g"
40case "${ARCH}" in 77 case "${ARCH}" in
41 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; 78 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;;
42 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 79 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC -m64" ;;
80 x86) ALLOWED_FLAGS="${ALLOWED_FLAGS} -m32" ;;
81 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
82 ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
43esac 83 esac
84 fi
44 85
45# C[XX]FLAGS that we are think is ok, but needs testing 86 # C[XX]FLAGS that we are think is ok, but needs testing
46# NOTE: currently -Os have issues with gcc3 and K6* arch's 87 # NOTE: currently -Os have issues with gcc3 and K6* arch's
47UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" 88 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays"
48 89 return 0
49filter-mfpmath() {
50 for a in $CFLAGS; do
51 if [ "${a:0:8}" == "-mfpmath" ]; then
52 orig_mfpmath=$a
53 fi
54 done
55
56 mfpmath="$( echo $orig_mfpmath | awk -F '=' '{print $2}' | tr "," " " )"
57 for b in $@; do
58 mfpmath="${mfpmath/$b}"
59 done
60
61 if [ -z "${mfpmath/ }" ]; then
62 filter-flags "$orig_mfpmath"
63 else
64 new_mfpmath="-mfpmath=$( echo $mfpmath | sed -e "s/ /,/g" -e "s/,,/,/g" )"
65 replace-flags "$orig_mfpmath" "$new_mfpmath"
66 fi
67} 90}
68 91
69filter-flags() { 92filter-flags() {
70 # we do this fancy spacing stuff so as to not filter 93 local x f fset
71 # out part of a flag ... we want flag atoms ! :D 94 declare -a new_CFLAGS new_CXXFLAGS
72 export CFLAGS=" ${CFLAGS} " 95
73 export CXXFLAGS=" ${CXXFLAGS} "
74 for x in $@ ; do 96 for x in "$@" ; do
75 export CFLAGS="${CFLAGS/ ${x} / }" 97 case "${x}" in
76 export CXXFLAGS="${CXXFLAGS/ ${x} / }" 98 -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;;
99 -fstack-protector|-fstack-protector-all) fstack-flags;;
100 esac
101 done
102
103 for fset in CFLAGS CXXFLAGS; do
104 # Looping over the flags instead of using a global
105 # substitution ensures that we're working with flag atoms.
106 # Otherwise globs like -O* have the potential to wipe out the
107 # list of flags.
108 for f in ${!fset}; do
109 for x in "$@"; do
110 # Note this should work with globs like -O*
111 [[ ${f} == ${x} ]] && continue 2
112 done
113 eval new_${fset}\[\${\#new_${fset}\[@]}]=\${f}
77 done 114 done
78 export CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" 115 eval export ${fset}=\${new_${fset}\[*]}
79 export CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" 116 done
117
118 return 0
119}
120
121filter-lfs-flags() {
122 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
123}
124
125append-lfs-flags() {
126 append-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
80} 127}
81 128
82append-flags() { 129append-flags() {
83 CFLAGS="${CFLAGS} $@" 130 export CFLAGS="${CFLAGS} $*"
84 CXXFLAGS="${CXXFLAGS} $@" 131 export CXXFLAGS="${CXXFLAGS} $*"
132 [ -n "`is-flag -fno-stack-protector`" -o \
133 -n "`is-flag -fno-stack-protector-all`" ] && fstack-flags
134 return 0
85} 135}
86 136
87replace-flags() { 137replace-flags() {
88 CFLAGS="${CFLAGS/${1}/${2} }" 138 local f fset
89 CXXFLAGS="${CXXFLAGS/${1}/${2} }" 139 declare -a new_CFLAGS new_CXXFLAGS
140
141 for fset in CFLAGS CXXFLAGS; do
142 # Looping over the flags instead of using a global
143 # substitution ensures that we're working with flag atoms.
144 # Otherwise globs like -O* have the potential to wipe out the
145 # list of flags.
146 for f in ${!fset}; do
147 # Note this should work with globs like -O*
148 [[ ${f} == ${1} ]] && f=${2}
149 eval new_${fset}\[\${\#new_${fset}\[@]}]=\${f}
150 done
151 eval export ${fset}=\${new_${fset}\[*]}
152 done
153
154 return 0
155}
156
157replace-cpu-flags() {
158 local oldcpu newcpu="$1" ; shift
159 for oldcpu in "$@" ; do
160 # quote to make sure that no globbing is done (particularly on
161 # ${oldcpu} prior to calling replace-flags
162 replace-flags "-march=${oldcpu}" "-march=${newcpu}"
163 replace-flags "-mcpu=${oldcpu}" "-mcpu=${newcpu}"
164 replace-flags "-mtune=${oldcpu}" "-mtune=${newcpu}"
165 done
166 return 0
90} 167}
91 168
92is-flag() { 169is-flag() {
170 local x
171
93 for x in ${CFLAGS} ${CXXFLAGS} ; do 172 for x in ${CFLAGS} ${CXXFLAGS} ; do
94 if [ "${x}" == "$1" ] ; then 173 if [ "${x}" == "$1" ] ; then
95 echo true 174 echo true
96 return 0 175 return 0
97 fi 176 fi
98 done 177 done
99 return 1 178 return 1
100} 179}
101 180
181filter-mfpmath() {
182 local orig_mfpmath new_math prune_math
183
184 # save the original -mfpmath flag
185 orig_mfpmath="`get-flag -mfpmath`"
186 # get the value of the current -mfpmath flag
187 new_math=" `get-flag mfpmath | tr , ' '` "
188 # figure out which math values are to be removed
189 prune_math=""
190 for prune_math in "$@" ; do
191 new_math="${new_math/ ${prune_math} / }"
192 done
193 new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`"
194
195 if [ -z "${new_math}" ] ; then
196 # if we're removing all user specified math values are
197 # slated for removal, then we just filter the flag
198 filter-flags ${orig_mfpmath}
199 else
200 # if we only want to filter some of the user specified
201 # math values, then we replace the current flag
202 replace-flags ${orig_mfpmath} -mfpmath=${new_math}
203 fi
204 return 0
205}
206
102strip-flags() { 207strip-flags() {
208 local x y flag NEW_CFLAGS NEW_CXXFLAGS
209
210 setup-allowed-flags
211
103 local NEW_CFLAGS="" 212 local NEW_CFLAGS=""
104 local NEW_CXXFLAGS="" 213 local NEW_CXXFLAGS=""
105 214
106 # Allow unstable C[XX]FLAGS if we are using unstable profile ... 215 # Allow unstable C[XX]FLAGS if we are using unstable profile ...
107 if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then 216 if has ~${ARCH} ${ACCEPT_KEYWORDS} ; then
108 [ `use debug` ] && einfo "Enabling the use of some unstable flags" 217 use debug && einfo "Enabling the use of some unstable flags"
109 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}" 218 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}"
110 fi 219 fi
111 220
112 set -f 221 set -f # disable pathname expansion
113 222
114 for x in ${CFLAGS} 223 for x in ${CFLAGS}; do
115 do
116 for y in ${ALLOWED_FLAGS} 224 for y in ${ALLOWED_FLAGS}; do
117 do
118 flag=${x%%=*} 225 flag=${x%%=*}
119 if [ "${flag%%${y}}" = "" ] 226 if [ "${flag%%${y}}" = "" ] || [ "${flag:0:5}" = "-fno-" ] || [ "${flag:0:5}" = "-mno-" ] ; then
120 then
121 NEW_CFLAGS="${NEW_CFLAGS} ${x}" 227 NEW_CFLAGS="${NEW_CFLAGS} ${x}"
122 break 228 break
123 fi 229 fi
124 done 230 done
125 done 231 done
126 232
127 for x in ${CXXFLAGS} 233 for x in ${CXXFLAGS}; do
128 do
129 for y in ${ALLOWED_FLAGS} 234 for y in ${ALLOWED_FLAGS}; do
130 do
131 flag=${x%%=*} 235 flag=${x%%=*}
132 if [ "${flag%%${y}}" = "" ] 236 if [ "${flag%%${y}}" = "" ] || [ "${flag:0:5}" = "-fno-" ] || [ "${flag:0:5}" = "-mno-" ] ; then
133 then
134 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}" 237 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}"
135 break 238 break
136 fi 239 fi
137 done 240 done
138 done 241 done
139 242
140 set +f 243 # In case we filtered out all optimization flags fallback to -O2
244 if [ "${CFLAGS/-O}" != "${CFLAGS}" -a "${NEW_CFLAGS/-O}" = "${NEW_CFLAGS}" ]; then
245 NEW_CFLAGS="${NEW_CFLAGS} -O2"
246 fi
247 if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then
248 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2"
249 fi
141 250
142 [ `use debug` ] \ 251 set +f # re-enable pathname expansion
143 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \
144 && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\""
145 252
146 export CFLAGS="${NEW_CFLAGS}" 253 export CFLAGS="${NEW_CFLAGS}"
147 export CXXFLAGS="${NEW_CXXFLAGS}" 254 export CXXFLAGS="${NEW_CXXFLAGS}"
255 return 0
256}
257
258test_flag() {
259 local cc=${CC:-gcc} ; cc=${cc%% *}
260 if ${cc} -S -xc "$@" -o /dev/null /dev/null &>/dev/null; then
261 printf "%s\n" "$*"
262 return 0
263 fi
264 return 1
265}
266
267test_version_info() {
268 local cc=${CC:-gcc} ; cc=${cc%% *}
269 if [[ $(${cc} --version 2>&1) == *$1* ]]; then
270 return 0
271 else
272 return 1
273 fi
274}
275
276strip-unsupported-flags() {
277 local NEW_CFLAGS NEW_CXXFLAGS
278
279 for x in ${CFLAGS} ; do
280 NEW_CFLAGS="${NEW_CFLAGS} `test_flag ${x}`"
281 done
282 for x in ${CXXFLAGS} ; do
283 NEW_CXXFLAGS="${NEW_CXXFLAGS} `test_flag ${x}`"
284 done
285
286 export CFLAGS="${NEW_CFLAGS}"
287 export CXXFLAGS="${NEW_CXXFLAGS}"
148} 288}
149 289
150get-flag() { 290get-flag() {
151 local findflag="$1" 291 local f findflag="$1"
152 292
293 # this code looks a little flaky but seems to work for
294 # everything we want ...
295 # for example, if CFLAGS="-march=i686":
296 # `get-flag -march` == "-march=i686"
297 # `get-flag march` == "i686"
153 for f in ${CFLAGS} ${CXXFLAGS} ; do 298 for f in ${CFLAGS} ${CXXFLAGS} ; do
154 if [ "${f/${findflag}}" != "${f}" ] ; then 299 if [ "${f/${findflag}}" != "${f}" ] ; then
155 echo "${f/-${findflag}=}" 300 printf "%s\n" "${f/-${findflag}=}"
156 return 301 return 0
157 fi 302 fi
158 done 303 done
304 return 1
305}
306
307has_hardened() {
308 test_version_info Hardened
309 return $?
310}
311
312has_pic() {
313 [ "${CFLAGS/-fPIC}" != "${CFLAGS}" ] && return 0
314 [ "${CFLAGS/-fpic}" != "${CFLAGS}" ] && return 0
315 test_version_info pie && return 0
316 return 1
317}
318
319has_pie() {
320 [ "${CFLAGS/-fPIE}" != "${CFLAGS}" ] && return 0
321 [ "${CFLAGS/-fpie}" != "${CFLAGS}" ] && return 0
322 test_version_info pie && return 0
323 return 1
324}
325
326has_ssp() {
327 [ "${CFLAGS/-fstack-protector}" != "${CFLAGS}" ] && return 0
328 test_version_info ssp && return 0
329 return 1
330}
331
332has_m64() {
333 # this doesnt test if the flag is accepted, it tests if the flag
334 # actually -WORKS-. non-multilib gcc will take both -m32 and -m64!
335 # please dont replace this function with test_flag in some future
336 # clean-up!
337 temp=`mktemp`
338 echo "int main() { return(0); }" > ${temp}.c
339 ${CC/ .*/} -m64 -o /dev/null ${temp}.c > /dev/null 2>&1
340 ret=$?
341 rm -f ${temp}.c
342 [ "$ret" != "1" ] && return 0
343 return 1
344}
345
346has_m32() {
347 # this doesnt test if the flag is accepted, it tests if the flag
348 # actually -WORKS-. non-multilib gcc will take both -m32 and -m64!
349 # please dont replace this function with test_flag in some future
350 # clean-up!
351 temp=`mktemp`
352 echo "int main() { return(0); }" > ${temp}.c
353 ${CC/ .*/} -m32 -o /dev/null ${temp}.c > /dev/null 2>&1
354 ret=$?
355 rm -f ${temp}.c
356 [ "$ret" != "1" ] && return 0
357 return 1
159} 358}
160 359
161replace-sparc64-flags() { 360replace-sparc64-flags() {
162 local SPARC64_CPUS="ultrasparc v9" 361 local SPARC64_CPUS="ultrasparc v9"
163 362
164 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ] 363 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
165 then
166 for x in ${SPARC64_CPUS} 364 for x in ${SPARC64_CPUS}; do
167 do
168 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}" 365 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
169 done 366 done
170 else 367 else
171 for x in ${SPARC64_CPUS} 368 for x in ${SPARC64_CPUS}; do
172 do
173 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" 369 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
174 done 370 done
175 fi 371 fi
176 372
177 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ] 373 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
178 then
179 for x in ${SPARC64_CPUS} 374 for x in ${SPARC64_CPUS}; do
180 do
181 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}" 375 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
182 done 376 done
183 else 377 else
184 for x in ${SPARC64_CPUS} 378 for x in ${SPARC64_CPUS}; do
185 do
186 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" 379 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
187 done 380 done
188 fi 381 fi
382
383 export CFLAGS CXXFLAGS
189} 384}
385
386append-ldflags() {
387 export LDFLAGS="${LDFLAGS} $*"
388 return 0
389}
390
391filter-ldflags() {
392 local x
393
394 # we do this fancy spacing stuff so as to not filter
395 # out part of a flag ... we want flag atoms ! :D
396 LDFLAGS=" ${LDFLAGS} "
397 for x in "$@" ; do
398 LDFLAGS="${LDFLAGS// ${x} / }"
399 done
400 LDFLAGS="${LDFLAGS:1:${#LDFLAGS}-2}"
401 export LDFLAGS
402 return 0
403}
404
405etexec-flags() {
406 # if you're not using a hardened compiler you wont need this
407 # PIC/no-pic kludge in the first place.
408 has_hardened || return 0
409
410 if has_pie || has_pic; then
411 [ -z "`is-flag -fno-pic`" ] &&
412 export CFLAGS="${CFLAGS} `test_flag -fno-pic`"
413 [ -z "`is-flag -nopie`" ] &&
414 export CFLAGS="${CFLAGS} `test_flag -nopie`"
415 fi
416 return 0
417}
418
419fstack-flags() {
420 if has_ssp; then
421 [ -z "`is-flag -fno-stack-protector`" ] &&
422 export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`"
423 fi
424 return 0
425}

Legend:
Removed from v.1.24  
changed lines
  Added in v.1.64

  ViewVC Help
Powered by ViewVC 1.1.20