/[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.54 - (hide annotations) (download)
Sun Jun 6 16:39:18 2004 UTC (10 years, 5 months ago) by solar
Branch: MAIN
Changes since 1.53: +2 -1 lines
QA loving.. flag-o-matic has use debug && do_stuff, but had a missing IUSE=debug

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 solar 1.54 # $Header: /home/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.53 2004/06/01 04:28:38 solar 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 solar 1.54 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 vapier 1.36 for x in "$@" ; do
91 solar 1.27 case "${x}" in
92 solar 1.39 -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;;
93 solar 1.32 -fstack-protector|-fstack-protector-all) fstack-flags;;
94 solar 1.27 *) ;;
95     esac
96     done
97 vapier 1.36
98 vapier 1.18 # we do this fancy spacing stuff so as to not filter
99     # out part of a flag ... we want flag atoms ! :D
100 agriffis 1.25 CFLAGS=" ${CFLAGS} "
101     CXXFLAGS=" ${CXXFLAGS} "
102 vapier 1.36 for x in "$@" ; do
103 vapier 1.37 CFLAGS="${CFLAGS// ${x} / }"
104     CXXFLAGS="${CXXFLAGS// ${x} / }"
105 verwilst 1.1 done
106 agriffis 1.25 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
107     CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
108 vapier 1.36 return 0
109 verwilst 1.2 }
110    
111 vapier 1.40 filter-lfs-flags() {
112     filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
113     }
114    
115 vapier 1.48 append-lfs-flags() {
116     append-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
117     }
118    
119 vapier 1.18 append-flags() {
120 vapier 1.36 export CFLAGS="${CFLAGS} $@"
121     export CXXFLAGS="${CXXFLAGS} $@"
122 vapier 1.33 [ "`is-flag -fno-stack-protector`" -o "`is-flag -fno-stack-protector-all`" ] && fstack-flags
123 vapier 1.28 return 0
124 verwilst 1.1 }
125    
126 vapier 1.18 replace-flags() {
127 vapier 1.36 # we do this fancy spacing stuff so as to not filter
128     # out part of a flag ... we want flag atoms ! :D
129     CFLAGS=" ${CFLAGS} "
130     CXXFLAGS=" ${CXXFLAGS} "
131 vapier 1.37 CFLAGS="${CFLAGS// ${1} / ${2} }"
132     CXXFLAGS="${CXXFLAGS// ${1} / ${2} }"
133 vapier 1.36 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
134     CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
135 vapier 1.28 return 0
136 danarmak 1.7 }
137    
138 vapier 1.37 replace-cpu-flags() {
139     local newcpu="$1" ; shift
140     local oldcpu=""
141     for oldcpu in "$@" ; do
142     replace-flags -march=${oldcpu} -march=${newcpu}
143     replace-flags -mcpu=${oldcpu} -mcpu=${newcpu}
144 vapier 1.38 replace-flags -mtune=${oldcpu} -mtune=${newcpu}
145 vapier 1.37 done
146     return 0
147     }
148    
149 danarmak 1.7 is-flag() {
150 vapier 1.18 for x in ${CFLAGS} ${CXXFLAGS} ; do
151     if [ "${x}" == "$1" ] ; then
152 azarah 1.8 echo true
153 vapier 1.18 return 0
154     fi
155 danarmak 1.7 done
156 vapier 1.18 return 1
157 verwilst 1.1 }
158 azarah 1.8
159 vapier 1.36 filter-mfpmath() {
160     # save the original -mfpmath flag
161     local orig_mfpmath="`get-flag -mfpmath`"
162     # get the value of the current -mfpmath flag
163     local new_math=" `get-flag mfpmath | tr , ' '` "
164     # figure out which math values are to be removed
165     local prune_math=""
166     for prune_math in "$@" ; do
167     new_math="${new_math/ ${prune_math} / }"
168     done
169     new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`"
170    
171     if [ -z "${new_math}" ] ; then
172     # if we're removing all user specified math values are
173     # slated for removal, then we just filter the flag
174     filter-flags ${orig_mfpmath}
175     else
176     # if we only want to filter some of the user specified
177     # math values, then we replace the current flag
178     replace-flags ${orig_mfpmath} -mfpmath=${new_math}
179     fi
180     return 0
181     }
182    
183 azarah 1.8 strip-flags() {
184 vapier 1.36 setup-allowed-flags
185    
186 azarah 1.8 local NEW_CFLAGS=""
187     local NEW_CXXFLAGS=""
188    
189 azarah 1.17 # Allow unstable C[XX]FLAGS if we are using unstable profile ...
190 vapier 1.49 if has ~${ARCH} ${ACCEPT_KEYWORDS} ; then
191     use debug && einfo "Enabling the use of some unstable flags"
192 azarah 1.17 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}"
193     fi
194    
195 azarah 1.8 set -f
196    
197     for x in ${CFLAGS}
198     do
199     for y in ${ALLOWED_FLAGS}
200     do
201 lostlogic 1.14 flag=${x%%=*}
202     if [ "${flag%%${y}}" = "" ]
203 azarah 1.8 then
204 lostlogic 1.14 NEW_CFLAGS="${NEW_CFLAGS} ${x}"
205     break
206 azarah 1.8 fi
207     done
208     done
209    
210     for x in ${CXXFLAGS}
211     do
212     for y in ${ALLOWED_FLAGS}
213     do
214 lostlogic 1.14 flag=${x%%=*}
215     if [ "${flag%%${y}}" = "" ]
216 azarah 1.8 then
217 lostlogic 1.14 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}"
218     break
219 azarah 1.8 fi
220     done
221     done
222 azarah 1.30
223     # In case we filtered out all optimization flags fallback to -O2
224     if [ "${CFLAGS/-O}" != "${CFLAGS}" -a "${NEW_CFLAGS/-O}" = "${NEW_CFLAGS}" ]; then
225     NEW_CFLAGS="${NEW_CFLAGS} -O2"
226     fi
227     if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then
228     NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2"
229     fi
230 azarah 1.8
231     set +f
232    
233 vapier 1.49 use debug \
234 vapier 1.20 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \
235     && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\""
236    
237 azarah 1.8 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 swtaylor 1.47 if [ -z "`gcc -S -xc "$@" -o /dev/null /dev/null 2>&1`" ]; then
244 vapier 1.36 echo "$@"
245     return 0
246 puggy 1.29 fi
247 vapier 1.36 return 1
248 puggy 1.29 }
249    
250     strip-unsupported-flags() {
251 vapier 1.36 for x in ${CFLAGS} ; do
252 puggy 1.29 NEW_CFLAGS=${NEW_CFLAGS}" ""`test_flag ${x}`"
253     done
254 vapier 1.36 for x in ${CXXFLAGS} ; do
255 puggy 1.29 NEW_CXXFLAGS=${NEW_CXXFLAGS}" ""`test_flag ${x}`"
256     done
257    
258 vapier 1.36 export CFLAGS="${NEW_CFLAGS}"
259     export CXXFLAGS="${NEW_CXXFLAGS}"
260 azarah 1.8 }
261    
262 vapier 1.10 get-flag() {
263 vapier 1.36 # this code looks a little flaky but seems to work for
264     # everything we want ...
265     # for example, if CFLAGS="-march=i686":
266     # `get-flags -march` == "-march=i686"
267     # `get-flags march` == "i686"
268 vapier 1.10 local findflag="$1"
269     for f in ${CFLAGS} ${CXXFLAGS} ; do
270     if [ "${f/${findflag}}" != "${f}" ] ; then
271     echo "${f/-${findflag}=}"
272 vapier 1.28 return 0
273 vapier 1.10 fi
274     done
275 vapier 1.28 return 1
276 joker 1.16 }
277    
278 solar 1.53 has_hardened() {
279     local cc=${CC:-gcc}
280     [[ $(${cc%% *} --version 2>&1) == *Hardened* ]]
281     return $?
282     }
283    
284 tseng 1.42 has_pic() {
285     [ "${CFLAGS/-fPIC}" != "${CFLAGS}" ] && return 0
286     [ "${CFLAGS/-fpic}" != "${CFLAGS}" ] && return 0
287     [ ! -z "`${CC/ .*/} --version| grep pie`" ] && return 0
288     return 1
289     }
290    
291     has_pie() {
292 tseng 1.43 [ "${CFLAGS/-fPIE}" != "${CFLAGS}" ] && return 0
293     [ "${CFLAGS/-fpie}" != "${CFLAGS}" ] && return 0
294 tseng 1.42 [ ! -z "`${CC/ .*/} --version| grep pie`" ] && return 0
295     return 1
296     }
297    
298     has_ssp() {
299     [ "${CFLAGS/-fstack-protector}" != "${CFLAGS}" ] && return 0
300     [ ! -z "`${CC/ .*/} --version| grep ssp`" ] && return 0
301     return 1
302     }
303    
304 vapier 1.18 replace-sparc64-flags() {
305 joker 1.16 local SPARC64_CPUS="ultrasparc v9"
306    
307     if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]
308     then
309     for x in ${SPARC64_CPUS}
310     do
311     CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
312     done
313     else
314     for x in ${SPARC64_CPUS}
315     do
316     CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
317     done
318     fi
319    
320     if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]
321     then
322     for x in ${SPARC64_CPUS}
323     do
324     CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
325     done
326     else
327     for x in ${SPARC64_CPUS}
328     do
329     CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
330     done
331     fi
332 solar 1.27 }
333    
334     append-ldflags() {
335     LDFLAGS="${LDFLAGS} $@"
336 vapier 1.28 return 0
337 solar 1.27 }
338    
339 solar 1.41 filter-ldflags() {
340     # we do this fancy spacing stuff so as to not filter
341     # out part of a flag ... we want flag atoms ! :D
342     LDFLAGS=" ${LDFLAGS} "
343     for x in "$@" ; do
344     LDFLAGS="${LDFLAGS// ${x} / }"
345     done
346     LDFLAGS="${LDFLAGS:1:${#LDFLAGS}-2}"
347     return 0
348     }
349    
350 vapier 1.36 etexec-flags() {
351 solar 1.53 # if your not using a hardened compiler you wont need this
352     # PIC/no-pic kludge in the first place.
353     has_hardened || return
354    
355 tseng 1.42 has_pie || has_pic
356 vapier 1.36 if [ $? == 0 ] ; then
357 pappy 1.50 [ -z "`is-flag -fno-pic`" ] &&
358     export CFLAGS="${CFLAGS} `test_flag -fno-pic`"
359    
360     [ -z "`is-flag -nopie`" ] &&
361     export CFLAGS="${CFLAGS} `test_flag -nopie`"
362 solar 1.35 fi
363 solar 1.27 }
364    
365     fstack-flags() {
366 tseng 1.42 has_ssp
367 vapier 1.36 if [ $? == 0 ] ; then
368 pappy 1.50 [ -z "`is-flag -fno-stack-protector`" ] &&
369     export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`"
370 solar 1.35 fi
371 vapier 1.10 }

  ViewVC Help
Powered by ViewVC 1.1.20