/[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.36 - (hide annotations) (download)
Sat Feb 21 07:07:56 2004 UTC (10 years, 7 months ago) by vapier
Branch: MAIN
Changes since 1.35: +73 -52 lines
move ALLOWED_FLAGS/UNSTABLE_FLAGS out of global scope since only one or two functions use them
change filter-mfpmath to use other flag-o-matic functions instead of its own code
change instances of $@ to "$@" to provide better quoting
update replace-flags to only work on flag atoms
add more comments for people to understand the code
remove trailing space in etexec-flags/fstack-flags

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

  ViewVC Help
Powered by ViewVC 1.1.20