/[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.113 - (show annotations) (download)
Wed Nov 15 22:46:52 2006 UTC (7 years, 7 months ago) by vapier
Branch: MAIN
Changes since 1.112: +46 -42 lines
rewrite some the flag handling functions to be simpler and to fix introducing extraneous whitespace

1 # Copyright 1999-2006 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.112 2006/11/15 22:17:56 vapier Exp $
4 #
5 # Maintainer: base-system@gentoo.org
6
7 # need access to emktemp()
8 inherit eutils toolchain-funcs multilib
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 #### replace-cpu-flags <old.cpus> <new.cpu> ###
22 # Replace march/mcpu flags that specify <old.cpus>
23 # with flags that specify <new.cpu>
24 #
25 #### is-flag[q] <flag> ####
26 # Returns "true" if flag is set in C[XX]FLAGS
27 # Matches only complete a flag
28 # q version sets return code but doesn't echo
29 #
30 #### is-ldflag[q] <flag> ####
31 # Returns "true" if flag is set in LDFLAGS
32 # Matches only complete a flag
33 # q version sets return code but doesn't echo
34 #
35 #### strip-flags ####
36 # Strip C[XX]FLAGS of everything except known
37 # good options.
38 #
39 #### strip-unsupported-flags ####
40 # Strip C[XX]FLAGS of any flags not supported by
41 # installed version of gcc
42 #
43 #### get-flag <flag> ####
44 # Find and echo the value for a particular flag
45 #
46 #### replace-sparc64-flags ####
47 # Sets mcpu to v8 and uses the original value
48 # as mtune if none specified.
49 #
50 #### filter-mfpmath <math types> ####
51 # Remove specified math types from the fpmath specification
52 # If the user has -mfpmath=sse,386, running `filter-mfpmath sse`
53 # will leave the user with -mfpmath=386
54 #
55 #### append-ldflags ####
56 # Add extra flags to your current LDFLAGS
57 #
58 #### filter-ldflags <flags> ####
59 # Remove particular flags from LDFLAGS
60 # Matches only complete flags
61 #
62 #### bindnow-flags ####
63 # Returns the flags to enable "now" binding in the current selected linker.
64 #
65 ################ DEPRECATED functions ################
66 # The following are still present to avoid breaking existing
67 # code more than necessary; however they are deprecated. Please
68 # use gcc-specs-* from toolchain-funcs.eclass instead, if you
69 # need to know which hardened techs are active in the compiler.
70 # See bug #100974
71 #
72 #### has_hardened ####
73 # Returns true if the compiler has 'Hardened' in its version string,
74 # (note; switched-spec vanilla compilers satisfy this condition) or
75 # the specs file name contains 'hardened'.
76 #
77 #### has_pie ####
78 # Returns true if the compiler by default or with current CFLAGS
79 # builds position-independent code.
80 #
81 #### has_pic ####
82 # Returns true if the compiler by default or with current CFLAGS
83 # builds position-independent code.
84 #
85 #### has_ssp_all ####
86 # Returns true if the compiler by default or with current CFLAGS
87 # generates stack smash protections for all functions
88 #
89 #### has_ssp ####
90 # Returns true if the compiler by default or with current CFLAGS
91 # generates stack smash protections for most vulnerable functions
92 #
93
94 # C[XX]FLAGS that we allow in strip-flags
95 setup-allowed-flags() {
96 if [[ -z ${ALLOWED_FLAGS} ]] ; then
97 export ALLOWED_FLAGS="-pipe"
98 export ALLOWED_FLAGS="${ALLOWED_FLAGS} -O -O0 -O1 -O2 -mcpu -march -mtune"
99 export ALLOWED_FLAGS="${ALLOWED_FLAGS} -fstack-protector -fstack-protector-all"
100 export ALLOWED_FLAGS="${ALLOWED_FLAGS} -fbounds-checking -fno-bounds-checking"
101 export ALLOWED_FLAGS="${ALLOWED_FLAGS} -fno-PIE -fno-pie -fno-unit-at-a-time"
102 export ALLOWED_FLAGS="${ALLOWED_FLAGS} -g -g0 -g1 -g2 -g3 -ggdb -ggdb0 -ggdb1 -ggdb2 -ggdb3"
103 export ALLOWED_FLAGS="${ALLOWED_FLAGS} -fno-ident"
104 fi
105 # allow a bunch of flags that negate features / control ABI
106 ALLOWED_FLAGS="${ALLOWED_FLAGS} -fno-stack-protector -fno-stack-protector-all"
107 ALLOWED_FLAGS="${ALLOWED_FLAGS} -mregparm -mno-app-regs -mapp-regs \
108 -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow \
109 -mips1 -mips2 -mips3 -mips4 -mips32 -mips64 -mips16 \
110 -msoft-float -mno-soft-float -mhard-float -mno-hard-float -mfpu \
111 -mieee -mieee-with-inexact -mschedule \
112 -mtls-direct-seg-refs -mno-tls-direct-seg-refs \
113 -mflat -mno-flat -mno-faster-structs -mfaster-structs \
114 -m32 -m64 -mabi -mlittle-endian -mbig-endian -EL -EB -fPIC \
115 -mlive-g0 -mcmodel -mstack-bias -mno-stack-bias"
116
117 # C[XX]FLAGS that we are think is ok, but needs testing
118 # NOTE: currently -Os have issues with gcc3 and K6* arch's
119 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks"
120 return 0
121 }
122
123 # inverted filters for hardened compiler. This is trying to unpick
124 # the hardened compiler defaults.
125 _filter-hardened() {
126 local f
127 for f in "$@" ; do
128 case "${f}" in
129 # Ideally we should only concern ourselves with PIE flags,
130 # not -fPIC or -fpic, but too many places filter -fPIC without
131 # thinking about -fPIE.
132 -fPIC|-fpic|-fPIE|-fpie|-Wl,pie|-pie)
133 gcc-specs-pie || continue
134 is-flagq -nopie || append-flags -nopie;;
135 -fstack-protector)
136 gcc-specs-ssp || continue
137 is-flagq -fno-stack-protector || append-flags -fno-stack-protector;;
138 -fstack-protector-all)
139 gcc-specs-ssp-to-all || continue
140 is-flagq -fno-stack-protector-all || append-flags -fno-stack-protector-all;;
141 esac
142 done
143 }
144
145 # Remove occurrences of strings from variable given in $1
146 # Strings removed are matched as globs, so for example
147 # '-O*' would remove -O1, -O2 etc.
148 _filter-var() {
149 local f x VAR VAL
150 declare -a new
151
152 VAR=$1
153 shift
154 eval VAL=\${${VAR}}
155 for f in ${VAL}; do
156 for x in "$@"; do
157 # Note this should work with globs like -O*
158 [[ ${f} == ${x} ]] && continue 2
159 done
160 eval new\[\${\#new\[@]}]=\${f}
161 done
162 eval export ${VAR}=\${new\[*]}
163 }
164
165 filter-flags() {
166 _filter-hardened "$@"
167 _filter-var CFLAGS "$@"
168 _filter-var CXXFLAGS "$@"
169 return 0
170 }
171
172 filter-lfs-flags() {
173 [[ -n $@ ]] && die "filter-lfs-flags takes no arguments"
174 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
175 }
176
177 append-lfs-flags() {
178 [[ -n $@ ]] && die "append-lfs-flags takes no arguments"
179 append-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
180 }
181
182 append-flags() {
183 [[ -z $* ]] && return 0
184 export CFLAGS="${CFLAGS} $*"
185 export CXXFLAGS="${CXXFLAGS} $*"
186 return 0
187 }
188
189 replace-flags() {
190 [[ $# != 2 ]] \
191 && echo && eerror "Usage: replace-flags <old flag> <new flag>" \
192 && die "replace-flags takes 2 arguments, not $#"
193
194 local f fset
195 declare -a new_CFLAGS new_CXXFLAGS
196
197 for fset in CFLAGS CXXFLAGS; do
198 # Looping over the flags instead of using a global
199 # substitution ensures that we're working with flag atoms.
200 # Otherwise globs like -O* have the potential to wipe out the
201 # list of flags.
202 for f in ${!fset}; do
203 # Note this should work with globs like -O*
204 [[ ${f} == ${1} ]] && f=${2}
205 eval new_${fset}\[\${\#new_${fset}\[@]}]=\${f}
206 done
207 eval export ${fset}=\${new_${fset}\[*]}
208 done
209
210 return 0
211 }
212
213 replace-cpu-flags() {
214 local newcpu="$#" ; newcpu="${!newcpu}"
215 while [ $# -gt 1 ] ; do
216 # quote to make sure that no globbing is done (particularly on
217 # ${oldcpu}) prior to calling replace-flags
218 replace-flags "-march=${1}" "-march=${newcpu}"
219 replace-flags "-mcpu=${1}" "-mcpu=${newcpu}"
220 replace-flags "-mtune=${1}" "-mtune=${newcpu}"
221 shift
222 done
223 return 0
224 }
225
226 _is_flagq() {
227 local x
228 for x in ${!1} ; do
229 [[ ${x} == $2 ]] && return 0
230 done
231 return 1
232 }
233
234 is-flagq() {
235 [[ -n $2 ]] && die "Usage: is-flag <flag>"
236 _is_flagq CFLAGS $1 || _is_flagq CXXFLAGS $1
237 }
238
239 is-flag() {
240 is-flagq "$@" && echo true
241 }
242
243 is-ldflagq() {
244 [[ -n $2 ]] && die "Usage: is-ldflag <flag>"
245 _is_flagq LDFLAGS $1
246 }
247
248 is-ldflag() {
249 is-ldflagq "$@" && echo true
250 }
251
252 filter-mfpmath() {
253 local orig_mfpmath new_math prune_math
254
255 # save the original -mfpmath flag
256 orig_mfpmath=$(get-flag -mfpmath)
257 # get the value of the current -mfpmath flag
258 new_math=$(get-flag mfpmath)
259 new_math=" ${new_math//,/ } "
260 # figure out which math values are to be removed
261 prune_math=""
262 for prune_math in "$@" ; do
263 new_math=${new_math/ ${prune_math} / }
264 done
265 new_math=$(echo ${new_math})
266 new_math=${new_math// /,}
267
268 if [[ -z ${new_math} ]] ; then
269 # if we're removing all user specified math values are
270 # slated for removal, then we just filter the flag
271 filter-flags ${orig_mfpmath}
272 else
273 # if we only want to filter some of the user specified
274 # math values, then we replace the current flag
275 replace-flags ${orig_mfpmath} -mfpmath=${new_math}
276 fi
277 return 0
278 }
279
280 strip-flags() {
281 local x y flag NEW_CFLAGS NEW_CXXFLAGS
282
283 setup-allowed-flags
284
285 local NEW_CFLAGS=""
286 local NEW_CXXFLAGS=""
287
288 # Allow unstable C[XX]FLAGS if we are using unstable profile ...
289 if has ~$(tc-arch) ${ACCEPT_KEYWORDS} ; then
290 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}"
291 fi
292
293 set -f # disable pathname expansion
294
295 for x in ${CFLAGS}; do
296 for y in ${ALLOWED_FLAGS}; do
297 flag=${x%%=*}
298 if [ "${flag%%${y}}" = "" ] ; then
299 NEW_CFLAGS="${NEW_CFLAGS} ${x}"
300 break
301 fi
302 done
303 done
304
305 for x in ${CXXFLAGS}; do
306 for y in ${ALLOWED_FLAGS}; do
307 flag=${x%%=*}
308 if [ "${flag%%${y}}" = "" ] ; then
309 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}"
310 break
311 fi
312 done
313 done
314
315 # In case we filtered out all optimization flags fallback to -O2
316 if [ "${CFLAGS/-O}" != "${CFLAGS}" -a "${NEW_CFLAGS/-O}" = "${NEW_CFLAGS}" ]; then
317 NEW_CFLAGS="${NEW_CFLAGS} -O2"
318 fi
319 if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then
320 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2"
321 fi
322
323 set +f # re-enable pathname expansion
324
325 export CFLAGS="${NEW_CFLAGS}"
326 export CXXFLAGS="${NEW_CXXFLAGS}"
327 return 0
328 }
329
330 test-flag-PROG() {
331 local comp=$1
332 local flags="$2"
333
334 [[ -z ${comp} || -z ${flags} ]] && \
335 return 1
336
337 local PROG=$(tc-get${comp})
338 ${PROG} ${flags} -S -o /dev/null -xc /dev/null \
339 > /dev/null 2>&1
340 }
341
342 # Returns true if C compiler support given flag
343 test-flag-CC() { test-flag-PROG "CC" "$1"; }
344
345 # Returns true if C++ compiler support given flag
346 test-flag-CXX() { test-flag-PROG "CXX" "$1"; }
347
348 test-flags-PROG() {
349 local comp=$1
350 local flags
351 local x
352
353 shift
354
355 [[ -z ${comp} ]] && return 1
356
357 x=""
358 for x in "$@" ; do
359 test-flag-${comp} "${x}" && flags="${flags}${flags:+ }${x}"
360 done
361
362 echo "${flags}"
363
364 # Just bail if we dont have any flags
365 [[ -n ${flags} ]]
366 }
367
368 # Returns (echo's) the given flags supported by the C compiler
369 test-flags-CC() { test-flags-PROG "CC" "$@"; }
370
371 # Returns (echo's) the given flags supported by the C++ compiler
372 test-flags-CXX() { test-flags-PROG "CXX" "$@"; }
373
374 # Short-hand that should hopefully work for both C and C++ compiler, but
375 # its really only present due to the append-flags() abomination.
376 test-flags() { test-flags-CC "$@"; }
377
378 # Depriciated, use test-flags()
379 test_flag() {
380 ewarn "test_flag: deprecated, please use test-flags()!" >&2
381
382 test-flags-CC "$@"
383 }
384
385 test_version_info() {
386 if [[ $($(tc-getCC) --version 2>&1) == *$1* ]]; then
387 return 0
388 else
389 return 1
390 fi
391 }
392
393 strip-unsupported-flags() {
394 export CFLAGS=$(test-flags-CC ${CFLAGS})
395 export CXXFLAGS=$(test-flags-CXX ${CXXFLAGS})
396 }
397
398 get-flag() {
399 local f findflag="$1"
400
401 # this code looks a little flaky but seems to work for
402 # everything we want ...
403 # for example, if CFLAGS="-march=i686":
404 # `get-flag -march` == "-march=i686"
405 # `get-flag march` == "i686"
406 for f in ${CFLAGS} ${CXXFLAGS} ; do
407 if [ "${f/${findflag}}" != "${f}" ] ; then
408 printf "%s\n" "${f/-${findflag}=}"
409 return 0
410 fi
411 done
412 return 1
413 }
414
415 # DEPRECATED - use gcc-specs-relro or gcc-specs-now from toolchain-funcs
416 has_hardened() {
417 ewarn "has_hardened: deprecated, please use gcc-specs-{relro,now}()!" >&2
418
419 test_version_info Hardened && return 0
420 # The specs file wont exist unless gcc has GCC_SPECS support
421 [[ -f ${GCC_SPECS} && ${GCC_SPECS} != ${GCC_SPECS/hardened/} ]]
422 }
423
424 # DEPRECATED - use gcc-specs-pie from toolchain-funcs
425 # indicate whether PIC is set
426 has_pic() {
427 ewarn "has_pic: deprecated, please use gcc-specs-pie()!" >&2
428
429 [[ ${CFLAGS/-fPIC} != ${CFLAGS} || \
430 ${CFLAGS/-fpic} != ${CFLAGS} ]] || \
431 gcc-specs-pie
432 }
433
434 # DEPRECATED - use gcc-specs-pie from toolchain-funcs
435 # indicate whether PIE is set
436 has_pie() {
437 ewarn "has_pie: deprecated, please use gcc-specs-pie()!" >&2
438
439 [[ ${CFLAGS/-fPIE} != ${CFLAGS} || \
440 ${CFLAGS/-fpie} != ${CFLAGS} ]] || \
441 gcc-specs-pie
442 }
443
444 # DEPRECATED - use gcc-specs-ssp from toolchain-funcs
445 # indicate whether code for SSP is being generated for all functions
446 has_ssp_all() {
447 ewarn "has_ssp_all: deprecated, please use gcc-specs-ssp()!" >&2
448
449 # note; this matches only -fstack-protector-all
450 [[ ${CFLAGS/-fstack-protector-all} != ${CFLAGS} || \
451 -n $(echo | $(tc-getCC) ${CFLAGS} -E -dM - | grep __SSP_ALL__) ]] || \
452 gcc-specs-ssp-all
453 }
454
455 # DEPRECATED - use gcc-specs-ssp from toolchain-funcs
456 # indicate whether code for SSP is being generated
457 has_ssp() {
458 ewarn "has_ssp: deprecated, please use gcc-specs-ssp()!" >&2
459
460 # note; this matches both -fstack-protector and -fstack-protector-all
461 [[ ${CFLAGS/-fstack-protector} != ${CFLAGS} || \
462 -n $(echo | $(tc-getCC) ${CFLAGS} -E -dM - | grep __SSP__) ]] || \
463 gcc-specs-ssp
464 }
465
466 has_m64() {
467 # this doesnt test if the flag is accepted, it tests if the flag
468 # actually -WORKS-. non-multilib gcc will take both -m32 and -m64!
469 # please dont replace this function with test_flag in some future
470 # clean-up!
471
472 local temp="$(emktemp)"
473 echo "int main() { return(0); }" > "${temp}".c
474 MY_CC=$(tc-getCC)
475 ${MY_CC/ .*/} -m64 -o "$(emktemp)" "${temp}".c > /dev/null 2>&1
476 local ret=$?
477 rm -f "${temp}".c
478 [[ ${ret} != 1 ]] && return 0
479 return 1
480 }
481
482 has_m32() {
483 # this doesnt test if the flag is accepted, it tests if the flag
484 # actually -WORKS-. non-multilib gcc will take both -m32 and -m64!
485 # please dont replace this function with test_flag in some future
486 # clean-up!
487
488 [ "$(tc-arch)" = "amd64" ] && has_multilib_profile && return 0
489
490 local temp=$(emktemp)
491 echo "int main() { return(0); }" > "${temp}".c
492 MY_CC=$(tc-getCC)
493 ${MY_CC/ .*/} -m32 -o "$(emktemp)" "${temp}".c > /dev/null 2>&1
494 local ret=$?
495 rm -f "${temp}".c
496 [[ ${ret} != 1 ]] && return 0
497 return 1
498 }
499
500 replace-sparc64-flags() {
501 local SPARC64_CPUS="ultrasparc3 ultrasparc v9"
502
503 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
504 for x in ${SPARC64_CPUS}; do
505 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
506 done
507 else
508 for x in ${SPARC64_CPUS}; do
509 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
510 done
511 fi
512
513 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
514 for x in ${SPARC64_CPUS}; do
515 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
516 done
517 else
518 for x in ${SPARC64_CPUS}; do
519 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
520 done
521 fi
522
523 export CFLAGS CXXFLAGS
524 }
525
526 append-ldflags() {
527 [[ -z $* ]] && return 0
528 export LDFLAGS="${LDFLAGS} $*"
529 return 0
530 }
531
532 # Remove flags from LDFLAGS - it's up to the ebuild to filter
533 # CFLAGS and CXXFLAGS via filter-flags if they need to.
534 filter-ldflags() {
535 _filter-var LDFLAGS "$@"
536 return 0
537 }
538
539 # Turn C style ldflags (-Wl,-foo) into straight ldflags - the results
540 # are suitable for passing directly to 'ld'; note LDFLAGS is usually passed
541 # to gcc where it needs the '-Wl,'.
542 raw-ldflags() {
543 local x input="$@"
544 [[ -z ${input} ]] && input=${LDFLAGS}
545 set --
546 for x in ${input} ; do
547 x=${x#-Wl,}
548 set -- "$@" ${x//,/ }
549 done
550 echo "$@"
551 }
552
553 # Gets the flags needed for "NOW" binding
554 bindnow-flags() {
555 case $($(tc-getLD) -v 2>&1 </dev/null) in
556 *GNU* | *'with BFD'*) # GNU ld
557 echo "-Wl,-z,now" ;;
558 *Apple*) # Darwin ld
559 echo "-bind_at_load" ;;
560 *)
561 # Some linkers just recognize -V instead of -v
562 case $($(tc-getLD) -V 2>&1 </dev/null) in
563 *Solaris*) # Solaris accept almost the same GNU options
564 echo "-Wl,-z,now" ;;
565 esac
566 ;;
567 esac
568 }
569
570
571 # Some tests for when we screw with things and want to make
572 # sure we didn't break anything
573 #TESTS() {
574 # CFLAGS="-a -b -c=1"
575 # CXXFLAGS="-x -y -z=2"
576 # LDFLAGS="-l -m -n=3"
577 #
578 # die() { exit 1; }
579 # (is-flag 1 2 3) && die
580 # (is-ldflag 1 2 3) && die
581 #
582 # is-flagq -l && die
583 # is-ldflagq -a && die
584 # is-flagq -a || die
585 # is-flagq -x || die
586 # is-ldflagq -n=* || die
587 # is-ldflagq -n && die
588 #
589 # strip-unsupported-flags
590 # [[ ${CFLAGS} == "-c=1" ]] || die
591 # [[ ${CXXFLAGS} == "-y -z=2" ]] || die
592 #
593 # echo "All tests pass"
594 #}
595 #TESTS

  ViewVC Help
Powered by ViewVC 1.1.20