/[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 - (show annotations) (download)
Sat Feb 21 07:07:56 2004 UTC (10 years, 5 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 # Copyright 1999-2003 Gentoo Technologies, Inc.
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header: /home/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.35 2004/01/20 10:37:19 solar Exp $
4 #
5 # Author Bart Verwilst <verwilst@gentoo.org>
6
7 ECLASS=flag-o-matic
8 INHERITED="$INHERITED $ECLASS"
9
10 #
11 #### filter-flags <flags> ####
12 # Remove particular flags from C[XX]FLAGS
13 # Matches only complete flags
14 #
15 #### append-flags <flags> ####
16 # Add extra flags to your current C[XX]FLAGS
17 #
18 #### replace-flags <orig.flag> <new.flag> ###
19 # Replace a flag by another one
20 #
21 #### is-flag <flag> ####
22 # Returns "true" if flag is set in C[XX]FLAGS
23 # Matches only complete a flag
24 #
25 #### strip-flags ####
26 # Strip C[XX]FLAGS of everything except known
27 # good options.
28 #
29 #### strip-unsupported-flags ####
30 # Strip C[XX]FLAGS of any flags not supported by
31 # installed version of gcc
32 #
33 #### get-flag <flag> ####
34 # Find and echo the value for a particular flag
35 #
36 #### replace-sparc64-flags ####
37 # Sets mcpu to v8 and uses the original value
38 # as mtune if none specified.
39 #
40 #### 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 #
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 # and a package is filtering -fPIC,-fpic, -fPIE, -fpie
52 #
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 # and a package is filtering -fstack-protector, -fstack-protector-all
57 #
58
59 # C[XX]FLAGS that we allow in strip-flags
60 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 }
74
75 filter-flags() {
76 for x in "$@" ; do
77 case "${x}" in
78 -fPIC|-fpic|-fPIE|-fpie) etexec-flags;;
79 -fstack-protector|-fstack-protector-all) fstack-flags;;
80 *) ;;
81 esac
82 done
83
84 # we do this fancy spacing stuff so as to not filter
85 # out part of a flag ... we want flag atoms ! :D
86 CFLAGS=" ${CFLAGS} "
87 CXXFLAGS=" ${CXXFLAGS} "
88 for x in "$@" ; do
89 CFLAGS="${CFLAGS/ ${x} / }"
90 CXXFLAGS="${CXXFLAGS/ ${x} / }"
91 done
92 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
93 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
94 return 0
95 }
96
97 append-flags() {
98 export CFLAGS="${CFLAGS} $@"
99 export CXXFLAGS="${CXXFLAGS} $@"
100 [ "`is-flag -fno-stack-protector`" -o "`is-flag -fno-stack-protector-all`" ] && fstack-flags
101 return 0
102 }
103
104 replace-flags() {
105 # 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 return 0
114 }
115
116 is-flag() {
117 for x in ${CFLAGS} ${CXXFLAGS} ; do
118 if [ "${x}" == "$1" ] ; then
119 echo true
120 return 0
121 fi
122 done
123 return 1
124 }
125
126 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 strip-flags() {
151 setup-allowed-flags
152
153 local NEW_CFLAGS=""
154 local NEW_CXXFLAGS=""
155
156 # Allow unstable C[XX]FLAGS if we are using unstable profile ...
157 if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then
158 [ `use debug` ] && einfo "Enabling the use of some unstable flags"
159 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}"
160 fi
161
162 set -f
163
164 for x in ${CFLAGS}
165 do
166 for y in ${ALLOWED_FLAGS}
167 do
168 flag=${x%%=*}
169 if [ "${flag%%${y}}" = "" ]
170 then
171 NEW_CFLAGS="${NEW_CFLAGS} ${x}"
172 break
173 fi
174 done
175 done
176
177 for x in ${CXXFLAGS}
178 do
179 for y in ${ALLOWED_FLAGS}
180 do
181 flag=${x%%=*}
182 if [ "${flag%%${y}}" = "" ]
183 then
184 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}"
185 break
186 fi
187 done
188 done
189
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
198 set +f
199
200 [ `use debug` ] \
201 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \
202 && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\""
203
204 export CFLAGS="${NEW_CFLAGS}"
205 export CXXFLAGS="${NEW_CXXFLAGS}"
206 return 0
207 }
208
209 test_flag() {
210 if gcc -S -xc "$@" -o /dev/null /dev/null >/dev/null 2>&1; then
211 echo "$@"
212 return 0
213 fi
214 return 1
215 }
216
217 strip-unsupported-flags() {
218 for x in ${CFLAGS} ; do
219 NEW_CFLAGS=${NEW_CFLAGS}" ""`test_flag ${x}`"
220 done
221 for x in ${CXXFLAGS} ; do
222 NEW_CXXFLAGS=${NEW_CXXFLAGS}" ""`test_flag ${x}`"
223 done
224
225 export CFLAGS="${NEW_CFLAGS}"
226 export CXXFLAGS="${NEW_CXXFLAGS}"
227 }
228
229 get-flag() {
230 # 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 local findflag="$1"
236 for f in ${CFLAGS} ${CXXFLAGS} ; do
237 if [ "${f/${findflag}}" != "${f}" ] ; then
238 echo "${f/-${findflag}=}"
239 return 0
240 fi
241 done
242 return 1
243 }
244
245 replace-sparc64-flags() {
246 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 }
274
275 append-ldflags() {
276 LDFLAGS="${LDFLAGS} $@"
277 return 0
278 }
279
280 etexec-flags() {
281 has_version sys-devel/hardened-gcc
282 if [ $? == 0 ] ; then
283 if [ "`is-flag -yet_exec`" != "true" ]; then
284 debug-print ">>> appending flags -yet_exec"
285 append-flags -yet_exec
286 append-ldflags -yet_exec
287 fi
288 fi
289 }
290
291 fstack-flags() {
292 has_version sys-devel/hardened-gcc
293 if [ $? == 0 ] ; then
294 if [ "`is-flag -yno_propolice`" != "true" ]; then
295 debug-print ">>> appending flags -yno_propolice"
296 append-flags -yno_propolice
297 append-ldflags -yno_propolice
298 fi
299 fi
300 }

  ViewVC Help
Powered by ViewVC 1.1.20