/[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.62 - (hide annotations) (download)
Thu Jul 15 04:59:44 2004 UTC (9 years, 11 months ago) by lv
Branch: MAIN
Changes since 1.61: +23 -5 lines
revert some QA done on the multilib checks so that they work again

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

  ViewVC Help
Powered by ViewVC 1.1.20