/[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.65 - (show annotations) (download)
Tue Jul 27 19:25:49 2004 UTC (9 years, 11 months ago) by vapier
Branch: MAIN
Changes since 1.64: +10 -4 lines
back out the generic -fno and -mno filters and add a list of ABI related / -mno- flags

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

  ViewVC Help
Powered by ViewVC 1.1.20