/[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.63 Revision 1.79
1# Copyright 1999-2004 Gentoo Foundation 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.63 2004/07/18 04:59:30 vapier Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.79 2004/12/25 18:45:56 chriswhite 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 9
10IUSE="debug" 10IUSE="debug"
11
12# need access to emktemp()
13inherit eutils toolchain-funcs
11 14
12# 15#
13#### filter-flags <flags> #### 16#### filter-flags <flags> ####
14# Remove particular flags from C[XX]FLAGS 17# Remove particular flags from C[XX]FLAGS
15# Matches only complete flags 18# Matches only complete flags
18# Add extra flags to your current C[XX]FLAGS 21# Add extra flags to your current C[XX]FLAGS
19# 22#
20#### replace-flags <orig.flag> <new.flag> ### 23#### replace-flags <orig.flag> <new.flag> ###
21# Replace a flag by another one 24# Replace a flag by another one
22# 25#
23#### replace-cpu-flags <new.cpu> <old.cpus> ### 26#### replace-cpu-flags <old.cpus> <new.cpu> ###
24# Replace march/mcpu flags that specify <old.cpus> 27# Replace march/mcpu flags that specify <old.cpus>
25# with flags that specify <new.cpu> 28# with flags that specify <new.cpu>
26# 29#
27#### is-flag <flag> #### 30#### is-flag <flag> ####
28# Returns "true" if flag is set in C[XX]FLAGS 31# Returns "true" if flag is set in C[XX]FLAGS
69# 72#
70 73
71# C[XX]FLAGS that we allow in strip-flags 74# C[XX]FLAGS that we allow in strip-flags
72setup-allowed-flags() { 75setup-allowed-flags() {
73 if [ -z "${ALLOWED_FLAGS}" ] ; then 76 if [ -z "${ALLOWED_FLAGS}" ] ; then
74 export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -fno-unit-at-a-time -pipe -g" 77 export ALLOWED_FLAGS="-pipe"
78 export ALLOWED_FLAGS="${ALLOWED_FLAGS} -O -O0 -O1 -O2 -mcpu -march -mtune"
79 export ALLOWED_FLAGS="${ALLOWED_FLAGS} -fstack-protector -fno-stack-protector"
80 export ALLOWED_FLAGS="${ALLOWED_FLAGS} -fno-pie -fno-unit-at-a-time"
81 export ALLOWED_FLAGS="${ALLOWED_FLAGS} -g -g0 -g1 -g2 -g3 -ggdb -ggdb0 -ggdb1 -ggdb2 -ggdb3"
75 case "${ARCH}" in 82 case "${ARCH}" in
76 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; 83 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mips32 -mips64 -mips16 -EL -EB -mabi" ;;
77 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC -m64" ;; 84 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC -m64" ;;
78 x86) ALLOWED_FLAGS="${ALLOWED_FLAGS} -m32" ;; 85 x86) ALLOWED_FLAGS="${ALLOWED_FLAGS} -m32" ;;
79 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 86 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
80 ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 87 ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
88 sparc) ALLOWED_FLAGS="${ALLOWED_FLAGS} -m32 -m64" ;;
89 ppc) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mabi" ;;
81 esac 90 esac
82 fi 91 fi
92 # allow a bunch of flags that negate features / control ABI
93 ALLOWED_FLAGS="${ALLOWED_FLAGS} -fno-stack-protector -fno-stack-protector-all"
94 case "${ARCH}" in
95 x86|amd64|ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow" ;;
96 esac
97 ALLOWED_FLAGS="${ALLOWED_FLAGS} -mregparm -mno-app-regs -mapp-regs -msoft-float -mflat -mno-faster-structs -mfaster-structs -mlittle-endian -mbig-endian -mlive-g0 -mcmodel -mno-stack-bias -mstack-bias"
83 98
84 # C[XX]FLAGS that we are think is ok, but needs testing 99 # C[XX]FLAGS that we are think is ok, but needs testing
85 # NOTE: currently -Os have issues with gcc3 and K6* arch's 100 # NOTE: currently -Os have issues with gcc3 and K6* arch's
86 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" 101 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays"
87 return 0 102 return 0
97 -fstack-protector|-fstack-protector-all) fstack-flags;; 112 -fstack-protector|-fstack-protector-all) fstack-flags;;
98 esac 113 esac
99 done 114 done
100 115
101 for fset in CFLAGS CXXFLAGS; do 116 for fset in CFLAGS CXXFLAGS; do
117 # Looping over the flags instead of using a global
118 # substitution ensures that we're working with flag atoms.
119 # Otherwise globs like -O* have the potential to wipe out the
120 # list of flags.
102 for f in ${!fset}; do 121 for f in ${!fset}; do
103 for x in "$@"; do 122 for x in "$@"; do
104 # Note this should work with globs like -O* 123 # Note this should work with globs like -O*
105 [[ ${f} == ${x} ]] && continue 2 124 [[ ${f} == ${x} ]] && continue 2
106 done 125 done
127 -n "`is-flag -fno-stack-protector-all`" ] && fstack-flags 146 -n "`is-flag -fno-stack-protector-all`" ] && fstack-flags
128 return 0 147 return 0
129} 148}
130 149
131replace-flags() { 150replace-flags() {
132 # we do this fancy spacing stuff so as to not filter 151 local f fset
133 # out part of a flag ... we want flag atoms ! :D 152 declare -a new_CFLAGS new_CXXFLAGS
134 CFLAGS=" ${CFLAGS} " 153
135 CXXFLAGS=" ${CXXFLAGS} " 154 for fset in CFLAGS CXXFLAGS; do
136 CFLAGS="${CFLAGS// ${1} / ${2} }" 155 # Looping over the flags instead of using a global
137 CXXFLAGS="${CXXFLAGS// ${1} / ${2} }" 156 # substitution ensures that we're working with flag atoms.
138 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" 157 # Otherwise globs like -O* have the potential to wipe out the
139 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" 158 # list of flags.
140 export CFLAGS CXXFLAGS 159 for f in ${!fset}; do
160 # Note this should work with globs like -O*
161 [[ ${f} == ${1} ]] && f=${2}
162 eval new_${fset}\[\${\#new_${fset}\[@]}]=\${f}
163 done
164 eval export ${fset}=\${new_${fset}\[*]}
165 done
166
141 return 0 167 return 0
142} 168}
143 169
144replace-cpu-flags() { 170replace-cpu-flags() {
145 local oldcpu newcpu="$1" ; shift 171 local newcpu="$#" ; newcpu="${!newcpu}"
146 for oldcpu in "$@" ; do 172 while [ $# -gt 1 ] ; do
173 # quote to make sure that no globbing is done (particularly on
174 # ${oldcpu} prior to calling replace-flags
147 replace-flags -march=${oldcpu} -march=${newcpu} 175 replace-flags "-march=${1}" "-march=${newcpu}"
148 replace-flags -mcpu=${oldcpu} -mcpu=${newcpu} 176 replace-flags "-mcpu=${1}" "-mcpu=${newcpu}"
149 replace-flags -mtune=${oldcpu} -mtune=${newcpu} 177 replace-flags "-mtune=${1}" "-mtune=${newcpu}"
178 shift
150 done 179 done
151 return 0 180 return 0
152} 181}
153 182
154is-flag() { 183is-flag() {
155 local x 184 local x
156 185
157 for x in ${CFLAGS} ${CXXFLAGS} ; do 186 for x in ${CFLAGS} ${CXXFLAGS} ; do
187 # Note this should work with globs like -mcpu=ultrasparc*
158 if [ "${x}" == "$1" ] ; then 188 if [[ ${x} == ${1} ]]; then
159 echo true 189 echo true
160 return 0 190 return 0
161 fi 191 fi
162 done 192 done
163 return 1 193 return 1
206 set -f # disable pathname expansion 236 set -f # disable pathname expansion
207 237
208 for x in ${CFLAGS}; do 238 for x in ${CFLAGS}; do
209 for y in ${ALLOWED_FLAGS}; do 239 for y in ${ALLOWED_FLAGS}; do
210 flag=${x%%=*} 240 flag=${x%%=*}
211 if [ "${flag%%${y}}" = "" ] || [ "${flag:0:5}" = "-fno-" ] || [ "${flag:0:5}" = "-mno-" ] ; then 241 if [ "${flag%%${y}}" = "" ] ; then
212 NEW_CFLAGS="${NEW_CFLAGS} ${x}" 242 NEW_CFLAGS="${NEW_CFLAGS} ${x}"
213 break 243 break
214 fi 244 fi
215 done 245 done
216 done 246 done
217 247
218 for x in ${CXXFLAGS}; do 248 for x in ${CXXFLAGS}; do
219 for y in ${ALLOWED_FLAGS}; do 249 for y in ${ALLOWED_FLAGS}; do
220 flag=${x%%=*} 250 flag=${x%%=*}
221 if [ "${flag%%${y}}" = "" ] || [ "${flag:0:5}" = "-fno-" ] || [ "${flag:0:5}" = "-mno-" ] ; then 251 if [ "${flag%%${y}}" = "" ] ; then
222 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}" 252 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}"
223 break 253 break
224 fi 254 fi
225 done 255 done
226 done 256 done
239 export CXXFLAGS="${NEW_CXXFLAGS}" 269 export CXXFLAGS="${NEW_CXXFLAGS}"
240 return 0 270 return 0
241} 271}
242 272
243test_flag() { 273test_flag() {
244 local cc=${CC:-gcc} ; cc=${cc%% *} 274 if $(tc-getCC) -S -xc "$@" -o "$(emktemp)" /dev/null &>/dev/null; then
245 if ${cc} -S -xc "$@" -o /dev/null /dev/null &>/dev/null; then
246 printf "%s\n" "$*" 275 printf "%s\n" "$*"
247 return 0 276 return 0
248 fi 277 fi
249 return 1 278 return 1
250} 279}
251 280
252test_version_info() { 281test_version_info() {
253 local cc=${CC:-gcc} ; cc=${cc%% *}
254 if [[ $(${cc} --version 2>&1) == *$1* ]]; then 282 if [[ $($(tc-getCC) --version 2>&1) == *$1* ]]; then
255 return 0 283 return 0
256 else 284 else
257 return 1 285 return 1
258 fi 286 fi
259} 287}
288 done 316 done
289 return 1 317 return 1
290} 318}
291 319
292has_hardened() { 320has_hardened() {
293 test_version_info Hardened 321 test_version_info Hardened && return 0
322 # the specs file wont exist unless gcc has GCC_SPECS support
323 [ -f "${GCC_SPECS}" -a "${GCC_SPECS}" != "${GCC_SPECS/hardened/}" ] && \
324 return 0
294 return $? 325 return 1
295} 326}
296 327
297has_pic() { 328has_pic() {
298 [ "${CFLAGS/-fPIC}" != "${CFLAGS}" ] && return 0 329 [ "${CFLAGS/-fPIC}" != "${CFLAGS}" ] && return 0
299 [ "${CFLAGS/-fpic}" != "${CFLAGS}" ] && return 0 330 [ "${CFLAGS/-fpic}" != "${CFLAGS}" ] && return 0
317has_m64() { 348has_m64() {
318 # this doesnt test if the flag is accepted, it tests if the flag 349 # this doesnt test if the flag is accepted, it tests if the flag
319 # actually -WORKS-. non-multilib gcc will take both -m32 and -m64! 350 # actually -WORKS-. non-multilib gcc will take both -m32 and -m64!
320 # please dont replace this function with test_flag in some future 351 # please dont replace this function with test_flag in some future
321 # clean-up! 352 # clean-up!
322 temp=`mktemp` 353 local temp="$(emktemp)"
323 echo "int main() { return(0); }" > ${temp}.c 354 echo "int main() { return(0); }" > ${temp}.c
324 ${CC/ .*/} -m64 -o /dev/null ${temp}.c > /dev/null 2>&1 355 MY_CC=$(tc-getCC)
325 ret=$? 356 ${MY_CC/ .*/} -m64 -o "$(emktemp)" ${temp}.c > /dev/null 2>&1
357 local ret=$?
326 rm -f ${temp}.c 358 rm -f ${temp}.c
327 [ "$ret" != "1" ] && return 0 359 [ "$ret" != "1" ] && return 0
328 return 1 360 return 1
329} 361}
330 362
331has_m32() { 363has_m32() {
332 # this doesnt test if the flag is accepted, it tests if the flag 364 # this doesnt test if the flag is accepted, it tests if the flag
333 # actually -WORKS-. non-multilib gcc will take both -m32 and -m64! 365 # actually -WORKS-. non-multilib gcc will take both -m32 and -m64!
334 # please dont replace this function with test_flag in some future 366 # please dont replace this function with test_flag in some future
335 # clean-up! 367 # clean-up!
336 temp=`mktemp` 368 local temp="$(emktemp)"
337 echo "int main() { return(0); }" > ${temp}.c 369 echo "int main() { return(0); }" > ${temp}.c
338 ${CC/ .*/} -m32 -o /dev/null ${temp}.c > /dev/null 2>&1 370 MY_CC=$(tc-getCC)
339 ret=$? 371 ${MY_CC/ .*/} -m32 -o "$(emktemp)" ${temp}.c > /dev/null 2>&1
372 local ret=$?
340 rm -f ${temp}.c 373 rm -f ${temp}.c
341 [ "$ret" != "1" ] && return 0 374 [ "$ret" != "1" ] && return 0
342 return 1 375 return 1
343} 376}
344 377
389 422
390etexec-flags() { 423etexec-flags() {
391 # if you're not using a hardened compiler you wont need this 424 # if you're not using a hardened compiler you wont need this
392 # PIC/no-pic kludge in the first place. 425 # PIC/no-pic kludge in the first place.
393 has_hardened || return 0 426 has_hardened || return 0
427 use amd64 && return 0
428 use mips && return 0
394 429
395 if has_pie || has_pic; then 430 if has_pie || has_pic; then
396 [ -z "`is-flag -fno-pic`" ] && 431 [ -z "`is-flag -fno-pic`" ] &&
397 export CFLAGS="${CFLAGS} `test_flag -fno-pic`" 432 export CFLAGS="${CFLAGS} `test_flag -fno-pic`"
398 [ -z "`is-flag -nopie`" ] && 433 [ -z "`is-flag -nopie`" ] &&
406 [ -z "`is-flag -fno-stack-protector`" ] && 441 [ -z "`is-flag -fno-stack-protector`" ] &&
407 export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`" 442 export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`"
408 fi 443 fi
409 return 0 444 return 0
410} 445}
446
447# This is thanks to great work from Paul de Vrieze <gentoo-user@devrieze.net>,
448# bug #9016. Also thanks to Jukka Salmi <salmi@gmx.net> (bug #13907) for more
449# fixes.
450#
451# Export CFLAGS and CXXFLAGS that are compadible with gcc-2.95.3
452gcc2-flags() {
453 CFLAGS=${CFLAGS//pentium-mmx/i586}
454 CFLAGS=${CFLAGS//pentium[234]/i686}
455 CFLAGS=${CFLAGS//k6-[23]/k6}
456 CFLAGS=${CFLAGS//athlon-tbird/i686}
457 CFLAGS=${CFLAGS//athlon-4/i686}
458 CFLAGS=${CFLAGS//athlon-[xm]p/i686}
459 CFLAGS=${CFLAGS//athlon/i686}
460
461 CXXFLAGS=${CXXFLAGS//pentium-mmx/i586}
462 CXXFLAGS=${CXXFLAGS//pentium[234]/i686}
463 CXXFLAGS=${CXXFLAGS//k6-[23]/k6}
464 CXXFLAGS=${CXXFLAGS//athlon-tbird/i686}
465 CXXFLAGS=${CXXFLAGS//athlon-4/i686}
466 CXXFLAGS=${CXXFLAGS//athlon-[xm]p/i686}
467 CXXFLAGS=${CXXFLAGS//athlon/i686}
468
469 if [ "$ARCH" = alpha ]; then
470 CHOST=${CHOST/#alphaev6[78]/alphaev6}
471 CFLAGS=${CFLAGS//ev6[78]/ev6}
472 CXXFLAGS=${CXXFLAGS//ev6[78]/ev6}
473 fi
474
475 export CFLAGS CXXFLAGS
476}

Legend:
Removed from v.1.63  
changed lines
  Added in v.1.79

  ViewVC Help
Powered by ViewVC 1.1.20