/[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.63 - (hide annotations) (download)
Sun Jul 18 04:59:30 2004 UTC (9 years, 9 months ago) by vapier
Branch: MAIN
Changes since 1.62: +5 -4 lines
dont strip out -fno- and -mno- related flags

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 vapier 1.63 # $Header: /var/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.62 2004/07/15 04:59:44 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     IUSE="debug"
11 azarah 1.8
12 verwilst 1.2 #
13 vapier 1.18 #### filter-flags <flags> ####
14 verwilst 1.3 # Remove particular flags from C[XX]FLAGS
15 vapier 1.18 # Matches only complete flags
16 verwilst 1.2 #
17 vapier 1.18 #### append-flags <flags> ####
18 verwilst 1.3 # Add extra flags to your current C[XX]FLAGS
19     #
20 verwilst 1.4 #### replace-flags <orig.flag> <new.flag> ###
21 verwilst 1.3 # Replace a flag by another one
22     #
23 vapier 1.37 #### replace-cpu-flags <new.cpu> <old.cpus> ###
24     # Replace march/mcpu flags that specify <old.cpus>
25     # with flags that specify <new.cpu>
26     #
27 danarmak 1.7 #### is-flag <flag> ####
28     # Returns "true" if flag is set in C[XX]FLAGS
29 vapier 1.18 # Matches only complete a flag
30 danarmak 1.7 #
31 azarah 1.8 #### strip-flags ####
32     # Strip C[XX]FLAGS of everything except known
33     # good options.
34     #
35 puggy 1.29 #### strip-unsupported-flags ####
36     # Strip C[XX]FLAGS of any flags not supported by
37     # installed version of gcc
38     #
39 vapier 1.10 #### get-flag <flag> ####
40     # Find and echo the value for a particular flag
41     #
42 joker 1.16 #### replace-sparc64-flags ####
43     # Sets mcpu to v8 and uses the original value
44     # as mtune if none specified.
45     #
46 vapier 1.26 #### filter-mfpmath <math types> ####
47     # Remove specified math types from the fpmath specification
48     # If the user has -mfpmath=sse,386, running `filter-mfpmath sse`
49     # will leave the user with -mfpmath=386
50 solar 1.27 #
51     #### append-ldflags ####
52     # Add extra flags to your current LDFLAGS
53     #
54 solar 1.41 #### filter-ldflags <flags> ####
55     # Remove particular flags from LDFLAGS
56     # Matches only complete flags
57     #
58 solar 1.27 #### etexec-flags ####
59 pappy 1.50 # hooked function for hardened gcc that appends
60     # -fno-pic to {C,CXX,LD}FLAGS
61     # when a package is filtering -fpic, -fPIC, -fpie, -fPIE
62 solar 1.27 #
63     #### fstack-flags ####
64 pappy 1.50 # hooked function for hardened gcc that appends
65     # -fno-stack-protector to {C,CXX,LD}FLAGS
66     # when a package is filtering -fstack-protector, -fstack-protector-all
67     # notice: modern automatic specs files will also suppress -fstack-protector-all
68     # when only -fno-stack-protector is given
69 solar 1.27 #
70 verwilst 1.3
71 azarah 1.17 # C[XX]FLAGS that we allow in strip-flags
72 vapier 1.36 setup-allowed-flags() {
73 vapier 1.44 if [ -z "${ALLOWED_FLAGS}" ] ; then
74 lv 1.52 export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -fno-unit-at-a-time -pipe -g"
75 vapier 1.44 case "${ARCH}" in
76     mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;;
77 lv 1.51 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC -m64" ;;
78     x86) ALLOWED_FLAGS="${ALLOWED_FLAGS} -m32" ;;
79 vapier 1.44 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
80     ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
81     esac
82     fi
83 vapier 1.36
84     # C[XX]FLAGS that we are think is ok, but needs testing
85     # NOTE: currently -Os have issues with gcc3 and K6* arch's
86     export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays"
87     return 0
88 aliz 1.24 }
89 verwilst 1.13
90 vapier 1.18 filter-flags() {
91 agriffis 1.61 local x f fset
92     declare -a new_CFLAGS new_CXXFLAGS
93 agriffis 1.58
94 vapier 1.36 for x in "$@" ; do
95 solar 1.27 case "${x}" in
96 solar 1.39 -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;;
97 solar 1.32 -fstack-protector|-fstack-protector-all) fstack-flags;;
98 solar 1.27 esac
99     done
100 vapier 1.36
101 agriffis 1.61 for fset in CFLAGS CXXFLAGS; do
102     for f in ${!fset}; do
103     for x in "$@"; do
104     # Note this should work with globs like -O*
105     [[ ${f} == ${x} ]] && continue 2
106     done
107     eval new_${fset}\[\${\#new_${fset}\[@]}]=\${f}
108     done
109     eval export ${fset}=\${new_${fset}\[*]}
110 verwilst 1.1 done
111 agriffis 1.61
112 vapier 1.36 return 0
113 verwilst 1.2 }
114    
115 vapier 1.40 filter-lfs-flags() {
116     filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
117     }
118    
119 vapier 1.48 append-lfs-flags() {
120     append-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
121     }
122    
123 vapier 1.18 append-flags() {
124 agriffis 1.58 export CFLAGS="${CFLAGS} $*"
125     export CXXFLAGS="${CXXFLAGS} $*"
126     [ -n "`is-flag -fno-stack-protector`" -o \
127     -n "`is-flag -fno-stack-protector-all`" ] && fstack-flags
128 vapier 1.28 return 0
129 verwilst 1.1 }
130    
131 vapier 1.18 replace-flags() {
132 vapier 1.36 # we do this fancy spacing stuff so as to not filter
133     # out part of a flag ... we want flag atoms ! :D
134     CFLAGS=" ${CFLAGS} "
135     CXXFLAGS=" ${CXXFLAGS} "
136 vapier 1.37 CFLAGS="${CFLAGS// ${1} / ${2} }"
137     CXXFLAGS="${CXXFLAGS// ${1} / ${2} }"
138 vapier 1.36 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
139     CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
140 vapier 1.59 export CFLAGS CXXFLAGS
141 vapier 1.28 return 0
142 danarmak 1.7 }
143    
144 vapier 1.37 replace-cpu-flags() {
145 agriffis 1.58 local oldcpu newcpu="$1" ; shift
146 vapier 1.37 for oldcpu in "$@" ; do
147     replace-flags -march=${oldcpu} -march=${newcpu}
148     replace-flags -mcpu=${oldcpu} -mcpu=${newcpu}
149 vapier 1.38 replace-flags -mtune=${oldcpu} -mtune=${newcpu}
150 vapier 1.37 done
151     return 0
152     }
153    
154 danarmak 1.7 is-flag() {
155 agriffis 1.58 local x
156    
157 vapier 1.18 for x in ${CFLAGS} ${CXXFLAGS} ; do
158     if [ "${x}" == "$1" ] ; then
159 azarah 1.8 echo true
160 vapier 1.18 return 0
161     fi
162 danarmak 1.7 done
163 vapier 1.18 return 1
164 verwilst 1.1 }
165 azarah 1.8
166 vapier 1.36 filter-mfpmath() {
167 agriffis 1.58 local orig_mfpmath new_math prune_math
168    
169 vapier 1.36 # save the original -mfpmath flag
170 agriffis 1.58 orig_mfpmath="`get-flag -mfpmath`"
171 vapier 1.36 # get the value of the current -mfpmath flag
172 agriffis 1.58 new_math=" `get-flag mfpmath | tr , ' '` "
173 vapier 1.36 # figure out which math values are to be removed
174 agriffis 1.58 prune_math=""
175 vapier 1.36 for prune_math in "$@" ; do
176     new_math="${new_math/ ${prune_math} / }"
177     done
178     new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`"
179    
180     if [ -z "${new_math}" ] ; then
181     # if we're removing all user specified math values are
182     # slated for removal, then we just filter the flag
183     filter-flags ${orig_mfpmath}
184     else
185     # if we only want to filter some of the user specified
186     # math values, then we replace the current flag
187     replace-flags ${orig_mfpmath} -mfpmath=${new_math}
188     fi
189     return 0
190     }
191    
192 azarah 1.8 strip-flags() {
193 agriffis 1.58 local x y flag NEW_CFLAGS NEW_CXXFLAGS
194    
195 vapier 1.36 setup-allowed-flags
196    
197 azarah 1.8 local NEW_CFLAGS=""
198     local NEW_CXXFLAGS=""
199    
200 azarah 1.17 # Allow unstable C[XX]FLAGS if we are using unstable profile ...
201 vapier 1.49 if has ~${ARCH} ${ACCEPT_KEYWORDS} ; then
202     use debug && einfo "Enabling the use of some unstable flags"
203 azarah 1.17 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}"
204     fi
205    
206 agriffis 1.58 set -f # disable pathname expansion
207 azarah 1.8
208 agriffis 1.58 for x in ${CFLAGS}; do
209     for y in ${ALLOWED_FLAGS}; do
210 lostlogic 1.14 flag=${x%%=*}
211 vapier 1.63 if [ "${flag%%${y}}" = "" ] || [ "${flag:0:5}" = "-fno-" ] || [ "${flag:0:5}" = "-mno-" ] ; then
212 lostlogic 1.14 NEW_CFLAGS="${NEW_CFLAGS} ${x}"
213     break
214 azarah 1.8 fi
215     done
216     done
217    
218 agriffis 1.58 for x in ${CXXFLAGS}; do
219     for y in ${ALLOWED_FLAGS}; do
220 lostlogic 1.14 flag=${x%%=*}
221 vapier 1.63 if [ "${flag%%${y}}" = "" ] || [ "${flag:0:5}" = "-fno-" ] || [ "${flag:0:5}" = "-mno-" ] ; then
222 lostlogic 1.14 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}"
223     break
224 azarah 1.8 fi
225     done
226     done
227 azarah 1.30
228     # In case we filtered out all optimization flags fallback to -O2
229     if [ "${CFLAGS/-O}" != "${CFLAGS}" -a "${NEW_CFLAGS/-O}" = "${NEW_CFLAGS}" ]; then
230     NEW_CFLAGS="${NEW_CFLAGS} -O2"
231     fi
232     if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then
233     NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2"
234     fi
235 azarah 1.8
236 agriffis 1.58 set +f # re-enable pathname expansion
237 azarah 1.8
238     export CFLAGS="${NEW_CFLAGS}"
239     export CXXFLAGS="${NEW_CXXFLAGS}"
240 vapier 1.36 return 0
241 puggy 1.29 }
242    
243 vapier 1.36 test_flag() {
244 agriffis 1.58 local cc=${CC:-gcc} ; cc=${cc%% *}
245     if ${cc} -S -xc "$@" -o /dev/null /dev/null &>/dev/null; then
246     printf "%s\n" "$*"
247 vapier 1.36 return 0
248 puggy 1.29 fi
249 vapier 1.36 return 1
250 puggy 1.29 }
251    
252 agriffis 1.58 test_version_info() {
253     local cc=${CC:-gcc} ; cc=${cc%% *}
254     if [[ $(${cc} --version 2>&1) == *$1* ]]; then
255     return 0
256     else
257     return 1
258     fi
259     }
260    
261 puggy 1.29 strip-unsupported-flags() {
262 agriffis 1.58 local NEW_CFLAGS NEW_CXXFLAGS
263    
264 vapier 1.36 for x in ${CFLAGS} ; do
265 agriffis 1.58 NEW_CFLAGS="${NEW_CFLAGS} `test_flag ${x}`"
266 puggy 1.29 done
267 vapier 1.36 for x in ${CXXFLAGS} ; do
268 agriffis 1.58 NEW_CXXFLAGS="${NEW_CXXFLAGS} `test_flag ${x}`"
269 puggy 1.29 done
270    
271 vapier 1.36 export CFLAGS="${NEW_CFLAGS}"
272     export CXXFLAGS="${NEW_CXXFLAGS}"
273 azarah 1.8 }
274    
275 vapier 1.10 get-flag() {
276 agriffis 1.58 local f findflag="$1"
277    
278 vapier 1.36 # this code looks a little flaky but seems to work for
279     # everything we want ...
280     # for example, if CFLAGS="-march=i686":
281 agriffis 1.58 # `get-flag -march` == "-march=i686"
282     # `get-flag march` == "i686"
283 vapier 1.10 for f in ${CFLAGS} ${CXXFLAGS} ; do
284     if [ "${f/${findflag}}" != "${f}" ] ; then
285 agriffis 1.58 printf "%s\n" "${f/-${findflag}=}"
286 vapier 1.28 return 0
287 vapier 1.10 fi
288     done
289 vapier 1.28 return 1
290 joker 1.16 }
291    
292 solar 1.53 has_hardened() {
293 agriffis 1.58 test_version_info Hardened
294 solar 1.53 return $?
295     }
296    
297 tseng 1.42 has_pic() {
298     [ "${CFLAGS/-fPIC}" != "${CFLAGS}" ] && return 0
299     [ "${CFLAGS/-fpic}" != "${CFLAGS}" ] && return 0
300 agriffis 1.58 test_version_info pie && return 0
301 tseng 1.42 return 1
302     }
303    
304     has_pie() {
305 tseng 1.43 [ "${CFLAGS/-fPIE}" != "${CFLAGS}" ] && return 0
306     [ "${CFLAGS/-fpie}" != "${CFLAGS}" ] && return 0
307 agriffis 1.58 test_version_info pie && return 0
308 tseng 1.42 return 1
309     }
310    
311     has_ssp() {
312     [ "${CFLAGS/-fstack-protector}" != "${CFLAGS}" ] && return 0
313 agriffis 1.58 test_version_info ssp && return 0
314 tseng 1.42 return 1
315     }
316    
317 lv 1.56 has_m64() {
318 lv 1.62 # 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!
320     # please dont replace this function with test_flag in some future
321     # clean-up!
322     temp=`mktemp`
323     echo "int main() { return(0); }" > ${temp}.c
324     ${CC/ .*/} -m64 -o /dev/null ${temp}.c > /dev/null 2>&1
325     ret=$?
326     rm -f ${temp}.c
327     [ "$ret" != "1" ] && return 0
328     return 1
329 lv 1.56 }
330    
331     has_m32() {
332 lv 1.62 # 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!
334     # please dont replace this function with test_flag in some future
335     # clean-up!
336     temp=`mktemp`
337     echo "int main() { return(0); }" > ${temp}.c
338     ${CC/ .*/} -m32 -o /dev/null ${temp}.c > /dev/null 2>&1
339     ret=$?
340     rm -f ${temp}.c
341     [ "$ret" != "1" ] && return 0
342     return 1
343 lv 1.56 }
344    
345 vapier 1.18 replace-sparc64-flags() {
346 joker 1.16 local SPARC64_CPUS="ultrasparc v9"
347    
348 agriffis 1.58 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
349     for x in ${SPARC64_CPUS}; do
350 joker 1.16 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
351     done
352     else
353 agriffis 1.58 for x in ${SPARC64_CPUS}; do
354 joker 1.16 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
355     done
356     fi
357    
358 agriffis 1.58 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
359     for x in ${SPARC64_CPUS}; do
360 joker 1.16 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
361     done
362     else
363 agriffis 1.58 for x in ${SPARC64_CPUS}; do
364 joker 1.16 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
365     done
366     fi
367 vapier 1.59
368     export CFLAGS CXXFLAGS
369 solar 1.27 }
370    
371     append-ldflags() {
372 vapier 1.59 export LDFLAGS="${LDFLAGS} $*"
373 vapier 1.28 return 0
374 solar 1.27 }
375    
376 solar 1.41 filter-ldflags() {
377 agriffis 1.58 local x
378    
379 solar 1.41 # we do this fancy spacing stuff so as to not filter
380     # out part of a flag ... we want flag atoms ! :D
381     LDFLAGS=" ${LDFLAGS} "
382     for x in "$@" ; do
383     LDFLAGS="${LDFLAGS// ${x} / }"
384     done
385     LDFLAGS="${LDFLAGS:1:${#LDFLAGS}-2}"
386 vapier 1.59 export LDFLAGS
387 solar 1.41 return 0
388     }
389    
390 vapier 1.36 etexec-flags() {
391 agriffis 1.58 # if you're not using a hardened compiler you wont need this
392 solar 1.53 # PIC/no-pic kludge in the first place.
393 agriffis 1.58 has_hardened || return 0
394 pappy 1.50
395 agriffis 1.58 if has_pie || has_pic; then
396     [ -z "`is-flag -fno-pic`" ] &&
397     export CFLAGS="${CFLAGS} `test_flag -fno-pic`"
398     [ -z "`is-flag -nopie`" ] &&
399     export CFLAGS="${CFLAGS} `test_flag -nopie`"
400 solar 1.35 fi
401 agriffis 1.58 return 0
402 solar 1.27 }
403    
404     fstack-flags() {
405 agriffis 1.58 if has_ssp; then
406     [ -z "`is-flag -fno-stack-protector`" ] &&
407     export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`"
408 solar 1.35 fi
409 agriffis 1.58 return 0
410 vapier 1.10 }

  ViewVC Help
Powered by ViewVC 1.1.20