/[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.40 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.40 2004/03/12 11:21:15 vapier 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
47# will leave the user with -mfpmath=386 51# will leave the user with -mfpmath=386
48# 52#
49#### append-ldflags #### 53#### append-ldflags ####
50# Add extra flags to your current LDFLAGS 54# Add extra flags to your current LDFLAGS
51# 55#
56#### filter-ldflags <flags> ####
57# Remove particular flags from LDFLAGS
58# Matches only complete flags
59#
52#### etexec-flags #### 60#### etexec-flags ####
53# hooked function for hardened-gcc that appends 61# hooked function for hardened gcc that appends
54# -yet_exec {C,CXX,LD}FLAGS when hardened-gcc is installed 62# -fno-pic to {C,CXX,LD}FLAGS
55# and a package is filtering -fPIC,-fpic, -fPIE, -fpie 63# when a package is filtering -fpic, -fPIC, -fpie, -fPIE
56# 64#
57#### fstack-flags #### 65#### fstack-flags ####
58# hooked function for hardened-gcc that appends 66# hooked function for hardened gcc that appends
59# -yno_propolice to {C,CXX,LD}FLAGS when hardened-gcc is installed 67# -fno-stack-protector to {C,CXX,LD}FLAGS
60# and a package is filtering -fstack-protector, -fstack-protector-all 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
61# 71#
62 72
63# C[XX]FLAGS that we allow in strip-flags 73# C[XX]FLAGS that we allow in strip-flags
64setup-allowed-flags() { 74setup-allowed-flags() {
75 if [ -z "${ALLOWED_FLAGS}" ] ; then
65 export ALLOWED_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"
66 case "${ARCH}" in 77 case "${ARCH}" in
67 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; 78 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;;
68 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 79 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC -m64" ;;
80 x86) ALLOWED_FLAGS="${ALLOWED_FLAGS} -m32" ;;
69 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 81 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
70 ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 82 ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
71 esac 83 esac
84 fi
72 85
73 # 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
74 # NOTE: currently -Os have issues with gcc3 and K6* arch's 87 # NOTE: currently -Os have issues with gcc3 and K6* arch's
75 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" 88 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays"
76 return 0 89 return 0
77} 90}
78 91
79filter-flags() { 92filter-flags() {
93 local x f fset
94 declare -a new_CFLAGS new_CXXFLAGS
95
80 for x in "$@" ; do 96 for x in "$@" ; do
81 case "${x}" in 97 case "${x}" in
82 -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;; 98 -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;;
83 -fstack-protector|-fstack-protector-all) fstack-flags;; 99 -fstack-protector|-fstack-protector-all) fstack-flags;;
84 *) ;;
85 esac 100 esac
86 done 101 done
87 102
88 # we do this fancy spacing stuff so as to not filter 103 for fset in CFLAGS CXXFLAGS; do
89 # out part of a flag ... we want flag atoms ! :D 104 # Looping over the flags instead of using a global
90 CFLAGS=" ${CFLAGS} " 105 # substitution ensures that we're working with flag atoms.
91 CXXFLAGS=" ${CXXFLAGS} " 106 # Otherwise globs like -O* have the potential to wipe out the
107 # list of flags.
108 for f in ${!fset}; do
92 for x in "$@" ; do 109 for x in "$@"; do
93 CFLAGS="${CFLAGS// ${x} / }" 110 # Note this should work with globs like -O*
94 CXXFLAGS="${CXXFLAGS// ${x} / }" 111 [[ ${f} == ${x} ]] && continue 2
112 done
113 eval new_${fset}\[\${\#new_${fset}\[@]}]=\${f}
95 done 114 done
96 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" 115 eval export ${fset}=\${new_${fset}\[*]}
97 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" 116 done
117
98 return 0 118 return 0
99} 119}
100 120
101filter-lfs-flags() { 121filter-lfs-flags() {
102 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE 122 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
103} 123}
104 124
125append-lfs-flags() {
126 append-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
127}
128
105append-flags() { 129append-flags() {
106 export CFLAGS="${CFLAGS} $@" 130 export CFLAGS="${CFLAGS} $*"
107 export CXXFLAGS="${CXXFLAGS} $@" 131 export CXXFLAGS="${CXXFLAGS} $*"
108 [ "`is-flag -fno-stack-protector`" -o "`is-flag -fno-stack-protector-all`" ] && fstack-flags 132 [ -n "`is-flag -fno-stack-protector`" -o \
133 -n "`is-flag -fno-stack-protector-all`" ] && fstack-flags
109 return 0 134 return 0
110} 135}
111 136
112replace-flags() { 137replace-flags() {
113 # we do this fancy spacing stuff so as to not filter 138 local f fset
114 # out part of a flag ... we want flag atoms ! :D 139 declare -a new_CFLAGS new_CXXFLAGS
115 CFLAGS=" ${CFLAGS} " 140
116 CXXFLAGS=" ${CXXFLAGS} " 141 for fset in CFLAGS CXXFLAGS; do
117 CFLAGS="${CFLAGS// ${1} / ${2} }" 142 # Looping over the flags instead of using a global
118 CXXFLAGS="${CXXFLAGS// ${1} / ${2} }" 143 # substitution ensures that we're working with flag atoms.
119 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" 144 # Otherwise globs like -O* have the potential to wipe out the
120 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" 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
121 return 0 154 return 0
122} 155}
123 156
124replace-cpu-flags() { 157replace-cpu-flags() {
125 local newcpu="$1" ; shift 158 local oldcpu newcpu="$1" ; shift
126 local oldcpu=""
127 for oldcpu in "$@" ; do 159 for oldcpu in "$@" ; do
160 # quote to make sure that no globbing is done (particularly on
161 # ${oldcpu} prior to calling replace-flags
128 replace-flags -march=${oldcpu} -march=${newcpu} 162 replace-flags "-march=${oldcpu}" "-march=${newcpu}"
129 replace-flags -mcpu=${oldcpu} -mcpu=${newcpu} 163 replace-flags "-mcpu=${oldcpu}" "-mcpu=${newcpu}"
130 replace-flags -mtune=${oldcpu} -mtune=${newcpu} 164 replace-flags "-mtune=${oldcpu}" "-mtune=${newcpu}"
131 done 165 done
132 return 0 166 return 0
133} 167}
134 168
135is-flag() { 169is-flag() {
170 local x
171
136 for x in ${CFLAGS} ${CXXFLAGS} ; do 172 for x in ${CFLAGS} ${CXXFLAGS} ; do
137 if [ "${x}" == "$1" ] ; then 173 if [ "${x}" == "$1" ] ; then
138 echo true 174 echo true
139 return 0 175 return 0
140 fi 176 fi
141 done 177 done
142 return 1 178 return 1
143} 179}
144 180
145filter-mfpmath() { 181filter-mfpmath() {
182 local orig_mfpmath new_math prune_math
183
146 # save the original -mfpmath flag 184 # save the original -mfpmath flag
147 local orig_mfpmath="`get-flag -mfpmath`" 185 orig_mfpmath="`get-flag -mfpmath`"
148 # get the value of the current -mfpmath flag 186 # get the value of the current -mfpmath flag
149 local new_math=" `get-flag mfpmath | tr , ' '` " 187 new_math=" `get-flag mfpmath | tr , ' '` "
150 # figure out which math values are to be removed 188 # figure out which math values are to be removed
151 local prune_math="" 189 prune_math=""
152 for prune_math in "$@" ; do 190 for prune_math in "$@" ; do
153 new_math="${new_math/ ${prune_math} / }" 191 new_math="${new_math/ ${prune_math} / }"
154 done 192 done
155 new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`" 193 new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`"
156 194
165 fi 203 fi
166 return 0 204 return 0
167} 205}
168 206
169strip-flags() { 207strip-flags() {
208 local x y flag NEW_CFLAGS NEW_CXXFLAGS
209
170 setup-allowed-flags 210 setup-allowed-flags
171 211
172 local NEW_CFLAGS="" 212 local NEW_CFLAGS=""
173 local NEW_CXXFLAGS="" 213 local NEW_CXXFLAGS=""
174 214
175 # Allow unstable C[XX]FLAGS if we are using unstable profile ... 215 # Allow unstable C[XX]FLAGS if we are using unstable profile ...
176 if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then 216 if has ~${ARCH} ${ACCEPT_KEYWORDS} ; then
177 [ `use debug` ] && einfo "Enabling the use of some unstable flags" 217 use debug && einfo "Enabling the use of some unstable flags"
178 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}" 218 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}"
179 fi 219 fi
180 220
181 set -f 221 set -f # disable pathname expansion
182 222
183 for x in ${CFLAGS} 223 for x in ${CFLAGS}; do
184 do
185 for y in ${ALLOWED_FLAGS} 224 for y in ${ALLOWED_FLAGS}; do
186 do
187 flag=${x%%=*} 225 flag=${x%%=*}
188 if [ "${flag%%${y}}" = "" ] 226 if [ "${flag%%${y}}" = "" ] || [ "${flag:0:5}" = "-fno-" ] || [ "${flag:0:5}" = "-mno-" ] ; then
189 then
190 NEW_CFLAGS="${NEW_CFLAGS} ${x}" 227 NEW_CFLAGS="${NEW_CFLAGS} ${x}"
191 break 228 break
192 fi 229 fi
193 done 230 done
194 done 231 done
195 232
196 for x in ${CXXFLAGS} 233 for x in ${CXXFLAGS}; do
197 do
198 for y in ${ALLOWED_FLAGS} 234 for y in ${ALLOWED_FLAGS}; do
199 do
200 flag=${x%%=*} 235 flag=${x%%=*}
201 if [ "${flag%%${y}}" = "" ] 236 if [ "${flag%%${y}}" = "" ] || [ "${flag:0:5}" = "-fno-" ] || [ "${flag:0:5}" = "-mno-" ] ; then
202 then
203 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}" 237 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}"
204 break 238 break
205 fi 239 fi
206 done 240 done
207 done 241 done
212 fi 246 fi
213 if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then 247 if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then
214 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2" 248 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2"
215 fi 249 fi
216 250
217 set +f 251 set +f # re-enable pathname expansion
218
219 [ `use debug` ] \
220 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \
221 && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\""
222 252
223 export CFLAGS="${NEW_CFLAGS}" 253 export CFLAGS="${NEW_CFLAGS}"
224 export CXXFLAGS="${NEW_CXXFLAGS}" 254 export CXXFLAGS="${NEW_CXXFLAGS}"
225 return 0 255 return 0
226} 256}
227 257
228test_flag() { 258test_flag() {
259 local cc=${CC:-gcc} ; cc=${cc%% *}
229 if gcc -S -xc "$@" -o /dev/null /dev/null >/dev/null 2>&1; then 260 if ${cc} -S -xc "$@" -o /dev/null /dev/null &>/dev/null; then
230 echo "$@" 261 printf "%s\n" "$*"
231 return 0 262 return 0
232 fi 263 fi
233 return 1 264 return 1
234} 265}
235 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
236strip-unsupported-flags() { 276strip-unsupported-flags() {
277 local NEW_CFLAGS NEW_CXXFLAGS
278
237 for x in ${CFLAGS} ; do 279 for x in ${CFLAGS} ; do
238 NEW_CFLAGS=${NEW_CFLAGS}" ""`test_flag ${x}`" 280 NEW_CFLAGS="${NEW_CFLAGS} `test_flag ${x}`"
239 done 281 done
240 for x in ${CXXFLAGS} ; do 282 for x in ${CXXFLAGS} ; do
241 NEW_CXXFLAGS=${NEW_CXXFLAGS}" ""`test_flag ${x}`" 283 NEW_CXXFLAGS="${NEW_CXXFLAGS} `test_flag ${x}`"
242 done 284 done
243 285
244 export CFLAGS="${NEW_CFLAGS}" 286 export CFLAGS="${NEW_CFLAGS}"
245 export CXXFLAGS="${NEW_CXXFLAGS}" 287 export CXXFLAGS="${NEW_CXXFLAGS}"
246} 288}
247 289
248get-flag() { 290get-flag() {
291 local f findflag="$1"
292
249 # this code looks a little flaky but seems to work for 293 # this code looks a little flaky but seems to work for
250 # everything we want ... 294 # everything we want ...
251 # for example, if CFLAGS="-march=i686": 295 # for example, if CFLAGS="-march=i686":
252 # `get-flags -march` == "-march=i686" 296 # `get-flag -march` == "-march=i686"
253 # `get-flags march` == "i686" 297 # `get-flag march` == "i686"
254 local findflag="$1"
255 for f in ${CFLAGS} ${CXXFLAGS} ; do 298 for f in ${CFLAGS} ${CXXFLAGS} ; do
256 if [ "${f/${findflag}}" != "${f}" ] ; then 299 if [ "${f/${findflag}}" != "${f}" ] ; then
257 echo "${f/-${findflag}=}" 300 printf "%s\n" "${f/-${findflag}=}"
258 return 0 301 return 0
259 fi 302 fi
260 done 303 done
261 return 1 304 return 1
262} 305}
263 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
358}
359
264replace-sparc64-flags() { 360replace-sparc64-flags() {
265 local SPARC64_CPUS="ultrasparc v9" 361 local SPARC64_CPUS="ultrasparc v9"
266 362
267 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ] 363 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
268 then
269 for x in ${SPARC64_CPUS} 364 for x in ${SPARC64_CPUS}; do
270 do
271 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}" 365 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
272 done 366 done
273 else 367 else
274 for x in ${SPARC64_CPUS} 368 for x in ${SPARC64_CPUS}; do
275 do
276 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" 369 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
277 done 370 done
278 fi 371 fi
279 372
280 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ] 373 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
281 then
282 for x in ${SPARC64_CPUS} 374 for x in ${SPARC64_CPUS}; do
283 do
284 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}" 375 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
285 done 376 done
286 else 377 else
287 for x in ${SPARC64_CPUS} 378 for x in ${SPARC64_CPUS}; do
288 do
289 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" 379 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
290 done 380 done
291 fi 381 fi
382
383 export CFLAGS CXXFLAGS
292} 384}
293 385
294append-ldflags() { 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
295 LDFLAGS="${LDFLAGS} $@" 396 LDFLAGS=" ${LDFLAGS} "
397 for x in "$@" ; do
398 LDFLAGS="${LDFLAGS// ${x} / }"
399 done
400 LDFLAGS="${LDFLAGS:1:${#LDFLAGS}-2}"
401 export LDFLAGS
296 return 0 402 return 0
297} 403}
298 404
299etexec-flags() { 405etexec-flags() {
300 has_version sys-devel/hardened-gcc 406 # if you're not using a hardened compiler you wont need this
301 if [ $? == 0 ] ; then 407 # PIC/no-pic kludge in the first place.
302 if [ "`is-flag -yet_exec`" != "true" ]; then 408 has_hardened || return 0
303 debug-print ">>> appending flags -yet_exec" 409
304 append-flags -yet_exec 410 if has_pie || has_pic; then
305 append-ldflags -yet_exec 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`"
306 fi 415 fi
307 fi 416 return 0
308} 417}
309 418
310fstack-flags() { 419fstack-flags() {
311 has_version sys-devel/hardened-gcc 420 if has_ssp; then
312 if [ $? == 0 ] ; then 421 [ -z "`is-flag -fno-stack-protector`" ] &&
313 if [ "`is-flag -yno_propolice`" != "true" ]; then 422 export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`"
314 debug-print ">>> appending flags -yno_propolice"
315 append-flags -yno_propolice
316 append-ldflags -yno_propolice
317 fi 423 fi
318 fi 424 return 0
319} 425}

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

  ViewVC Help
Powered by ViewVC 1.1.20