1 | # Copyright 1999-2005 Gentoo Foundation |
1 | # Copyright 1999-2012 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.104 2006/01/14 10:52:12 kevquinn Exp $ |
3 | # $Header: /var/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.175 2012/06/05 02:16:40 dirtyepic Exp $ |
4 | |
4 | |
|
|
5 | # @ECLASS: flag-o-matic.eclass |
|
|
6 | # @MAINTAINER: |
|
|
7 | # toolchain@gentoo.org |
|
|
8 | # @BLURB: common functions to manipulate and query toolchain flags |
|
|
9 | # @DESCRIPTION: |
|
|
10 | # This eclass contains a suite of functions to help developers sanely |
|
|
11 | # and safely manage toolchain flags in their builds. |
5 | |
12 | |
6 | # need access to emktemp() |
13 | if [[ ${___ECLASS_ONCE_FLAG_O_MATIC} != "recur -_+^+_- spank" ]] ; then |
|
|
14 | ___ECLASS_ONCE_FLAG_O_MATIC="recur -_+^+_- spank" |
|
|
15 | |
7 | inherit eutils toolchain-funcs multilib |
16 | inherit eutils toolchain-funcs multilib |
8 | |
17 | |
9 | # |
18 | # Return all the flag variables that our high level funcs operate on. |
10 | #### filter-flags <flags> #### |
19 | all-flag-vars() { |
11 | # Remove particular flags from C[XX]FLAGS |
20 | echo {C,CPP,CXX,CCAS,F,FC,LD}FLAGS |
12 | # Matches only complete flags |
21 | } |
13 | # |
|
|
14 | #### append-flags <flags> #### |
|
|
15 | # Add extra flags to your current C[XX]FLAGS |
|
|
16 | # |
|
|
17 | #### replace-flags <orig.flag> <new.flag> ### |
|
|
18 | # Replace a flag by another one |
|
|
19 | # |
|
|
20 | #### replace-cpu-flags <old.cpus> <new.cpu> ### |
|
|
21 | # Replace march/mcpu flags that specify <old.cpus> |
|
|
22 | # with flags that specify <new.cpu> |
|
|
23 | # |
|
|
24 | #### is-flag <flag> #### |
|
|
25 | # Returns "true" if flag is set in C[XX]FLAGS |
|
|
26 | # Matches only complete a flag |
|
|
27 | # |
|
|
28 | #### strip-flags #### |
|
|
29 | # Strip C[XX]FLAGS of everything except known |
|
|
30 | # good options. |
|
|
31 | # |
|
|
32 | #### strip-unsupported-flags #### |
|
|
33 | # Strip C[XX]FLAGS of any flags not supported by |
|
|
34 | # installed version of gcc |
|
|
35 | # |
|
|
36 | #### get-flag <flag> #### |
|
|
37 | # Find and echo the value for a particular flag |
|
|
38 | # |
|
|
39 | #### replace-sparc64-flags #### |
|
|
40 | # Sets mcpu to v8 and uses the original value |
|
|
41 | # as mtune if none specified. |
|
|
42 | # |
|
|
43 | #### filter-mfpmath <math types> #### |
|
|
44 | # Remove specified math types from the fpmath specification |
|
|
45 | # If the user has -mfpmath=sse,386, running `filter-mfpmath sse` |
|
|
46 | # will leave the user with -mfpmath=386 |
|
|
47 | # |
|
|
48 | #### append-ldflags #### |
|
|
49 | # Add extra flags to your current LDFLAGS |
|
|
50 | # |
|
|
51 | #### filter-ldflags <flags> #### |
|
|
52 | # Remove particular flags from LDFLAGS |
|
|
53 | # Matches only complete flags |
|
|
54 | # |
|
|
55 | #### fstack-flags #### |
|
|
56 | # hooked function for hardened gcc that appends |
|
|
57 | # -fno-stack-protector to {C,CXX,LD}FLAGS |
|
|
58 | # when a package is filtering -fstack-protector, -fstack-protector-all |
|
|
59 | # notice: modern automatic specs files will also suppress -fstack-protector-all |
|
|
60 | # when only -fno-stack-protector is given |
|
|
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 | |
22 | |
94 | # C[XX]FLAGS that we allow in strip-flags |
23 | # {C,CXX,F,FC}FLAGS that we allow in strip-flags |
|
|
24 | # Note: shell globs and character lists are allowed |
95 | setup-allowed-flags() { |
25 | setup-allowed-flags() { |
96 | if [[ -z ${ALLOWED_FLAGS} ]] ; then |
|
|
97 | export ALLOWED_FLAGS="-pipe" |
26 | ALLOWED_FLAGS="-pipe" |
98 | export ALLOWED_FLAGS="${ALLOWED_FLAGS} -O -O0 -O1 -O2 -mcpu -march -mtune" |
27 | ALLOWED_FLAGS+=" -O -O1 -O2 -Os -mcpu -march -mtune" |
99 | export ALLOWED_FLAGS="${ALLOWED_FLAGS} -fstack-protector -fstack-protector-all" |
28 | ALLOWED_FLAGS+=" -fstack-protector -fstack-protector-all" |
100 | export ALLOWED_FLAGS="${ALLOWED_FLAGS} -fbounds-checking -fno-bounds-checking" |
29 | ALLOWED_FLAGS+=" -fbounds-checking -fno-strict-overflow" |
101 | export ALLOWED_FLAGS="${ALLOWED_FLAGS} -fno-pie -fno-unit-at-a-time" |
30 | 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" |
31 | ALLOWED_FLAGS+=" -g -g[0-9] -ggdb -ggdb[0-9] -gstabs -gstabs+" |
103 | fi |
32 | ALLOWED_FLAGS+=" -fno-ident -fpermissive" |
|
|
33 | ALLOWED_FLAGS+=" -W* -w" |
|
|
34 | |
104 | # allow a bunch of flags that negate features / control ABI |
35 | # allow a bunch of flags that negate features / control ABI |
105 | ALLOWED_FLAGS="${ALLOWED_FLAGS} -fno-stack-protector -fno-stack-protector-all" |
36 | ALLOWED_FLAGS+=" -fno-stack-protector -fno-stack-protector-all \ |
|
|
37 | -fno-strict-aliasing -fno-bounds-checking -fstrict-overflow \ |
|
|
38 | -fno-omit-frame-pointer" |
106 | ALLOWED_FLAGS="${ALLOWED_FLAGS} -mregparm -mno-app-regs -mapp-regs \ |
39 | ALLOWED_FLAGS+=" -mregparm -mno-app-regs -mapp-regs -mno-mmx -mno-sse \ |
107 | -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow \ |
40 | -mno-sse2 -mno-sse3 -mno-ssse3 -mno-sse4 -mno-sse4.1 -mno-sse4.2 \ |
|
|
41 | -mno-avx -mno-aes -mno-pclmul -mno-sse4a -mno-3dnow -mno-popcnt \ |
108 | -mips1 -mips2 -mips3 -mips4 -mips32 -mips64 -mips16 \ |
42 | -mno-abm -mips1 -mips2 -mips3 -mips4 -mips32 -mips64 -mips16 -mplt \ |
109 | -msoft-float -mno-soft-float -mhard-float -mno-hard-float -mfpu \ |
43 | -msoft-float -mno-soft-float -mhard-float -mno-hard-float -mfpu \ |
110 | -mieee -mieee-with-inexact \ |
44 | -mieee -mieee-with-inexact -mschedule -mfloat-gprs -mspe -mno-spe \ |
111 | -mtls-direct-seg-refs -mno-tls-direct-seg-refs \ |
45 | -mtls-direct-seg-refs -mno-tls-direct-seg-refs -mflat -mno-flat \ |
112 | -mflat -mno-flat -mno-faster-structs -mfaster-structs \ |
46 | -mno-faster-structs -mfaster-structs -m32 -m64 -mx32 -mabi \ |
113 | -m32 -m64 -mabi -mlittle-endian -mbig-endian -EL -EB -fPIC \ |
47 | -mlittle-endian -mbig-endian -EL -EB -fPIC -mlive-g0 -mcmodel \ |
114 | -mlive-g0 -mcmodel -mstack-bias -mno-stack-bias" |
48 | -mstack-bias -mno-stack-bias -msecure-plt -m*-toc -mfloat-abi=* \ |
|
|
49 | -D* -U*" |
115 | |
50 | |
116 | # C[XX]FLAGS that we are think is ok, but needs testing |
51 | # 4.5 |
117 | # NOTE: currently -Os have issues with gcc3 and K6* arch's |
52 | ALLOWED_FLAGS+=" -mno-fma4 -mno-movbe -mno-xop -mno-lwp" |
118 | export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" |
53 | # 4.6 |
119 | return 0 |
54 | ALLOWED_FLAGS+=" -mno-fsgsbase -mno-rdrnd -mno-f16c -mno-bmi -mno-tbm" |
120 | } |
55 | # 4.7 |
|
|
56 | ALLOWED_FLAGS+=" -mno-avx2 -mno-bmi2 -mno-fma -mno-lzcnt" |
121 | |
57 | |
|
|
58 | export ALLOWED_FLAGS |
|
|
59 | return 0 |
|
|
60 | } |
|
|
61 | |
|
|
62 | # inverted filters for hardened compiler. This is trying to unpick |
|
|
63 | # the hardened compiler defaults. |
|
|
64 | _filter-hardened() { |
|
|
65 | local f |
|
|
66 | for f in "$@" ; do |
|
|
67 | case "${f}" in |
|
|
68 | # Ideally we should only concern ourselves with PIE flags, |
|
|
69 | # not -fPIC or -fpic, but too many places filter -fPIC without |
|
|
70 | # thinking about -fPIE. |
|
|
71 | -fPIC|-fpic|-fPIE|-fpie|-Wl,pie|-pie) |
|
|
72 | gcc-specs-pie || continue |
|
|
73 | is-flagq -nopie || append-flags -nopie;; |
|
|
74 | -fstack-protector) |
|
|
75 | gcc-specs-ssp || continue |
|
|
76 | is-flagq -fno-stack-protector || append-flags $(test-flags -fno-stack-protector);; |
|
|
77 | -fstack-protector-all) |
|
|
78 | gcc-specs-ssp-to-all || continue |
|
|
79 | is-flagq -fno-stack-protector-all || append-flags $(test-flags -fno-stack-protector-all);; |
|
|
80 | -fno-strict-overflow) |
|
|
81 | gcc-specs-nostrict || continue |
|
|
82 | is-flagq -fstrict-overflow || append-flags $(test-flags -fstrict-overflow);; |
|
|
83 | esac |
|
|
84 | done |
|
|
85 | } |
|
|
86 | |
|
|
87 | # Remove occurrences of strings from variable given in $1 |
|
|
88 | # Strings removed are matched as globs, so for example |
|
|
89 | # '-O*' would remove -O1, -O2 etc. |
|
|
90 | _filter-var() { |
|
|
91 | local f x var=$1 new=() |
|
|
92 | shift |
|
|
93 | |
|
|
94 | for f in ${!var} ; do |
|
|
95 | for x in "$@" ; do |
|
|
96 | # Note this should work with globs like -O* |
|
|
97 | [[ ${f} == ${x} ]] && continue 2 |
|
|
98 | done |
|
|
99 | new+=( "${f}" ) |
|
|
100 | done |
|
|
101 | eval export ${var}=\""${new[*]}"\" |
|
|
102 | } |
|
|
103 | |
|
|
104 | # @FUNCTION: filter-flags |
|
|
105 | # @USAGE: <flags> |
|
|
106 | # @DESCRIPTION: |
|
|
107 | # Remove particular <flags> from {C,CPP,CXX,CCAS,F,FC,LD}FLAGS. Accepts shell globs. |
122 | filter-flags() { |
108 | filter-flags() { |
123 | local x f fset |
109 | _filter-hardened "$@" |
124 | declare -a new_CFLAGS new_CXXFLAGS |
110 | local v |
|
|
111 | for v in $(all-flag-vars) ; do |
|
|
112 | _filter-var ${v} "$@" |
|
|
113 | done |
|
|
114 | return 0 |
|
|
115 | } |
125 | |
116 | |
|
|
117 | # @FUNCTION: filter-lfs-flags |
|
|
118 | # @DESCRIPTION: |
|
|
119 | # Remove flags that enable Large File Support. |
|
|
120 | filter-lfs-flags() { |
|
|
121 | [[ $# -ne 0 ]] && die "filter-lfs-flags takes no arguments" |
|
|
122 | # http://www.gnu.org/s/libc/manual/html_node/Feature-Test-Macros.html |
|
|
123 | # _LARGEFILE_SOURCE: enable support for new LFS funcs (ftello/etc...) |
|
|
124 | # _LARGEFILE64_SOURCE: enable support for 64bit variants (off64_t/fseeko64/etc...) |
|
|
125 | # _FILE_OFFSET_BITS: default to 64bit variants (off_t is defined as off64_t) |
|
|
126 | filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE |
|
|
127 | } |
|
|
128 | |
|
|
129 | # @FUNCTION: filter-ldflags |
|
|
130 | # @USAGE: <flags> |
|
|
131 | # @DESCRIPTION: |
|
|
132 | # Remove particular <flags> from LDFLAGS. Accepts shell globs. |
|
|
133 | filter-ldflags() { |
|
|
134 | _filter-var LDFLAGS "$@" |
|
|
135 | return 0 |
|
|
136 | } |
|
|
137 | |
|
|
138 | # @FUNCTION: append-cppflags |
|
|
139 | # @USAGE: <flags> |
|
|
140 | # @DESCRIPTION: |
|
|
141 | # Add extra <flags> to the current CPPFLAGS. |
|
|
142 | append-cppflags() { |
|
|
143 | [[ $# -eq 0 ]] && return 0 |
|
|
144 | export CPPFLAGS="${CPPFLAGS} $*" |
|
|
145 | return 0 |
|
|
146 | } |
|
|
147 | |
|
|
148 | # @FUNCTION: append-cflags |
|
|
149 | # @USAGE: <flags> |
|
|
150 | # @DESCRIPTION: |
|
|
151 | # Add extra <flags> to the current CFLAGS. |
|
|
152 | append-cflags() { |
|
|
153 | [[ $# -eq 0 ]] && return 0 |
|
|
154 | export CFLAGS=$(test-flags-CC ${CFLAGS} "$@") |
|
|
155 | return 0 |
|
|
156 | } |
|
|
157 | |
|
|
158 | # @FUNCTION: append-cxxflags |
|
|
159 | # @USAGE: <flags> |
|
|
160 | # @DESCRIPTION: |
|
|
161 | # Add extra <flags> to the current CXXFLAGS. |
|
|
162 | append-cxxflags() { |
|
|
163 | [[ $# -eq 0 ]] && return 0 |
|
|
164 | export CXXFLAGS=$(test-flags-CXX ${CXXFLAGS} "$@") |
|
|
165 | return 0 |
|
|
166 | } |
|
|
167 | |
|
|
168 | # @FUNCTION: append-fflags |
|
|
169 | # @USAGE: <flags> |
|
|
170 | # @DESCRIPTION: |
|
|
171 | # Add extra <flags> to the current {F,FC}FLAGS. |
|
|
172 | append-fflags() { |
|
|
173 | [[ $# -eq 0 ]] && return 0 |
|
|
174 | export FFLAGS=$(test-flags-F77 ${FFLAGS} "$@") |
|
|
175 | export FCFLAGS=$(test-flags-FC ${FCFLAGS} "$@") |
|
|
176 | return 0 |
|
|
177 | } |
|
|
178 | |
|
|
179 | # @FUNCTION: append-lfs-flags |
|
|
180 | # @DESCRIPTION: |
|
|
181 | # Add flags that enable Large File Support. |
|
|
182 | append-lfs-flags() { |
|
|
183 | [[ $# -ne 0 ]] && die "append-lfs-flags takes no arguments" |
|
|
184 | # see comments in filter-lfs-flags func for meaning of these |
|
|
185 | append-cppflags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE |
|
|
186 | } |
|
|
187 | |
|
|
188 | # @FUNCTION: append-ldflags |
|
|
189 | # @USAGE: <flags> |
|
|
190 | # @DESCRIPTION: |
|
|
191 | # Add extra <flags> to the current LDFLAGS. |
|
|
192 | append-ldflags() { |
|
|
193 | [[ $# -eq 0 ]] && return 0 |
|
|
194 | local flag |
126 | for x in "$@" ; do |
195 | for flag in "$@"; do |
|
|
196 | [[ ${flag} == -l* ]] && \ |
|
|
197 | ewarn "Appending a library link instruction (${flag}); libraries to link to should not be passed through LDFLAGS" |
|
|
198 | done |
|
|
199 | |
|
|
200 | export LDFLAGS="${LDFLAGS} $*" |
|
|
201 | return 0 |
|
|
202 | } |
|
|
203 | |
|
|
204 | # @FUNCTION: append-flags |
|
|
205 | # @USAGE: <flags> |
|
|
206 | # @DESCRIPTION: |
|
|
207 | # Add extra <flags> to your current {C,CXX,F,FC}FLAGS. |
|
|
208 | append-flags() { |
|
|
209 | [[ $# -eq 0 ]] && return 0 |
127 | case "${x}" in |
210 | case " $* " in |
128 | -fPIC|-fpic|-fPIE|-fpie|-pie) |
211 | *' '-[DIU]*) eqawarn 'please use append-cppflags for preprocessor flags' ;; |
129 | append-flags `test_flag -fno-pie`;; |
212 | *' '-L*|\ |
130 | -fstack-protector|-fstack-protector-all) |
213 | *' '-Wl,*) eqawarn 'please use append-ldflags for linker flags' ;; |
131 | fstack-flags;; |
|
|
132 | esac |
214 | esac |
133 | done |
215 | append-cflags "$@" |
|
|
216 | append-cxxflags "$@" |
|
|
217 | append-fflags "$@" |
|
|
218 | return 0 |
|
|
219 | } |
134 | |
220 | |
135 | for fset in CFLAGS CXXFLAGS; do |
221 | # @FUNCTION: replace-flags |
|
|
222 | # @USAGE: <old> <new> |
|
|
223 | # @DESCRIPTION: |
|
|
224 | # Replace the <old> flag with <new>. Accepts shell globs for <old>. |
|
|
225 | replace-flags() { |
|
|
226 | [[ $# != 2 ]] && die "Usage: replace-flags <old flag> <new flag>" |
|
|
227 | |
|
|
228 | local f var new |
|
|
229 | for var in $(all-flag-vars) ; do |
136 | # Looping over the flags instead of using a global |
230 | # Looping over the flags instead of using a global |
137 | # substitution ensures that we're working with flag atoms. |
231 | # substitution ensures that we're working with flag atoms. |
138 | # Otherwise globs like -O* have the potential to wipe out the |
232 | # Otherwise globs like -O* have the potential to wipe out the |
139 | # list of flags. |
233 | # list of flags. |
|
|
234 | new=() |
140 | for f in ${!fset}; do |
235 | for f in ${!var} ; do |
141 | for x in "$@"; do |
|
|
142 | # Note this should work with globs like -O* |
|
|
143 | [[ ${f} == ${x} ]] && continue 2 |
|
|
144 | done |
|
|
145 | eval new_${fset}\[\${\#new_${fset}\[@]}]=\${f} |
|
|
146 | done |
|
|
147 | eval export ${fset}=\${new_${fset}\[*]} |
|
|
148 | done |
|
|
149 | |
|
|
150 | return 0 |
|
|
151 | } |
|
|
152 | |
|
|
153 | filter-lfs-flags() { |
|
|
154 | [[ -n $@ ]] && die "filter-lfs-flags takes no arguments" |
|
|
155 | filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE |
|
|
156 | } |
|
|
157 | |
|
|
158 | append-lfs-flags() { |
|
|
159 | [[ -n $@ ]] && die "append-lfs-flags takes no arguments" |
|
|
160 | append-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE |
|
|
161 | } |
|
|
162 | |
|
|
163 | append-flags() { |
|
|
164 | [[ -z $* ]] && return 0 |
|
|
165 | export CFLAGS="${CFLAGS} $*" |
|
|
166 | export CXXFLAGS="${CXXFLAGS} $*" |
|
|
167 | [ -n "`is-flag -fno-stack-protector`" -o \ |
|
|
168 | -n "`is-flag -fno-stack-protector-all`" ] && fstack-flags |
|
|
169 | return 0 |
|
|
170 | } |
|
|
171 | |
|
|
172 | replace-flags() { |
|
|
173 | [[ $# != 2 ]] \ |
|
|
174 | && echo && eerror "Usage: replace-flags <old flag> <new flag>" \ |
|
|
175 | && die "replace-flags takes 2 arguments, not $#" |
|
|
176 | |
|
|
177 | local f fset |
|
|
178 | declare -a new_CFLAGS new_CXXFLAGS |
|
|
179 | |
|
|
180 | for fset in CFLAGS CXXFLAGS; do |
|
|
181 | # Looping over the flags instead of using a global |
|
|
182 | # substitution ensures that we're working with flag atoms. |
|
|
183 | # Otherwise globs like -O* have the potential to wipe out the |
|
|
184 | # list of flags. |
|
|
185 | for f in ${!fset}; do |
|
|
186 | # Note this should work with globs like -O* |
236 | # Note this should work with globs like -O* |
187 | [[ ${f} == ${1} ]] && f=${2} |
237 | [[ ${f} == ${1} ]] && f=${2} |
188 | eval new_${fset}\[\${\#new_${fset}\[@]}]=\${f} |
238 | new+=( "${f}" ) |
189 | done |
|
|
190 | eval export ${fset}=\${new_${fset}\[*]} |
|
|
191 | done |
239 | done |
|
|
240 | eval export ${var}=\""${new[*]}"\" |
|
|
241 | done |
192 | |
242 | |
193 | return 0 |
243 | return 0 |
194 | } |
244 | } |
195 | |
245 | |
|
|
246 | # @FUNCTION: replace-cpu-flags |
|
|
247 | # @USAGE: <old> <new> |
|
|
248 | # @DESCRIPTION: |
|
|
249 | # Replace cpu flags (like -march/-mcpu/-mtune) that select the <old> cpu |
|
|
250 | # with flags that select the <new> cpu. Accepts shell globs for <old>. |
196 | replace-cpu-flags() { |
251 | replace-cpu-flags() { |
197 | local newcpu="$#" ; newcpu="${!newcpu}" |
252 | local newcpu="$#" ; newcpu="${!newcpu}" |
198 | while [ $# -gt 1 ] ; do |
253 | while [ $# -gt 1 ] ; do |
199 | # quote to make sure that no globbing is done (particularly on |
254 | # quote to make sure that no globbing is done (particularly on |
200 | # ${oldcpu} prior to calling replace-flags |
255 | # ${oldcpu}) prior to calling replace-flags |
201 | replace-flags "-march=${1}" "-march=${newcpu}" |
256 | replace-flags "-march=${1}" "-march=${newcpu}" |
202 | replace-flags "-mcpu=${1}" "-mcpu=${newcpu}" |
257 | replace-flags "-mcpu=${1}" "-mcpu=${newcpu}" |
203 | replace-flags "-mtune=${1}" "-mtune=${newcpu}" |
258 | replace-flags "-mtune=${1}" "-mtune=${newcpu}" |
204 | shift |
259 | shift |
205 | done |
260 | done |
206 | return 0 |
261 | return 0 |
207 | } |
262 | } |
208 | |
263 | |
|
|
264 | _is_flagq() { |
|
|
265 | local x |
|
|
266 | for x in ${!1} ; do |
|
|
267 | [[ ${x} == $2 ]] && return 0 |
|
|
268 | done |
|
|
269 | return 1 |
|
|
270 | } |
|
|
271 | |
|
|
272 | # @FUNCTION: is-flagq |
|
|
273 | # @USAGE: <flag> |
|
|
274 | # @DESCRIPTION: |
|
|
275 | # Returns shell true if <flag> is in {C,CXX,F,FC}FLAGS, else returns shell false. Accepts shell globs. |
|
|
276 | is-flagq() { |
|
|
277 | [[ -n $2 ]] && die "Usage: is-flag <flag>" |
|
|
278 | |
|
|
279 | local var |
|
|
280 | for var in $(all-flag-vars) ; do |
|
|
281 | _is_flagq ${var} "$1" && return 0 |
|
|
282 | done |
|
|
283 | return 1 |
|
|
284 | } |
|
|
285 | |
|
|
286 | # @FUNCTION: is-flag |
|
|
287 | # @USAGE: <flag> |
|
|
288 | # @DESCRIPTION: |
|
|
289 | # Echo's "true" if flag is set in {C,CXX,F,FC}FLAGS. Accepts shell globs. |
209 | is-flag() { |
290 | is-flag() { |
210 | local x |
291 | is-flagq "$@" && echo true |
211 | |
|
|
212 | for x in ${CFLAGS} ${CXXFLAGS} ; do |
|
|
213 | # Note this should work with globs like -mcpu=ultrasparc* |
|
|
214 | if [[ ${x} == ${1} ]]; then |
|
|
215 | echo true |
|
|
216 | return 0 |
|
|
217 | fi |
|
|
218 | done |
|
|
219 | return 1 |
|
|
220 | } |
292 | } |
221 | |
293 | |
|
|
294 | # @FUNCTION: is-ldflagq |
|
|
295 | # @USAGE: <flag> |
|
|
296 | # @DESCRIPTION: |
|
|
297 | # Returns shell true if <flag> is in LDFLAGS, else returns shell false. Accepts shell globs. |
|
|
298 | is-ldflagq() { |
|
|
299 | [[ -n $2 ]] && die "Usage: is-ldflag <flag>" |
|
|
300 | _is_flagq LDFLAGS $1 |
|
|
301 | } |
|
|
302 | |
|
|
303 | # @FUNCTION: is-ldflag |
|
|
304 | # @USAGE: <flag> |
|
|
305 | # @DESCRIPTION: |
|
|
306 | # Echo's "true" if flag is set in LDFLAGS. Accepts shell globs. |
|
|
307 | is-ldflag() { |
|
|
308 | is-ldflagq "$@" && echo true |
|
|
309 | } |
|
|
310 | |
|
|
311 | # @FUNCTION: filter-mfpmath |
|
|
312 | # @USAGE: <math types> |
|
|
313 | # @DESCRIPTION: |
|
|
314 | # Remove specified math types from the fpmath flag. For example, if the user |
|
|
315 | # has -mfpmath=sse,386, running `filter-mfpmath sse` will leave the user with |
|
|
316 | # -mfpmath=386. |
222 | filter-mfpmath() { |
317 | filter-mfpmath() { |
223 | local orig_mfpmath new_math prune_math |
318 | local orig_mfpmath new_math prune_math |
224 | |
319 | |
225 | # save the original -mfpmath flag |
320 | # save the original -mfpmath flag |
226 | orig_mfpmath="`get-flag -mfpmath`" |
321 | orig_mfpmath=$(get-flag -mfpmath) |
227 | # get the value of the current -mfpmath flag |
322 | # get the value of the current -mfpmath flag |
228 | new_math=" `get-flag mfpmath | tr , ' '` " |
323 | new_math=$(get-flag mfpmath) |
|
|
324 | new_math=" ${new_math//,/ } " |
229 | # figure out which math values are to be removed |
325 | # figure out which math values are to be removed |
230 | prune_math="" |
326 | prune_math="" |
231 | for prune_math in "$@" ; do |
327 | for prune_math in "$@" ; do |
232 | new_math="${new_math/ ${prune_math} / }" |
328 | new_math=${new_math/ ${prune_math} / } |
233 | done |
329 | done |
234 | new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`" |
330 | new_math=$(echo ${new_math}) |
|
|
331 | new_math=${new_math// /,} |
235 | |
332 | |
236 | if [ -z "${new_math}" ] ; then |
333 | if [[ -z ${new_math} ]] ; then |
237 | # if we're removing all user specified math values are |
334 | # if we're removing all user specified math values are |
238 | # slated for removal, then we just filter the flag |
335 | # slated for removal, then we just filter the flag |
239 | filter-flags ${orig_mfpmath} |
336 | filter-flags ${orig_mfpmath} |
240 | else |
337 | else |
241 | # if we only want to filter some of the user specified |
338 | # if we only want to filter some of the user specified |
… | |
… | |
243 | replace-flags ${orig_mfpmath} -mfpmath=${new_math} |
340 | replace-flags ${orig_mfpmath} -mfpmath=${new_math} |
244 | fi |
341 | fi |
245 | return 0 |
342 | return 0 |
246 | } |
343 | } |
247 | |
344 | |
|
|
345 | # @FUNCTION: strip-flags |
|
|
346 | # @DESCRIPTION: |
|
|
347 | # Strip C[XX]FLAGS of everything except known good/safe flags. |
248 | strip-flags() { |
348 | strip-flags() { |
249 | local x y flag NEW_CFLAGS NEW_CXXFLAGS |
349 | local x y var |
250 | |
350 | |
251 | setup-allowed-flags |
351 | setup-allowed-flags |
252 | |
352 | |
253 | local NEW_CFLAGS="" |
|
|
254 | local NEW_CXXFLAGS="" |
|
|
255 | |
|
|
256 | # Allow unstable C[XX]FLAGS if we are using unstable profile ... |
|
|
257 | if has ~$(tc-arch) ${ACCEPT_KEYWORDS} ; then |
|
|
258 | ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}" |
|
|
259 | fi |
|
|
260 | |
|
|
261 | set -f # disable pathname expansion |
353 | set -f # disable pathname expansion |
262 | |
354 | |
263 | for x in ${CFLAGS}; do |
355 | for var in $(all-flag-vars) ; do |
|
|
356 | local new=() |
|
|
357 | |
|
|
358 | for x in ${!var} ; do |
|
|
359 | local flag=${x%%=*} |
264 | for y in ${ALLOWED_FLAGS}; do |
360 | for y in ${ALLOWED_FLAGS} ; do |
265 | flag=${x%%=*} |
|
|
266 | if [ "${flag%%${y}}" = "" ] ; then |
361 | if [[ -z ${flag%%${y}} ]] ; then |
267 | NEW_CFLAGS="${NEW_CFLAGS} ${x}" |
362 | new+=( "${x}" ) |
268 | break |
363 | break |
269 | fi |
364 | fi |
270 | done |
365 | done |
271 | done |
366 | done |
272 | |
367 | |
273 | for x in ${CXXFLAGS}; do |
|
|
274 | for y in ${ALLOWED_FLAGS}; do |
|
|
275 | flag=${x%%=*} |
|
|
276 | if [ "${flag%%${y}}" = "" ] ; then |
|
|
277 | NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}" |
|
|
278 | break |
|
|
279 | fi |
|
|
280 | done |
|
|
281 | done |
|
|
282 | |
|
|
283 | # In case we filtered out all optimization flags fallback to -O2 |
368 | # In case we filtered out all optimization flags fallback to -O2 |
284 | if [ "${CFLAGS/-O}" != "${CFLAGS}" -a "${NEW_CFLAGS/-O}" = "${NEW_CFLAGS}" ]; then |
369 | if _is_flagq ${var} "-O*" && ! _is_flagq new "-O*" ; then |
285 | NEW_CFLAGS="${NEW_CFLAGS} -O2" |
370 | new+=( -O2 ) |
286 | fi |
371 | fi |
287 | if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then |
372 | |
288 | NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2" |
373 | eval export ${var}=\""${new[*]}"\" |
289 | fi |
374 | done |
290 | |
375 | |
291 | set +f # re-enable pathname expansion |
376 | set +f # re-enable pathname expansion |
292 | |
377 | |
293 | export CFLAGS="${NEW_CFLAGS}" |
|
|
294 | export CXXFLAGS="${NEW_CXXFLAGS}" |
|
|
295 | return 0 |
378 | return 0 |
296 | } |
379 | } |
297 | |
380 | |
298 | test-flag-PROG() { |
381 | test-flag-PROG() { |
299 | local comp=$1 |
382 | local comp=$1 |
300 | local flags="$2" |
383 | local flag=$2 |
301 | |
384 | |
302 | [[ -z ${comp} || -z ${flags} ]] && \ |
385 | [[ -z ${comp} || -z ${flag} ]] && return 1 |
303 | return 1 |
|
|
304 | |
386 | |
|
|
387 | # use -c so we can test the assembler as well |
305 | local PROG=$(tc-get${comp}) |
388 | local PROG=$(tc-get${comp}) |
306 | ${PROG} ${flags} -S -o /dev/null -xc /dev/null \ |
389 | ${PROG} "${flag}" -c -o /dev/null -xc /dev/null \ |
307 | > /dev/null 2>&1 |
390 | > /dev/null 2>&1 |
308 | } |
391 | } |
309 | |
392 | |
310 | # Returns true if C compiler support given flag |
393 | # @FUNCTION: test-flag-CC |
|
|
394 | # @USAGE: <flag> |
|
|
395 | # @DESCRIPTION: |
|
|
396 | # Returns shell true if <flag> is supported by the C compiler, else returns shell false. |
311 | test-flag-CC() { test-flag-PROG "CC" "$1"; } |
397 | test-flag-CC() { test-flag-PROG "CC" "$1"; } |
312 | |
398 | |
313 | # Returns true if C++ compiler support given flag |
399 | # @FUNCTION: test-flag-CXX |
|
|
400 | # @USAGE: <flag> |
|
|
401 | # @DESCRIPTION: |
|
|
402 | # Returns shell true if <flag> is supported by the C++ compiler, else returns shell false. |
314 | test-flag-CXX() { test-flag-PROG "CXX" "$1"; } |
403 | test-flag-CXX() { test-flag-PROG "CXX" "$1"; } |
|
|
404 | |
|
|
405 | # @FUNCTION: test-flag-F77 |
|
|
406 | # @USAGE: <flag> |
|
|
407 | # @DESCRIPTION: |
|
|
408 | # Returns shell true if <flag> is supported by the Fortran 77 compiler, else returns shell false. |
|
|
409 | test-flag-F77() { test-flag-PROG "F77" "$1"; } |
|
|
410 | |
|
|
411 | # @FUNCTION: test-flag-FC |
|
|
412 | # @USAGE: <flag> |
|
|
413 | # @DESCRIPTION: |
|
|
414 | # Returns shell true if <flag> is supported by the Fortran 90 compiler, else returns shell false. |
|
|
415 | test-flag-FC() { test-flag-PROG "FC" "$1"; } |
315 | |
416 | |
316 | test-flags-PROG() { |
417 | test-flags-PROG() { |
317 | local comp=$1 |
418 | local comp=$1 |
318 | local flags |
419 | local flags |
319 | local x |
420 | local x |
320 | |
421 | |
321 | shift |
422 | shift |
322 | |
423 | |
323 | [[ -z ${comp} ]] && \ |
424 | [[ -z ${comp} ]] && return 1 |
324 | return 1 |
|
|
325 | |
425 | |
326 | for x in "$@" ; do |
426 | for x in "$@" ; do |
327 | test-flag-${comp} "${x}" && flags="${flags} ${x}" |
427 | test-flag-${comp} "${x}" && flags="${flags}${flags:+ }${x}" |
328 | done |
428 | done |
329 | |
429 | |
330 | echo "${flags}" |
430 | echo "${flags}" |
331 | |
431 | |
332 | # Just bail if we dont have any flags |
432 | # Just bail if we dont have any flags |
333 | [[ -n ${flags} ]] |
433 | [[ -n ${flags} ]] |
334 | } |
434 | } |
335 | |
435 | |
336 | # Returns (echo's) the given flags supported by the C compiler |
436 | # @FUNCTION: test-flags-CC |
|
|
437 | # @USAGE: <flags> |
|
|
438 | # @DESCRIPTION: |
|
|
439 | # Returns shell true if <flags> are supported by the C compiler, else returns shell false. |
337 | test-flags-CC() { test-flags-PROG "CC" "$@"; } |
440 | test-flags-CC() { test-flags-PROG "CC" "$@"; } |
338 | |
441 | |
339 | # Returns (echo's) the given flags supported by the C++ compiler |
442 | # @FUNCTION: test-flags-CXX |
|
|
443 | # @USAGE: <flags> |
|
|
444 | # @DESCRIPTION: |
|
|
445 | # Returns shell true if <flags> are supported by the C++ compiler, else returns shell false. |
340 | test-flags-CXX() { test-flags-PROG "CXX" "$@"; } |
446 | test-flags-CXX() { test-flags-PROG "CXX" "$@"; } |
341 | |
447 | |
|
|
448 | # @FUNCTION: test-flags-F77 |
|
|
449 | # @USAGE: <flags> |
|
|
450 | # @DESCRIPTION: |
|
|
451 | # Returns shell true if <flags> are supported by the Fortran 77 compiler, else returns shell false. |
|
|
452 | test-flags-F77() { test-flags-PROG "F77" "$@"; } |
|
|
453 | |
|
|
454 | # @FUNCTION: test-flags-FC |
|
|
455 | # @USAGE: <flags> |
|
|
456 | # @DESCRIPTION: |
|
|
457 | # Returns shell true if <flags> are supported by the Fortran 90 compiler, else returns shell false. |
|
|
458 | test-flags-FC() { test-flags-PROG "FC" "$@"; } |
|
|
459 | |
|
|
460 | # @FUNCTION: test-flags |
|
|
461 | # @USAGE: <flags> |
|
|
462 | # @DESCRIPTION: |
342 | # Short-hand that should hopefully work for both C and C++ compiler, but |
463 | # Short-hand that should hopefully work for both C and C++ compiler, but |
343 | # its really only present due to the append-flags() abomination. |
464 | # its really only present due to the append-flags() abomination. |
344 | test-flags() { test-flags-CC "$@"; } |
465 | test-flags() { test-flags-CC "$@"; } |
345 | |
466 | |
346 | # Deprecated, use test-flags() |
467 | # @FUNCTION: test_version_info |
347 | test_flag() { |
468 | # @USAGE: <version> |
348 | ewarn "test_flag: deprecated, please use test-flags()!" >&2 |
469 | # @DESCRIPTION: |
349 | |
470 | # Returns shell true if the current C compiler version matches <version>, else returns shell false. |
350 | test-flags-CC "$@" |
471 | # Accepts shell globs. |
351 | } |
|
|
352 | |
|
|
353 | test_version_info() { |
472 | test_version_info() { |
354 | if [[ $($(tc-getCC) --version 2>&1) == *$1* ]]; then |
473 | if [[ $($(tc-getCC) --version 2>&1) == *$1* ]]; then |
355 | return 0 |
474 | return 0 |
356 | else |
475 | else |
357 | return 1 |
476 | return 1 |
358 | fi |
477 | fi |
359 | } |
478 | } |
360 | |
479 | |
|
|
480 | # @FUNCTION: strip-unsupported-flags |
|
|
481 | # @DESCRIPTION: |
|
|
482 | # Strip {C,CXX,F,FC}FLAGS of any flags not supported by the active toolchain. |
361 | strip-unsupported-flags() { |
483 | strip-unsupported-flags() { |
362 | local x NEW_CFLAGS NEW_CXXFLAGS |
484 | export CFLAGS=$(test-flags-CC ${CFLAGS}) |
363 | |
485 | export CXXFLAGS=$(test-flags-CXX ${CXXFLAGS}) |
364 | for x in ${CFLAGS} ; do |
486 | export FFLAGS=$(test-flags-F77 ${FFLAGS}) |
365 | NEW_CFLAGS="${NEW_CFLAGS} $(test-flags ${x})" |
487 | export FCFLAGS=$(test-flags-FC ${FCFLAGS}) |
366 | done |
|
|
367 | for x in ${CXXFLAGS} ; do |
|
|
368 | NEW_CXXFLAGS="${NEW_CXXFLAGS} $(test-flags ${x})" |
|
|
369 | done |
|
|
370 | |
|
|
371 | export CFLAGS=${NEW_CFLAGS} |
|
|
372 | export CXXFLAGS=${NEW_CXXFLAGS} |
|
|
373 | } |
488 | } |
374 | |
489 | |
|
|
490 | # @FUNCTION: get-flag |
|
|
491 | # @USAGE: <flag> |
|
|
492 | # @DESCRIPTION: |
|
|
493 | # Find and echo the value for a particular flag. Accepts shell globs. |
375 | get-flag() { |
494 | get-flag() { |
376 | local f findflag="$1" |
495 | local f var findflag="$1" |
377 | |
496 | |
378 | # this code looks a little flaky but seems to work for |
497 | # this code looks a little flaky but seems to work for |
379 | # everything we want ... |
498 | # everything we want ... |
380 | # for example, if CFLAGS="-march=i686": |
499 | # for example, if CFLAGS="-march=i686": |
381 | # `get-flag -march` == "-march=i686" |
500 | # `get-flag -march` == "-march=i686" |
382 | # `get-flag march` == "i686" |
501 | # `get-flag march` == "i686" |
383 | for f in ${CFLAGS} ${CXXFLAGS} ; do |
502 | for var in $(all-flag-vars) ; do |
|
|
503 | for f in ${!var} ; do |
384 | if [ "${f/${findflag}}" != "${f}" ] ; then |
504 | if [ "${f/${findflag}}" != "${f}" ] ; then |
385 | printf "%s\n" "${f/-${findflag}=}" |
505 | printf "%s\n" "${f/-${findflag}=}" |
386 | return 0 |
506 | return 0 |
387 | fi |
507 | fi |
|
|
508 | done |
388 | done |
509 | done |
389 | return 1 |
510 | return 1 |
390 | } |
511 | } |
391 | |
512 | |
392 | # DEPRECATED - use gcc-specs-relro or gcc-specs-now from toolchain-funcs |
513 | # @FUNCTION: has_m64 |
393 | has_hardened() { |
514 | # @DESCRIPTION: |
394 | ewarn "has_hardened: deprecated, please use gcc-specs-{relro,now}()!" >&2 |
515 | # This doesn't test if the flag is accepted, it tests if the flag actually |
395 | |
516 | # WORKS. Non-multilib gcc will take both -m32 and -m64. If the flag works |
396 | test_version_info Hardened && return 0 |
517 | # return code is 0, else the return code is 1. |
397 | # The specs file wont exist unless gcc has GCC_SPECS support |
|
|
398 | [[ -f ${GCC_SPECS} && ${GCC_SPECS} != ${GCC_SPECS/hardened/} ]] |
|
|
399 | } |
|
|
400 | |
|
|
401 | # DEPRECATED - use gcc-specs-pie from toolchain-funcs |
|
|
402 | # indicate whether PIC is set |
|
|
403 | has_pic() { |
|
|
404 | ewarn "has_pic: deprecated, please use gcc-specs-pie()!" >&2 |
|
|
405 | |
|
|
406 | [[ ${CFLAGS/-fPIC} != ${CFLAGS} || \ |
|
|
407 | ${CFLAGS/-fpic} != ${CFLAGS} || \ |
|
|
408 | -n $(echo | $(tc-getCC) ${CFLAGS} -E -dM - | grep __PIC__) ]] |
|
|
409 | } |
|
|
410 | |
|
|
411 | # DEPRECATED - use gcc-specs-pie from toolchain-funcs |
|
|
412 | # indicate whether PIE is set |
|
|
413 | has_pie() { |
|
|
414 | ewarn "has_pie: deprecated, please use gcc-specs-pie()!" >&2 |
|
|
415 | |
|
|
416 | [[ ${CFLAGS/-fPIE} != ${CFLAGS} || \ |
|
|
417 | ${CFLAGS/-fpie} != ${CFLAGS} || \ |
|
|
418 | -n $(echo | $(tc-getCC) ${CFLAGS} -E -dM - | grep __PIE__) || \ |
|
|
419 | -n $(echo | $(tc-getCC) ${CFLAGS} -E -dM - | grep __PIC__) ]] |
|
|
420 | # test PIC while waiting for specs to be updated to generate __PIE__ |
|
|
421 | } |
|
|
422 | |
|
|
423 | # DEPRECATED - use gcc-specs-ssp from toolchain-funcs |
|
|
424 | # indicate whether code for SSP is being generated for all functions |
|
|
425 | has_ssp_all() { |
|
|
426 | ewarn "has_ssp_all: deprecated, please use gcc-specs-ssp()!" >&2 |
|
|
427 | |
|
|
428 | # note; this matches only -fstack-protector-all |
|
|
429 | [[ ${CFLAGS/-fstack-protector-all} != ${CFLAGS} || \ |
|
|
430 | -n $(echo | $(tc-getCC) ${CFLAGS} -E -dM - | grep __SSP_ALL__) ]] || \ |
|
|
431 | gcc-specs-ssp |
|
|
432 | } |
|
|
433 | |
|
|
434 | # DEPRECATED - use gcc-specs-ssp from toolchain-funcs |
|
|
435 | # indicate whether code for SSP is being generated |
|
|
436 | has_ssp() { |
|
|
437 | ewarn "has_ssp: deprecated, please use gcc-specs-ssp()!" >&2 |
|
|
438 | |
|
|
439 | # note; this matches both -fstack-protector and -fstack-protector-all |
|
|
440 | [[ ${CFLAGS/-fstack-protector} != ${CFLAGS} || \ |
|
|
441 | -n $(echo | $(tc-getCC) ${CFLAGS} -E -dM - | grep __SSP__) ]] || \ |
|
|
442 | gcc-specs-ssp |
|
|
443 | } |
|
|
444 | |
|
|
445 | has_m64() { |
518 | has_m64() { |
|
|
519 | eqawarn "${FUNCNAME}: don't use this anymore" |
|
|
520 | |
446 | # this doesnt test if the flag is accepted, it tests if the flag |
521 | # this doesnt test if the flag is accepted, it tests if the flag |
447 | # actually -WORKS-. non-multilib gcc will take both -m32 and -m64! |
522 | # actually -WORKS-. non-multilib gcc will take both -m32 and -m64! |
448 | # please dont replace this function with test_flag in some future |
523 | # please dont replace this function with test_flag in some future |
449 | # clean-up! |
524 | # clean-up! |
450 | |
525 | |
… | |
… | |
457 | [[ ${ret} != 1 ]] && return 0 |
532 | [[ ${ret} != 1 ]] && return 0 |
458 | return 1 |
533 | return 1 |
459 | } |
534 | } |
460 | |
535 | |
461 | has_m32() { |
536 | has_m32() { |
462 | # this doesnt test if the flag is accepted, it tests if the flag |
537 | die "${FUNCNAME}: don't use this anymore" |
463 | # actually -WORKS-. non-multilib gcc will take both -m32 and -m64! |
|
|
464 | # please dont replace this function with test_flag in some future |
|
|
465 | # clean-up! |
|
|
466 | |
|
|
467 | [ "$(tc-arch)" = "amd64" ] && has_multilib_profile && return 0 |
|
|
468 | |
|
|
469 | local temp=$(emktemp) |
|
|
470 | echo "int main() { return(0); }" > "${temp}".c |
|
|
471 | MY_CC=$(tc-getCC) |
|
|
472 | ${MY_CC/ .*/} -m32 -o "$(emktemp)" "${temp}".c > /dev/null 2>&1 |
|
|
473 | local ret=$? |
|
|
474 | rm -f "${temp}".c |
|
|
475 | [[ ${ret} != 1 ]] && return 0 |
|
|
476 | return 1 |
|
|
477 | } |
538 | } |
478 | |
539 | |
|
|
540 | # @FUNCTION: replace-sparc64-flags |
|
|
541 | # @DESCRIPTION: |
|
|
542 | # Sets mcpu to v8 and uses the original value as mtune if none specified. |
479 | replace-sparc64-flags() { |
543 | replace-sparc64-flags() { |
480 | local SPARC64_CPUS="ultrasparc3 ultrasparc v9" |
544 | local SPARC64_CPUS="ultrasparc3 ultrasparc v9" |
481 | |
545 | |
482 | if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then |
546 | if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then |
483 | for x in ${SPARC64_CPUS}; do |
547 | for x in ${SPARC64_CPUS}; do |
484 | CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}" |
548 | CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}" |
485 | done |
549 | done |
486 | else |
550 | else |
487 | for x in ${SPARC64_CPUS}; do |
551 | for x in ${SPARC64_CPUS}; do |
488 | CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" |
552 | CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" |
489 | done |
553 | done |
490 | fi |
554 | fi |
491 | |
555 | |
492 | if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then |
556 | if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then |
493 | for x in ${SPARC64_CPUS}; do |
557 | for x in ${SPARC64_CPUS}; do |
494 | CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}" |
558 | CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}" |
495 | done |
559 | done |
496 | else |
560 | else |
497 | for x in ${SPARC64_CPUS}; do |
561 | for x in ${SPARC64_CPUS}; do |
498 | CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" |
562 | CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" |
499 | done |
563 | done |
500 | fi |
564 | fi |
501 | |
565 | |
502 | export CFLAGS CXXFLAGS |
566 | export CFLAGS CXXFLAGS |
503 | } |
567 | } |
504 | |
568 | |
|
|
569 | # @FUNCTION: append-libs |
|
|
570 | # @USAGE: <libs> |
|
|
571 | # @DESCRIPTION: |
|
|
572 | # Add extra <libs> to the current LIBS. |
505 | append-ldflags() { |
573 | append-libs() { |
506 | export LDFLAGS="${LDFLAGS} $*" |
574 | [[ $# -eq 0 ]] && return 0 |
507 | return 0 |
575 | local flag |
508 | } |
|
|
509 | |
|
|
510 | filter-ldflags() { |
|
|
511 | local x |
|
|
512 | |
|
|
513 | # we do this fancy spacing stuff so as to not filter |
|
|
514 | # out part of a flag ... we want flag atoms ! :D |
|
|
515 | LDFLAGS=" ${LDFLAGS} " |
|
|
516 | for x in "$@" ; do |
576 | for flag in "$@"; do |
517 | LDFLAGS=${LDFLAGS// ${x} / } |
577 | [[ ${flag} == -l* ]] && flag=${flag#-l} |
|
|
578 | export LIBS="${LIBS} -l${flag}" |
518 | done |
579 | done |
519 | [[ -z ${LDFLAGS// } ]] \ |
|
|
520 | && LDFLAGS="" \ |
|
|
521 | || LDFLAGS=${LDFLAGS:1:${#LDFLAGS}-2} |
|
|
522 | export LDFLAGS |
|
|
523 | return 0 |
|
|
524 | } |
|
|
525 | |
580 | |
|
|
581 | return 0 |
|
|
582 | } |
|
|
583 | |
|
|
584 | # @FUNCTION: raw-ldflags |
|
|
585 | # @USAGE: [flags] |
|
|
586 | # @DESCRIPTION: |
526 | # Turn C style ldflags (-Wl,-foo) into straight ldflags |
587 | # Turn C style ldflags (-Wl,-foo) into straight ldflags - the results |
|
|
588 | # are suitable for passing directly to 'ld'; note LDFLAGS is usually passed |
|
|
589 | # to gcc where it needs the '-Wl,'. |
|
|
590 | # |
|
|
591 | # If no flags are specified, then default to ${LDFLAGS}. |
527 | raw-ldflags() { |
592 | raw-ldflags() { |
528 | local x input="$@" |
593 | local x input="$@" |
529 | [[ -z ${input} ]] && input=${LDFLAGS} |
594 | [[ -z ${input} ]] && input=${LDFLAGS} |
530 | set -- |
595 | set -- |
531 | for x in ${input} ; do |
596 | for x in ${input} ; do |
… | |
… | |
533 | set -- "$@" ${x//,/ } |
598 | set -- "$@" ${x//,/ } |
534 | done |
599 | done |
535 | echo "$@" |
600 | echo "$@" |
536 | } |
601 | } |
537 | |
602 | |
538 | fstack-flags() { |
603 | # @FUNCTION: no-as-needed |
539 | if gcc-specs-ssp; then |
604 | # @RETURN: Flag to disable asneeded behavior for use with append-ldflags. |
540 | [ -z "`is-flag -fno-stack-protector`" ] && |
605 | no-as-needed() { |
541 | export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`" |
|
|
542 | fi |
|
|
543 | return 0 |
|
|
544 | } |
|
|
545 | |
|
|
546 | # This is thanks to great work from Paul de Vrieze <gentoo-user@devrieze.net>, |
|
|
547 | # bug #9016. Also thanks to Jukka Salmi <salmi@gmx.net> (bug #13907) for more |
|
|
548 | # fixes. |
|
|
549 | # |
|
|
550 | # Export CFLAGS and CXXFLAGS that are compadible with gcc-2.95.3 |
|
|
551 | gcc2-flags() { |
|
|
552 | if [[ $(tc-arch) == "x86" || $(tc-arch) == "amd64" ]] ; then |
|
|
553 | CFLAGS=${CFLAGS//-mtune=/-mcpu=} |
|
|
554 | CXXFLAGS=${CXXFLAGS//-mtune=/-mcpu=} |
|
|
555 | fi |
|
|
556 | |
|
|
557 | replace-cpu-flags k6-{2,3} k6 |
|
|
558 | replace-cpu-flags athlon{,-{tbird,4,xp,mp}} i686 |
|
|
559 | |
|
|
560 | replace-cpu-flags pentium-mmx i586 |
|
|
561 | replace-cpu-flags pentium{2,3,4} i686 |
|
|
562 | |
|
|
563 | replace-cpu-flags ev6{7,8} ev6 |
|
|
564 | |
|
|
565 | export CFLAGS CXXFLAGS |
|
|
566 | } |
|
|
567 | |
|
|
568 | # Gets the flags needed for "NOW" binding |
|
|
569 | bindnow-flags() { |
|
|
570 | case $($(tc-getLD) -v 2>&1 </dev/null) in |
606 | case $($(tc-getLD) -v 2>&1 </dev/null) in |
571 | *GNU* | *'with BFD'*) # GNU ld |
607 | *GNU*) # GNU ld |
572 | echo "-Wl,-z,now" ;; |
608 | echo "-Wl,--no-as-needed" ;; |
573 | *Apple*) # Darwin ld |
|
|
574 | echo "-bind_at_load" ;; |
|
|
575 | *) |
|
|
576 | # Some linkers just recognize -V instead of -v |
|
|
577 | case $($(tc-getLD) -V 2>&1 </dev/null) in |
|
|
578 | *Solaris*) # Solaris accept almost the same GNU options |
|
|
579 | echo "-Wl,-z,now" ;; |
|
|
580 | esac |
|
|
581 | ;; |
|
|
582 | esac |
609 | esac |
583 | } |
610 | } |
|
|
611 | |
|
|
612 | fi |