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

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

  ViewVC Help
Powered by ViewVC 1.1.20