/[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.58 Revision 1.73
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.58 2004/06/10 15:28:56 agriffis 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
10# Please leave ${IUSE} in this until portage .51 is stable, otherwise
11# IUSE gets clobbered.
9IUSE="$IUSE debug" 12IUSE="${IUSE} debug"
13
14# need access to emktemp()
15inherit eutils
10 16
11# 17#
12#### filter-flags <flags> #### 18#### filter-flags <flags> ####
13# Remove particular flags from C[XX]FLAGS 19# Remove particular flags from C[XX]FLAGS
14# Matches only complete flags 20# Matches only complete flags
17# Add extra flags to your current C[XX]FLAGS 23# Add extra flags to your current C[XX]FLAGS
18# 24#
19#### replace-flags <orig.flag> <new.flag> ### 25#### replace-flags <orig.flag> <new.flag> ###
20# Replace a flag by another one 26# Replace a flag by another one
21# 27#
22#### replace-cpu-flags <new.cpu> <old.cpus> ### 28#### replace-cpu-flags <old.cpus> <new.cpu> ###
23# Replace march/mcpu flags that specify <old.cpus> 29# Replace march/mcpu flags that specify <old.cpus>
24# with flags that specify <new.cpu> 30# with flags that specify <new.cpu>
25# 31#
26#### is-flag <flag> #### 32#### is-flag <flag> ####
27# Returns "true" if flag is set in C[XX]FLAGS 33# Returns "true" if flag is set in C[XX]FLAGS
68# 74#
69 75
70# C[XX]FLAGS that we allow in strip-flags 76# C[XX]FLAGS that we allow in strip-flags
71setup-allowed-flags() { 77setup-allowed-flags() {
72 if [ -z "${ALLOWED_FLAGS}" ] ; then 78 if [ -z "${ALLOWED_FLAGS}" ] ; then
73 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"
74 case "${ARCH}" in 80 case "${ARCH}" in
75 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" ;;
76 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC -m64" ;; 82 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC -m64" ;;
77 x86) ALLOWED_FLAGS="${ALLOWED_FLAGS} -m32" ;; 83 x86) ALLOWED_FLAGS="${ALLOWED_FLAGS} -m32" ;;
78 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 84 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
79 ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 85 ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
80 esac 86 esac
81 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"
82 94
83 # 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
84 # NOTE: currently -Os have issues with gcc3 and K6* arch's 96 # NOTE: currently -Os have issues with gcc3 and K6* arch's
85 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" 97 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays"
86 return 0 98 return 0
87} 99}
88 100
89filter-flags() { 101filter-flags() {
90 local x 102 local x f fset
103 declare -a new_CFLAGS new_CXXFLAGS
91 104
92 for x in "$@" ; do 105 for x in "$@" ; do
93 case "${x}" in 106 case "${x}" in
94 -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;; 107 -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;;
95 -fstack-protector|-fstack-protector-all) fstack-flags;; 108 -fstack-protector|-fstack-protector-all) fstack-flags;;
96 *) ;;
97 esac 109 esac
98 done 110 done
99 111
100 # we do this fancy spacing stuff so as to not filter 112 for fset in CFLAGS CXXFLAGS; do
101 # out part of a flag ... we want flag atoms ! :D 113 # Looping over the flags instead of using a global
102 CFLAGS=" ${CFLAGS} " 114 # substitution ensures that we're working with flag atoms.
103 CXXFLAGS=" ${CXXFLAGS} " 115 # Otherwise globs like -O* have the potential to wipe out the
116 # list of flags.
117 for f in ${!fset}; do
104 for x in "$@" ; do 118 for x in "$@"; do
105 CFLAGS="${CFLAGS// ${x} / }" 119 # Note this should work with globs like -O*
106 CXXFLAGS="${CXXFLAGS// ${x} / }" 120 [[ ${f} == ${x} ]] && continue 2
121 done
122 eval new_${fset}\[\${\#new_${fset}\[@]}]=\${f}
107 done 123 done
108 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" 124 eval export ${fset}=\${new_${fset}\[*]}
109 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" 125 done
126
110 return 0 127 return 0
111} 128}
112 129
113filter-lfs-flags() { 130filter-lfs-flags() {
114 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE 131 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
125 -n "`is-flag -fno-stack-protector-all`" ] && fstack-flags 142 -n "`is-flag -fno-stack-protector-all`" ] && fstack-flags
126 return 0 143 return 0
127} 144}
128 145
129replace-flags() { 146replace-flags() {
130 # we do this fancy spacing stuff so as to not filter 147 local f fset
131 # out part of a flag ... we want flag atoms ! :D 148 declare -a new_CFLAGS new_CXXFLAGS
132 CFLAGS=" ${CFLAGS} " 149
133 CXXFLAGS=" ${CXXFLAGS} " 150 for fset in CFLAGS CXXFLAGS; do
134 CFLAGS="${CFLAGS// ${1} / ${2} }" 151 # Looping over the flags instead of using a global
135 CXXFLAGS="${CXXFLAGS// ${1} / ${2} }" 152 # substitution ensures that we're working with flag atoms.
136 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" 153 # Otherwise globs like -O* have the potential to wipe out the
137 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" 154 # list of flags.
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
138 return 0 163 return 0
139} 164}
140 165
141replace-cpu-flags() { 166replace-cpu-flags() {
142 local oldcpu newcpu="$1" ; shift 167 local newcpu="$#" ; newcpu="${!newcpu}"
143 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
144 replace-flags -march=${oldcpu} -march=${newcpu} 171 replace-flags "-march=${1}" "-march=${newcpu}"
145 replace-flags -mcpu=${oldcpu} -mcpu=${newcpu} 172 replace-flags "-mcpu=${1}" "-mcpu=${newcpu}"
146 replace-flags -mtune=${oldcpu} -mtune=${newcpu} 173 replace-flags "-mtune=${1}" "-mtune=${newcpu}"
174 shift
147 done 175 done
148 return 0 176 return 0
149} 177}
150 178
151is-flag() { 179is-flag() {
152 local x 180 local x
153 181
154 for x in ${CFLAGS} ${CXXFLAGS} ; do 182 for x in ${CFLAGS} ${CXXFLAGS} ; do
183 # Note this should work with globs like -mcpu=ultrasparc*
155 if [ "${x}" == "$1" ] ; then 184 if [[ ${x} == ${1} ]]; then
156 echo true 185 echo true
157 return 0 186 return 0
158 fi 187 fi
159 done 188 done
160 return 1 189 return 1
203 set -f # disable pathname expansion 232 set -f # disable pathname expansion
204 233
205 for x in ${CFLAGS}; do 234 for x in ${CFLAGS}; do
206 for y in ${ALLOWED_FLAGS}; do 235 for y in ${ALLOWED_FLAGS}; do
207 flag=${x%%=*} 236 flag=${x%%=*}
208 if [ "${flag%%${y}}" = "" ]; then 237 if [ "${flag%%${y}}" = "" ] ; then
209 NEW_CFLAGS="${NEW_CFLAGS} ${x}" 238 NEW_CFLAGS="${NEW_CFLAGS} ${x}"
210 break 239 break
211 fi 240 fi
212 done 241 done
213 done 242 done
214 243
215 for x in ${CXXFLAGS}; do 244 for x in ${CXXFLAGS}; do
216 for y in ${ALLOWED_FLAGS}; do 245 for y in ${ALLOWED_FLAGS}; do
217 flag=${x%%=*} 246 flag=${x%%=*}
218 if [ "${flag%%${y}}" = "" ]; then 247 if [ "${flag%%${y}}" = "" ] ; then
219 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}" 248 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}"
220 break 249 break
221 fi 250 fi
222 done 251 done
223 done 252 done
230 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2" 259 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2"
231 fi 260 fi
232 261
233 set +f # re-enable pathname expansion 262 set +f # re-enable pathname expansion
234 263
235 useq debug \
236 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \
237 && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\""
238
239 export CFLAGS="${NEW_CFLAGS}" 264 export CFLAGS="${NEW_CFLAGS}"
240 export CXXFLAGS="${NEW_CXXFLAGS}" 265 export CXXFLAGS="${NEW_CXXFLAGS}"
241 return 0 266 return 0
242} 267}
243 268
244test_flag() { 269test_flag() {
245 local cc=${CC:-gcc} ; cc=${cc%% *} 270 local cc=${CC:-gcc} ; cc=${cc%% *}
246 if ${cc} -S -xc "$@" -o /dev/null /dev/null &>/dev/null; then 271 if ${cc} -S -xc "$@" -o "$(emktemp)" /dev/null &>/dev/null; then
247 printf "%s\n" "$*" 272 printf "%s\n" "$*"
248 return 0 273 return 0
249 fi 274 fi
250 return 1 275 return 1
251} 276}
289 done 314 done
290 return 1 315 return 1
291} 316}
292 317
293has_hardened() { 318has_hardened() {
294 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
295 return $? 323 return 1
296} 324}
297 325
298has_pic() { 326has_pic() {
299 [ "${CFLAGS/-fPIC}" != "${CFLAGS}" ] && return 0 327 [ "${CFLAGS/-fPIC}" != "${CFLAGS}" ] && return 0
300 [ "${CFLAGS/-fpic}" != "${CFLAGS}" ] && return 0 328 [ "${CFLAGS/-fpic}" != "${CFLAGS}" ] && return 0
314 test_version_info ssp && return 0 342 test_version_info ssp && return 0
315 return 1 343 return 1
316} 344}
317 345
318has_m64() { 346has_m64() {
319 test_flag -m64 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 local temp="$(emktemp)"
352 echo "int main() { return(0); }" > ${temp}.c
353 ${CC/ .*/} -m64 -o "$(emktemp)" ${temp}.c > /dev/null 2>&1
354 local ret=$?
355 rm -f ${temp}.c
356 [ "$ret" != "1" ] && return 0
320 return $? 357 return 1
321} 358}
322 359
323has_m32() { 360has_m32() {
324 test_flag -m32 361 # this doesnt test if the flag is accepted, it tests if the flag
362 # actually -WORKS-. non-multilib gcc will take both -m32 and -m64!
363 # please dont replace this function with test_flag in some future
364 # clean-up!
365 local temp="$(emktemp)"
366 echo "int main() { return(0); }" > ${temp}.c
367 ${CC/ .*/} -m32 -o "$(emktemp)" ${temp}.c > /dev/null 2>&1
368 local ret=$?
369 rm -f ${temp}.c
370 [ "$ret" != "1" ] && return 0
325 return $? 371 return 1
326} 372}
327 373
328replace-sparc64-flags() { 374replace-sparc64-flags() {
329 local SPARC64_CPUS="ultrasparc v9" 375 local SPARC64_CPUS="ultrasparc v9"
330 376
345 else 391 else
346 for x in ${SPARC64_CPUS}; do 392 for x in ${SPARC64_CPUS}; do
347 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" 393 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
348 done 394 done
349 fi 395 fi
396
397 export CFLAGS CXXFLAGS
350} 398}
351 399
352append-ldflags() { 400append-ldflags() {
353 LDFLAGS="${LDFLAGS} $*" 401 export LDFLAGS="${LDFLAGS} $*"
354 return 0 402 return 0
355} 403}
356 404
357filter-ldflags() { 405filter-ldflags() {
358 local x 406 local x
362 LDFLAGS=" ${LDFLAGS} " 410 LDFLAGS=" ${LDFLAGS} "
363 for x in "$@" ; do 411 for x in "$@" ; do
364 LDFLAGS="${LDFLAGS// ${x} / }" 412 LDFLAGS="${LDFLAGS// ${x} / }"
365 done 413 done
366 LDFLAGS="${LDFLAGS:1:${#LDFLAGS}-2}" 414 LDFLAGS="${LDFLAGS:1:${#LDFLAGS}-2}"
415 export LDFLAGS
367 return 0 416 return 0
368} 417}
369 418
370etexec-flags() { 419etexec-flags() {
371 # 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
372 # PIC/no-pic kludge in the first place. 421 # PIC/no-pic kludge in the first place.
373 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
374 426
375 if has_pie || has_pic; then 427 if has_pie || has_pic; then
376 [ -z "`is-flag -fno-pic`" ] && 428 [ -z "`is-flag -fno-pic`" ] &&
377 export CFLAGS="${CFLAGS} `test_flag -fno-pic`" 429 export CFLAGS="${CFLAGS} `test_flag -fno-pic`"
378 [ -z "`is-flag -nopie`" ] && 430 [ -z "`is-flag -nopie`" ] &&
386 [ -z "`is-flag -fno-stack-protector`" ] && 438 [ -z "`is-flag -fno-stack-protector`" ] &&
387 export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`" 439 export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`"
388 fi 440 fi
389 return 0 441 return 0
390} 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.58  
changed lines
  Added in v.1.73

  ViewVC Help
Powered by ViewVC 1.1.20