/[gentoo-x86]/eclass/flag-o-matic.eclass
Gentoo

Diff of /eclass/flag-o-matic.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.149 Revision 1.163
1# Copyright 1999-2009 Gentoo Foundation 1# Copyright 1999-2011 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 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.149 2011/02/01 05:41:30 dirtyepic Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.163 2011/12/28 06:28:55 dirtyepic Exp $
4 4
5# @ECLASS: flag-o-matic.eclass 5# @ECLASS: flag-o-matic.eclass
6# @MAINTAINER: 6# @MAINTAINER:
7# toolchain@gentoo.org 7# toolchain@gentoo.org
8# @BLURB: common functions to manipulate and query toolchain flags 8# @BLURB: common functions to manipulate and query toolchain flags
9# @DESCRIPTION: 9# @DESCRIPTION:
10# This eclass contains a suite of functions to help developers sanely 10# This eclass contains a suite of functions to help developers sanely
11# and safely manage toolchain flags in their builds. 11# and safely manage toolchain flags in their builds.
12
13if [[ ${___ECLASS_ONCE_FLAG_O_MATIC} != "recur -_+^+_- spank" ]] ; then
14___ECLASS_ONCE_FLAG_O_MATIC="recur -_+^+_- spank"
12 15
13inherit eutils toolchain-funcs multilib 16inherit eutils toolchain-funcs multilib
14 17
15################ DEPRECATED functions ################ 18################ DEPRECATED functions ################
16# The following are still present to avoid breaking existing 19# The following are still present to avoid breaking existing
27 30
28 31
29# {C,CXX,F,FC}FLAGS that we allow in strip-flags 32# {C,CXX,F,FC}FLAGS that we allow in strip-flags
30# Note: shell globs and character lists are allowed 33# Note: shell globs and character lists are allowed
31setup-allowed-flags() { 34setup-allowed-flags() {
32 if [[ -z ${ALLOWED_FLAGS} ]] ; then
33 export ALLOWED_FLAGS="-pipe" 35 ALLOWED_FLAGS="-pipe"
34 export ALLOWED_FLAGS="${ALLOWED_FLAGS} -O -O0 -O1 -O2 -mcpu -march -mtune" 36 ALLOWED_FLAGS+=" -O -O1 -O2 -Os -mcpu -march -mtune"
35 export ALLOWED_FLAGS="${ALLOWED_FLAGS} -fstack-protector -fstack-protector-all" 37 ALLOWED_FLAGS+=" -fstack-protector -fstack-protector-all"
36 export ALLOWED_FLAGS="${ALLOWED_FLAGS} -fbounds-checking -fno-strict-overflow" 38 ALLOWED_FLAGS+=" -fbounds-checking -fno-strict-overflow"
37 export ALLOWED_FLAGS="${ALLOWED_FLAGS} -fno-PIE -fno-pie -fno-unit-at-a-time" 39 ALLOWED_FLAGS+=" -fno-PIE -fno-pie -fno-unit-at-a-time"
38 export ALLOWED_FLAGS="${ALLOWED_FLAGS} -g -g[0-9] -ggdb -ggdb[0-9] -gstabs -gstabs+" 40 ALLOWED_FLAGS+=" -g -g[0-9] -ggdb -ggdb[0-9] -gstabs -gstabs+"
39 export ALLOWED_FLAGS="${ALLOWED_FLAGS} -fno-ident" 41 ALLOWED_FLAGS+=" -fno-ident -fpermissive"
40 export ALLOWED_FLAGS="${ALLOWED_FLAGS} -W* -w" 42 ALLOWED_FLAGS+=" -W* -w"
41 fi 43
42 # allow a bunch of flags that negate features / control ABI 44 # allow a bunch of flags that negate features / control ABI
43 ALLOWED_FLAGS="${ALLOWED_FLAGS} -fno-stack-protector -fno-stack-protector-all \ 45 ALLOWED_FLAGS+=" -fno-stack-protector -fno-stack-protector-all \
44 -fno-strict-aliasing -fno-bounds-checking -fstrict-overflow -fno-omit-frame-pointer" 46 -fno-strict-aliasing -fno-bounds-checking -fstrict-overflow \
47 -fno-omit-frame-pointer"
45 ALLOWED_FLAGS="${ALLOWED_FLAGS} -mregparm -mno-app-regs -mapp-regs \ 48 ALLOWED_FLAGS+=" -mregparm -mno-app-regs -mapp-regs -mno-mmx -mno-sse \
46 -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-ssse3 -mno-sse4 -mno-sse4.1 \ 49 -mno-sse2 -mno-sse3 -mno-ssse3 -mno-sse4 -mno-sse4.1 -mno-sse4.2 \
47 -mno-sse4.2 -mno-avx -mno-aes -mno-pclmul -mno-sse4a -mno-3dnow \ 50 -mno-avx -mno-aes -mno-pclmul -mno-sse4a -mno-3dnow -mno-popcnt \
48 -mno-popcnt -mno-abm \
49 -mips1 -mips2 -mips3 -mips4 -mips32 -mips64 -mips16 -mplt \ 51 -mno-abm -mips1 -mips2 -mips3 -mips4 -mips32 -mips64 -mips16 -mplt \
50 -msoft-float -mno-soft-float -mhard-float -mno-hard-float -mfpu \ 52 -msoft-float -mno-soft-float -mhard-float -mno-hard-float -mfpu \
51 -mieee -mieee-with-inexact -mschedule -mfloat-gprs -mspe -mno-spe \ 53 -mieee -mieee-with-inexact -mschedule -mfloat-gprs -mspe -mno-spe \
52 -mtls-direct-seg-refs -mno-tls-direct-seg-refs \ 54 -mtls-direct-seg-refs -mno-tls-direct-seg-refs -mflat -mno-flat \
53 -mflat -mno-flat -mno-faster-structs -mfaster-structs \ 55 -mno-faster-structs -mfaster-structs -m32 -m64 -mx32 -mabi \
54 -m32 -m64 -mabi -mlittle-endian -mbig-endian -EL -EB -fPIC \ 56 -mlittle-endian -mbig-endian -EL -EB -fPIC -mlive-g0 -mcmodel \
55 -mlive-g0 -mcmodel -mstack-bias -mno-stack-bias \ 57 -mstack-bias -mno-stack-bias -msecure-plt -m*-toc -D* -U*"
56 -msecure-plt -m*-toc -D* -U*"
57 58
58 # 4.5 59 # 4.5
59 ALLOWED_FLAGS="${ALLOWED_FLAGS} -mno-fma4 -mno-movbe -mno-xop -mno-lwp" 60 ALLOWED_FLAGS+=" -mno-fma4 -mno-movbe -mno-xop -mno-lwp"
60 # 4.6 61 # 4.6
61 ALLOWED_FLAGS="${ALLOWED_FLAGS} -mno-fsgsbase -mno-rdrnd -mno-f16c \ 62 ALLOWED_FLAGS+=" -mno-fsgsbase -mno-rdrnd -mno-f16c -mno-bmi -mno-tbm"
62 -mno-bmi -mno-tbm"
63 63
64 # {C,CXX,F,FC}FLAGS that we are think is ok, but needs testing 64 export ALLOWED_FLAGS
65 # NOTE: currently -Os have issues with gcc3 and K6* arch's
66 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks"
67 return 0 65 return 0
68} 66}
69 67
70# inverted filters for hardened compiler. This is trying to unpick 68# inverted filters for hardened compiler. This is trying to unpick
71# the hardened compiler defaults. 69# the hardened compiler defaults.
128 126
129# @FUNCTION: filter-lfs-flags 127# @FUNCTION: filter-lfs-flags
130# @DESCRIPTION: 128# @DESCRIPTION:
131# Remove flags that enable Large File Support. 129# Remove flags that enable Large File Support.
132filter-lfs-flags() { 130filter-lfs-flags() {
133 [[ -n $@ ]] && die "filter-lfs-flags takes no arguments" 131 [[ $# -ne 0 ]] && die "filter-lfs-flags takes no arguments"
132 # http://www.gnu.org/s/libc/manual/html_node/Feature-Test-Macros.html
133 # _LARGEFILE_SOURCE: enable support for new LFS funcs (ftello/etc...)
134 # _LARGEFILE64_SOURCE: enable support for 64bit variants (off64_t/fseeko64/etc...)
135 # _FILE_OFFSET_BITS: default to 64bit variants (off_t is defined as off64_t)
134 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE 136 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
135} 137}
136 138
137# @FUNCTION: append-cppflags 139# @FUNCTION: append-cppflags
138# @USAGE: <flags> 140# @USAGE: <flags>
139# @DESCRIPTION: 141# @DESCRIPTION:
140# Add extra <flags> to the current CPPFLAGS. 142# Add extra <flags> to the current CPPFLAGS.
141append-cppflags() { 143append-cppflags() {
142 [[ -z $* ]] && return 0 144 [[ $# -eq 0 ]] && return 0
143 export CPPFLAGS="${CPPFLAGS} $*" 145 export CPPFLAGS="${CPPFLAGS} $*"
144 return 0 146 return 0
145} 147}
146 148
147# @FUNCTION: append-cflags 149# @FUNCTION: append-cflags
148# @USAGE: <flags> 150# @USAGE: <flags>
149# @DESCRIPTION: 151# @DESCRIPTION:
150# Add extra <flags> to the current CFLAGS. 152# Add extra <flags> to the current CFLAGS.
151append-cflags() { 153append-cflags() {
152 [[ -z $* ]] && return 0 154 [[ $# -eq 0 ]] && return 0
153 export CFLAGS="${CFLAGS} $*" 155 export CFLAGS=$(test-flags-CC ${CFLAGS} "$@")
154 return 0 156 return 0
155} 157}
156 158
157# @FUNCTION: append-cxxflags 159# @FUNCTION: append-cxxflags
158# @USAGE: <flags> 160# @USAGE: <flags>
159# @DESCRIPTION: 161# @DESCRIPTION:
160# Add extra <flags> to the current CXXFLAGS. 162# Add extra <flags> to the current CXXFLAGS.
161append-cxxflags() { 163append-cxxflags() {
162 [[ -z $* ]] && return 0 164 [[ $# -eq 0 ]] && return 0
163 export CXXFLAGS="${CXXFLAGS} $*" 165 export CXXFLAGS=$(test-flags-CXX ${CXXFLAGS} "$@")
164 return 0 166 return 0
165} 167}
166 168
167# @FUNCTION: append-fflags 169# @FUNCTION: append-fflags
168# @USAGE: <flags> 170# @USAGE: <flags>
169# @DESCRIPTION: 171# @DESCRIPTION:
170# Add extra <flags> to the current {F,FC}FLAGS. 172# Add extra <flags> to the current {F,FC}FLAGS.
171append-fflags() { 173append-fflags() {
172 [[ -z $* ]] && return 0 174 [[ $# -eq 0 ]] && return 0
173 export FFLAGS="${FFLAGS} $*" 175 export FFLAGS=$(test-flags-F77 ${FFLAGS} "$@")
174 export FCFLAGS="${FCFLAGS} $*" 176 export FCFLAGS=$(test-flags-FC ${FCFLAGS} "$@")
175 return 0 177 return 0
176} 178}
177 179
178# @FUNCTION: append-lfs-flags 180# @FUNCTION: append-lfs-flags
179# @DESCRIPTION: 181# @DESCRIPTION:
180# Add flags that enable Large File Support. 182# Add flags that enable Large File Support.
181append-lfs-flags() { 183append-lfs-flags() {
182 [[ -n $@ ]] && die "append-lfs-flags takes no arguments" 184 [[ $# -ne 0 ]] && die "append-lfs-flags takes no arguments"
185 # see comments in filter-lfs-flags func for meaning of these
183 append-cppflags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE 186 append-cppflags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
184} 187}
185 188
186# @FUNCTION: append-flags 189# @FUNCTION: append-flags
187# @USAGE: <flags> 190# @USAGE: <flags>
188# @DESCRIPTION: 191# @DESCRIPTION:
189# Add extra <flags> to your current {C,CXX,F,FC}FLAGS. 192# Add extra <flags> to your current {C,CXX,F,FC}FLAGS.
190append-flags() { 193append-flags() {
191 [[ -z $* ]] && return 0 194 [[ $# -eq 0 ]] && return 0
192 append-cflags "$@" 195 append-cflags "$@"
193 append-cxxflags "$@" 196 append-cxxflags "$@"
194 append-fflags "$@" 197 append-fflags "$@"
195 return 0 198 return 0
196} 199}
328 local NEW_CFLAGS="" 331 local NEW_CFLAGS=""
329 local NEW_CXXFLAGS="" 332 local NEW_CXXFLAGS=""
330 local NEW_FFLAGS="" 333 local NEW_FFLAGS=""
331 local NEW_FCFLAGS="" 334 local NEW_FCFLAGS=""
332 335
333 # Allow unstable C[XX]FLAGS if we are using unstable profile ...
334 if has "~$(tc-arch)" ${ACCEPT_KEYWORDS} ; then
335 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}"
336 fi
337
338 set -f # disable pathname expansion 336 set -f # disable pathname expansion
339 337
340 for x in ${CFLAGS}; do 338 for x in ${CFLAGS}; do
341 for y in ${ALLOWED_FLAGS}; do 339 for y in ${ALLOWED_FLAGS}; do
342 flag=${x%%=*} 340 flag=${x%%=*}
402 400
403test-flag-PROG() { 401test-flag-PROG() {
404 local comp=$1 402 local comp=$1
405 local flags="$2" 403 local flags="$2"
406 404
407 [[ -z ${comp} || -z ${flags} ]] && \ 405 [[ -z ${comp} || -z ${flags} ]] && return 1
408 return 1
409 406
407 # use -c so we can test the assembler as well
410 local PROG=$(tc-get${comp}) 408 local PROG=$(tc-get${comp})
411 ${PROG} ${flags} -S -o /dev/null -xc /dev/null \ 409 ${PROG} ${flags} -c -o /dev/null -xc /dev/null \
412 > /dev/null 2>&1 410 > /dev/null 2>&1
413} 411}
414 412
415# @FUNCTION: test-flag-CC 413# @FUNCTION: test-flag-CC
416# @USAGE: <flag> 414# @USAGE: <flag>
443 441
444 shift 442 shift
445 443
446 [[ -z ${comp} ]] && return 1 444 [[ -z ${comp} ]] && return 1
447 445
448 x=""
449 for x in "$@" ; do 446 for x in "$@" ; do
450 test-flag-${comp} "${x}" && flags="${flags}${flags:+ }${x}" 447 test-flag-${comp} "${x}" && flags="${flags}${flags:+ }${x}"
451 done 448 done
452 449
453 echo "${flags}" 450 echo "${flags}"
484# @USAGE: <flags> 481# @USAGE: <flags>
485# @DESCRIPTION: 482# @DESCRIPTION:
486# Short-hand that should hopefully work for both C and C++ compiler, but 483# Short-hand that should hopefully work for both C and C++ compiler, but
487# its really only present due to the append-flags() abomination. 484# its really only present due to the append-flags() abomination.
488test-flags() { test-flags-CC "$@"; } 485test-flags() { test-flags-CC "$@"; }
489
490# @FUNCTION: test_flag
491# @DESCRIPTION:
492# DEPRICIATED, use test-flags()
493test_flag() {
494 ewarn "test_flag: deprecated, please use test-flags()!" >&2
495
496 test-flags-CC "$@"
497}
498 486
499# @FUNCTION: test_version_info 487# @FUNCTION: test_version_info
500# @USAGE: <version> 488# @USAGE: <version>
501# @DESCRIPTION: 489# @DESCRIPTION:
502# Returns shell true if the current C compiler version matches <version>, else returns shell false. 490# Returns shell true if the current C compiler version matches <version>, else returns shell false.
538 fi 526 fi
539 done 527 done
540 return 1 528 return 1
541} 529}
542 530
543# @FUNCTION: has_hardened 531# DEAD FUNCS. Remove by Dec 2011.
544# @DESCRIPTION: 532test_flag() { die "$0: deprecated, please use test-flags()!" ; }
545# DEPRECATED - use gcc-specs-relro or gcc-specs-now from toolchain-funcs
546has_hardened() {
547 ewarn "has_hardened: deprecated, please use gcc-specs-{relro,now}()!" >&2 533has_hardened() { die "$0: deprecated, please use gcc-specs-{relro,now}()!" ; }
548
549 test_version_info Hardened && return 0
550 # The specs file wont exist unless gcc has GCC_SPECS support
551 [[ -f ${GCC_SPECS} && ${GCC_SPECS} != ${GCC_SPECS/hardened/} ]]
552}
553
554# @FUNCTION: has_pic
555# @DESCRIPTION:
556# DEPRECATED - use gcc-specs-pie from toolchain-funcs
557# indicate whether PIC is set
558has_pic() {
559 ewarn "has_pic: deprecated, please use gcc-specs-pie()!" >&2 534has_pic() { die "$0: deprecated, please use gcc-specs-pie()!" ; }
560
561 [[ ${CFLAGS/-fPIC} != ${CFLAGS} || \
562 ${CFLAGS/-fpic} != ${CFLAGS} ]] || \
563 gcc-specs-pie
564}
565
566# @FUNCTION: has_pie
567# @DESCRIPTION:
568# DEPRECATED - use gcc-specs-pie from toolchain-funcs
569# indicate whether PIE is set
570has_pie() {
571 ewarn "has_pie: deprecated, please use gcc-specs-pie()!" >&2 535has_pie() { die "$0: deprecated, please use gcc-specs-pie()!" ; }
572
573 [[ ${CFLAGS/-fPIE} != ${CFLAGS} || \
574 ${CFLAGS/-fpie} != ${CFLAGS} ]] || \
575 gcc-specs-pie
576}
577
578# @FUNCTION: has_ssp_all
579# @DESCRIPTION:
580# DEPRECATED - use gcc-specs-ssp from toolchain-funcs
581# indicate whether code for SSP is being generated for all functions
582has_ssp_all() {
583 ewarn "has_ssp_all: deprecated, please use gcc-specs-ssp()!" >&2 536has_ssp_all() { die "$0: deprecated, please use gcc-specs-ssp()!" ; }
584
585 # note; this matches only -fstack-protector-all
586 [[ ${CFLAGS/-fstack-protector-all} != ${CFLAGS} || \
587 -n $(echo | $(tc-getCC) ${CFLAGS} -E -dM - | grep __SSP_ALL__) ]] || \
588 gcc-specs-ssp-to-all
589}
590
591# @FUNCTION: has_ssp
592# @DESCRIPTION:
593# DEPRECATED - use gcc-specs-ssp from toolchain-funcs
594# indicate whether code for SSP is being generated
595has_ssp() {
596 ewarn "has_ssp: deprecated, please use gcc-specs-ssp()!" >&2 537has_ssp() { die "$0: deprecated, please use gcc-specs-ssp()!" ; }
597
598 # note; this matches both -fstack-protector and -fstack-protector-all
599 [[ ${CFLAGS/-fstack-protector} != ${CFLAGS} || \
600 -n $(echo | $(tc-getCC) ${CFLAGS} -E -dM - | grep __SSP__) ]] || \
601 gcc-specs-ssp
602}
603 538
604# @FUNCTION: has_m64 539# @FUNCTION: has_m64
605# @DESCRIPTION: 540# @DESCRIPTION:
606# This doesn't test if the flag is accepted, it tests if the flag actually 541# This doesn't test if the flag is accepted, it tests if the flag actually
607# WORKS. Non-multilib gcc will take both -m32 and -m64. If the flag works 542# WORKS. Non-multilib gcc will take both -m32 and -m64. If the flag works
677# @FUNCTION: append-libs 612# @FUNCTION: append-libs
678# @USAGE: <libs> 613# @USAGE: <libs>
679# @DESCRIPTION: 614# @DESCRIPTION:
680# Add extra <libs> to the current LIBS. 615# Add extra <libs> to the current LIBS.
681append-libs() { 616append-libs() {
682 [[ -z $* ]] && return 0 617 [[ $# -eq 0 ]] && return 0
683 local flag 618 local flag
684 for flag in "$@"; do 619 for flag in "$@"; do
685 [[ ${flag} == -l* ]] && flag=${flag#-l} 620 [[ ${flag} == -l* ]] && flag=${flag#-l}
686 export LIBS="${LIBS} -l${flag}" 621 export LIBS="${LIBS} -l${flag}"
687 done 622 done
692# @FUNCTION: append-ldflags 627# @FUNCTION: append-ldflags
693# @USAGE: <flags> 628# @USAGE: <flags>
694# @DESCRIPTION: 629# @DESCRIPTION:
695# Add extra <flags> to the current LDFLAGS. 630# Add extra <flags> to the current LDFLAGS.
696append-ldflags() { 631append-ldflags() {
697 [[ -z $* ]] && return 0 632 [[ $# -eq 0 ]] && return 0
698 local flag 633 local flag
699 for flag in "$@"; do 634 for flag in "$@"; do
700 [[ ${flag} == -l* ]] && \ 635 [[ ${flag} == -l* ]] && \
701 ewarn "Appending a library link instruction (${flag}); libraries to link to should not be passed through LDFLAGS" 636 ewarn "Appending a library link instruction (${flag}); libraries to link to should not be passed through LDFLAGS"
702 done 637 done
713 _filter-var LDFLAGS "$@" 648 _filter-var LDFLAGS "$@"
714 return 0 649 return 0
715} 650}
716 651
717# @FUNCTION: raw-ldflags 652# @FUNCTION: raw-ldflags
718# @USAGE: <flags> 653# @USAGE: [flags]
719# @DESCRIPTION: 654# @DESCRIPTION:
720# Turn C style ldflags (-Wl,-foo) into straight ldflags - the results 655# Turn C style ldflags (-Wl,-foo) into straight ldflags - the results
721# are suitable for passing directly to 'ld'; note LDFLAGS is usually passed 656# are suitable for passing directly to 'ld'; note LDFLAGS is usually passed
722# to gcc where it needs the '-Wl,'. 657# to gcc where it needs the '-Wl,'.
658#
659# If no flags are specified, then default to ${LDFLAGS}.
723raw-ldflags() { 660raw-ldflags() {
724 local x input="$@" 661 local x input="$@"
725 [[ -z ${input} ]] && input=${LDFLAGS} 662 [[ -z ${input} ]] && input=${LDFLAGS}
726 set -- 663 set --
727 for x in ${input} ; do 664 for x in ${input} ; do
738 *GNU*) # GNU ld 675 *GNU*) # GNU ld
739 echo "-Wl,--no-as-needed" ;; 676 echo "-Wl,--no-as-needed" ;;
740 esac 677 esac
741} 678}
742 679
743# Some tests for when we screw with things and want to make 680fi
744# sure we didn't break anything
745#TESTS() {
746# CFLAGS="-a -b -c=1"
747# CXXFLAGS="-x -y -z=2"
748# LDFLAGS="-l -m -n=3"
749#
750# die() { exit 1; }
751# (is-flag 1 2 3) && die
752# (is-ldflag 1 2 3) && die
753#
754# is-flagq -l && die
755# is-ldflagq -a && die
756# is-flagq -a || die
757# is-flagq -x || die
758# is-ldflagq -n=* || die
759# is-ldflagq -n && die
760#
761# strip-unsupported-flags
762# [[ ${CFLAGS} == "-c=1" ]] || die
763# [[ ${CXXFLAGS} == "-y -z=2" ]] || die
764#
765# echo "All tests pass"
766#}
767#TESTS

Legend:
Removed from v.1.149  
changed lines
  Added in v.1.163

  ViewVC Help
Powered by ViewVC 1.1.20