/[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.44 - (hide annotations) (download)
Thu Apr 8 21:53:15 2004 UTC (10 years, 5 months ago) by vapier
Branch: MAIN
Changes since 1.43: +10 -8 lines
allow $ALLOWED_FLAGS to be overridden in ebuilds #47140

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

  ViewVC Help
Powered by ViewVC 1.1.20