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

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.69

  ViewVC Help
Powered by ViewVC 1.1.20