/[gentoo-x86]/eclass/flag-o-matic.eclass
Gentoo

Contents of /eclass/flag-o-matic.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.69 - (hide annotations) (download)
Tue Sep 14 02:06:36 2004 UTC (10 years, 1 month ago) by lv
Branch: MAIN
Changes since 1.68: +6 -3 lines
update has_hardened for GCC_SPECS support

1 vapier 1.60 # Copyright 1999-2004 Gentoo Foundation
2 verwilst 1.1 # Distributed under the terms of the GNU General Public License v2
3 lv 1.69 # $Header: /var/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.68 2004/08/08 21:07:21 lv Exp $
4 vapier 1.12 #
5 verwilst 1.1 # Author Bart Verwilst <verwilst@gentoo.org>
6 azarah 1.8
7 verwilst 1.1 ECLASS=flag-o-matic
8 danarmak 1.6 INHERITED="$INHERITED $ECLASS"
9 vapier 1.63
10 agriffis 1.64 # Please leave ${IUSE} in this until portage .51 is stable, otherwise
11     # IUSE gets clobbered.
12     IUSE="${IUSE} debug"
13 azarah 1.8
14 verwilst 1.2 #
15 vapier 1.18 #### filter-flags <flags> ####
16 verwilst 1.3 # Remove particular flags from C[XX]FLAGS
17 vapier 1.18 # Matches only complete flags
18 verwilst 1.2 #
19 vapier 1.18 #### append-flags <flags> ####
20 verwilst 1.3 # Add extra flags to your current C[XX]FLAGS
21     #
22 verwilst 1.4 #### replace-flags <orig.flag> <new.flag> ###
23 verwilst 1.3 # Replace a flag by another one
24     #
25 vapier 1.37 #### 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 danarmak 1.7 #### is-flag <flag> ####
30     # Returns "true" if flag is set in C[XX]FLAGS
31 vapier 1.18 # Matches only complete a flag
32 danarmak 1.7 #
33 azarah 1.8 #### strip-flags ####
34     # Strip C[XX]FLAGS of everything except known
35     # good options.
36     #
37 puggy 1.29 #### strip-unsupported-flags ####
38     # Strip C[XX]FLAGS of any flags not supported by
39     # installed version of gcc
40     #
41 vapier 1.10 #### get-flag <flag> ####
42     # Find and echo the value for a particular flag
43     #
44 joker 1.16 #### replace-sparc64-flags ####
45     # Sets mcpu to v8 and uses the original value
46     # as mtune if none specified.
47     #
48 vapier 1.26 #### 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 solar 1.27 #
53     #### append-ldflags ####
54     # Add extra flags to your current LDFLAGS
55     #
56 solar 1.41 #### filter-ldflags <flags> ####
57     # Remove particular flags from LDFLAGS
58     # Matches only complete flags
59     #
60 solar 1.27 #### etexec-flags ####
61 pappy 1.50 # 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 solar 1.27 #
65     #### fstack-flags ####
66 pappy 1.50 # 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 solar 1.27 #
72 verwilst 1.3
73 azarah 1.17 # C[XX]FLAGS that we allow in strip-flags
74 vapier 1.36 setup-allowed-flags() {
75 vapier 1.44 if [ -z "${ALLOWED_FLAGS}" ] ; then
76 lv 1.68 export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -fno-stack-protector -fno-pie -fno-unit-at-a-time -pipe -g"
77 vapier 1.44 case "${ARCH}" in
78     mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;;
79 lv 1.51 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC -m64" ;;
80     x86) ALLOWED_FLAGS="${ALLOWED_FLAGS} -m32" ;;
81 vapier 1.44 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
82 vapier 1.65 ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
83 vapier 1.44 esac
84     fi
85 vapier 1.65 # 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"
91 vapier 1.36
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 aliz 1.24 }
97 verwilst 1.13
98 vapier 1.18 filter-flags() {
99 agriffis 1.61 local x f fset
100     declare -a new_CFLAGS new_CXXFLAGS
101 agriffis 1.58
102 vapier 1.36 for x in "$@" ; do
103 solar 1.27 case "${x}" in
104 solar 1.39 -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;;
105 solar 1.32 -fstack-protector|-fstack-protector-all) fstack-flags;;
106 solar 1.27 esac
107     done
108 vapier 1.36
109 agriffis 1.61 for fset in CFLAGS CXXFLAGS; do
110 agriffis 1.64 # 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 agriffis 1.61 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}
120     done
121     eval export ${fset}=\${new_${fset}\[*]}
122 verwilst 1.1 done
123 agriffis 1.61
124 vapier 1.36 return 0
125 verwilst 1.2 }
126    
127 vapier 1.40 filter-lfs-flags() {
128     filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
129     }
130    
131 vapier 1.48 append-lfs-flags() {
132     append-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
133     }
134    
135 vapier 1.18 append-flags() {
136 agriffis 1.58 export CFLAGS="${CFLAGS} $*"
137     export CXXFLAGS="${CXXFLAGS} $*"
138     [ -n "`is-flag -fno-stack-protector`" -o \
139     -n "`is-flag -fno-stack-protector-all`" ] && fstack-flags
140 vapier 1.28 return 0
141 verwilst 1.1 }
142    
143 vapier 1.18 replace-flags() {
144 agriffis 1.64 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 vapier 1.28 return 0
161 danarmak 1.7 }
162    
163 vapier 1.37 replace-cpu-flags() {
164 agriffis 1.58 local oldcpu newcpu="$1" ; shift
165 vapier 1.37 for oldcpu in "$@" ; do
166 agriffis 1.64 # 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 vapier 1.37 done
172     return 0
173     }
174    
175 danarmak 1.7 is-flag() {
176 agriffis 1.58 local x
177    
178 vapier 1.18 for x in ${CFLAGS} ${CXXFLAGS} ; do
179 agriffis 1.67 # Note this should work with globs like -mcpu=ultrasparc*
180     if [[ ${x} == ${1} ]]; then
181 azarah 1.8 echo true
182 vapier 1.18 return 0
183     fi
184 danarmak 1.7 done
185 vapier 1.18 return 1
186 verwilst 1.1 }
187 azarah 1.8
188 vapier 1.36 filter-mfpmath() {
189 agriffis 1.58 local orig_mfpmath new_math prune_math
190    
191 vapier 1.36 # save the original -mfpmath flag
192 agriffis 1.58 orig_mfpmath="`get-flag -mfpmath`"
193 vapier 1.36 # get the value of the current -mfpmath flag
194 agriffis 1.58 new_math=" `get-flag mfpmath | tr , ' '` "
195 vapier 1.36 # figure out which math values are to be removed
196 agriffis 1.58 prune_math=""
197 vapier 1.36 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    
214 azarah 1.8 strip-flags() {
215 agriffis 1.58 local x y flag NEW_CFLAGS NEW_CXXFLAGS
216    
217 vapier 1.36 setup-allowed-flags
218    
219 azarah 1.8 local NEW_CFLAGS=""
220     local NEW_CXXFLAGS=""
221    
222 azarah 1.17 # Allow unstable C[XX]FLAGS if we are using unstable profile ...
223 vapier 1.49 if has ~${ARCH} ${ACCEPT_KEYWORDS} ; then
224     use debug && einfo "Enabling the use of some unstable flags"
225 azarah 1.17 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}"
226     fi
227    
228 agriffis 1.58 set -f # disable pathname expansion
229 azarah 1.8
230 agriffis 1.58 for x in ${CFLAGS}; do
231     for y in ${ALLOWED_FLAGS}; do
232 lostlogic 1.14 flag=${x%%=*}
233 vapier 1.65 if [ "${flag%%${y}}" = "" ] ; then
234 lostlogic 1.14 NEW_CFLAGS="${NEW_CFLAGS} ${x}"
235     break
236 azarah 1.8 fi
237     done
238     done
239    
240 agriffis 1.58 for x in ${CXXFLAGS}; do
241     for y in ${ALLOWED_FLAGS}; do
242 lostlogic 1.14 flag=${x%%=*}
243 vapier 1.65 if [ "${flag%%${y}}" = "" ] ; then
244 lostlogic 1.14 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}"
245     break
246 azarah 1.8 fi
247     done
248     done
249 azarah 1.30
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 azarah 1.8
258 agriffis 1.58 set +f # re-enable pathname expansion
259 azarah 1.8
260     export CFLAGS="${NEW_CFLAGS}"
261     export CXXFLAGS="${NEW_CXXFLAGS}"
262 vapier 1.36 return 0
263 puggy 1.29 }
264    
265 vapier 1.36 test_flag() {
266 agriffis 1.58 local cc=${CC:-gcc} ; cc=${cc%% *}
267     if ${cc} -S -xc "$@" -o /dev/null /dev/null &>/dev/null; then
268     printf "%s\n" "$*"
269 vapier 1.36 return 0
270 puggy 1.29 fi
271 vapier 1.36 return 1
272 puggy 1.29 }
273    
274 agriffis 1.58 test_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    
283 puggy 1.29 strip-unsupported-flags() {
284 agriffis 1.58 local NEW_CFLAGS NEW_CXXFLAGS
285    
286 vapier 1.36 for x in ${CFLAGS} ; do
287 agriffis 1.58 NEW_CFLAGS="${NEW_CFLAGS} `test_flag ${x}`"
288 puggy 1.29 done
289 vapier 1.36 for x in ${CXXFLAGS} ; do
290 agriffis 1.58 NEW_CXXFLAGS="${NEW_CXXFLAGS} `test_flag ${x}`"
291 puggy 1.29 done
292    
293 vapier 1.36 export CFLAGS="${NEW_CFLAGS}"
294     export CXXFLAGS="${NEW_CXXFLAGS}"
295 azarah 1.8 }
296    
297 vapier 1.10 get-flag() {
298 agriffis 1.58 local f findflag="$1"
299    
300 vapier 1.36 # this code looks a little flaky but seems to work for
301     # everything we want ...
302     # for example, if CFLAGS="-march=i686":
303 agriffis 1.58 # `get-flag -march` == "-march=i686"
304     # `get-flag march` == "i686"
305 vapier 1.10 for f in ${CFLAGS} ${CXXFLAGS} ; do
306     if [ "${f/${findflag}}" != "${f}" ] ; then
307 agriffis 1.58 printf "%s\n" "${f/-${findflag}=}"
308 vapier 1.28 return 0
309 vapier 1.10 fi
310     done
311 vapier 1.28 return 1
312 joker 1.16 }
313    
314 solar 1.53 has_hardened() {
315 lv 1.69 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 solar 1.53 }
321    
322 tseng 1.42 has_pic() {
323     [ "${CFLAGS/-fPIC}" != "${CFLAGS}" ] && return 0
324     [ "${CFLAGS/-fpic}" != "${CFLAGS}" ] && return 0
325 agriffis 1.58 test_version_info pie && return 0
326 tseng 1.42 return 1
327     }
328    
329     has_pie() {
330 tseng 1.43 [ "${CFLAGS/-fPIE}" != "${CFLAGS}" ] && return 0
331     [ "${CFLAGS/-fpie}" != "${CFLAGS}" ] && return 0
332 agriffis 1.58 test_version_info pie && return 0
333 tseng 1.42 return 1
334     }
335    
336     has_ssp() {
337     [ "${CFLAGS/-fstack-protector}" != "${CFLAGS}" ] && return 0
338 agriffis 1.58 test_version_info ssp && return 0
339 tseng 1.42 return 1
340     }
341    
342 lv 1.56 has_m64() {
343 lv 1.62 # 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 lv 1.56 }
355    
356     has_m32() {
357 lv 1.62 # 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 lv 1.56 }
369    
370 vapier 1.18 replace-sparc64-flags() {
371 joker 1.16 local SPARC64_CPUS="ultrasparc v9"
372    
373 agriffis 1.58 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
374     for x in ${SPARC64_CPUS}; do
375 joker 1.16 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
376     done
377     else
378 agriffis 1.58 for x in ${SPARC64_CPUS}; do
379 joker 1.16 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
380     done
381     fi
382    
383 agriffis 1.58 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
384     for x in ${SPARC64_CPUS}; do
385 joker 1.16 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
386     done
387     else
388 agriffis 1.58 for x in ${SPARC64_CPUS}; do
389 joker 1.16 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
390     done
391     fi
392 vapier 1.59
393     export CFLAGS CXXFLAGS
394 solar 1.27 }
395    
396     append-ldflags() {
397 vapier 1.59 export LDFLAGS="${LDFLAGS} $*"
398 vapier 1.28 return 0
399 solar 1.27 }
400    
401 solar 1.41 filter-ldflags() {
402 agriffis 1.58 local x
403    
404 solar 1.41 # 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 vapier 1.59 export LDFLAGS
412 solar 1.41 return 0
413     }
414    
415 vapier 1.36 etexec-flags() {
416 agriffis 1.58 # if you're not using a hardened compiler you wont need this
417 solar 1.53 # PIC/no-pic kludge in the first place.
418 agriffis 1.58 has_hardened || return 0
419 lv 1.66 # this kludge breaks on amd64, and probably other -fPIC dependant
420     # archs.
421     use amd64 && return 0
422 pappy 1.50
423 agriffis 1.58 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 solar 1.35 fi
429 agriffis 1.58 return 0
430 solar 1.27 }
431    
432     fstack-flags() {
433 agriffis 1.58 if has_ssp; then
434     [ -z "`is-flag -fno-stack-protector`" ] &&
435     export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`"
436 solar 1.35 fi
437 agriffis 1.58 return 0
438 vapier 1.10 }

  ViewVC Help
Powered by ViewVC 1.1.20