| 1 | # Copyright 1999-2002 Gentoo Technologies, Inc. |
1 | # Copyright 1999-2003 Gentoo Technologies, Inc. |
| 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.37 2004/02/21 07:19:29 vapier Exp $ |
|
|
4 | # |
| 3 | # Author Bart Verwilst <verwilst@gentoo.org> |
5 | # Author Bart Verwilst <verwilst@gentoo.org> |
| 4 | # /space/gentoo/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass |
6 | |
| 5 | ECLASS=flag-o-matic |
7 | ECLASS=flag-o-matic |
|
|
8 | INHERITED="$INHERITED $ECLASS" |
| 6 | |
9 | |
| 7 | # |
10 | # |
| 8 | #### filter-flags <flag> #### |
11 | #### filter-flags <flags> #### |
| 9 | # Remove particular flags from C[XX]FLAGS |
12 | # Remove particular flags from C[XX]FLAGS |
|
|
13 | # Matches only complete flags |
| 10 | # |
14 | # |
| 11 | #### append-flags <flag> #### |
15 | #### append-flags <flags> #### |
| 12 | # Add extra flags to your current C[XX]FLAGS |
16 | # Add extra flags to your current C[XX]FLAGS |
| 13 | # |
17 | # |
| 14 | #### replace-flags <orig.flag> <new.flag> ### |
18 | #### replace-flags <orig.flag> <new.flag> ### |
| 15 | # Replace a flag by another one |
19 | # Replace a flag by another one |
| 16 | # |
20 | # |
|
|
21 | #### replace-cpu-flags <new.cpu> <old.cpus> ### |
|
|
22 | # Replace march/mcpu flags that specify <old.cpus> |
|
|
23 | # with flags that specify <new.cpu> |
|
|
24 | # |
|
|
25 | #### is-flag <flag> #### |
|
|
26 | # Returns "true" if flag is set in C[XX]FLAGS |
|
|
27 | # Matches only complete a flag |
|
|
28 | # |
|
|
29 | #### strip-flags #### |
|
|
30 | # Strip C[XX]FLAGS of everything except known |
|
|
31 | # good options. |
|
|
32 | # |
|
|
33 | #### strip-unsupported-flags #### |
|
|
34 | # Strip C[XX]FLAGS of any flags not supported by |
|
|
35 | # installed version of gcc |
|
|
36 | # |
|
|
37 | #### get-flag <flag> #### |
|
|
38 | # Find and echo the value for a particular flag |
|
|
39 | # |
|
|
40 | #### replace-sparc64-flags #### |
|
|
41 | # Sets mcpu to v8 and uses the original value |
|
|
42 | # as mtune if none specified. |
|
|
43 | # |
|
|
44 | #### filter-mfpmath <math types> #### |
|
|
45 | # Remove specified math types from the fpmath specification |
|
|
46 | # If the user has -mfpmath=sse,386, running `filter-mfpmath sse` |
|
|
47 | # will leave the user with -mfpmath=386 |
|
|
48 | # |
|
|
49 | #### append-ldflags #### |
|
|
50 | # Add extra flags to your current LDFLAGS |
|
|
51 | # |
|
|
52 | #### etexec-flags #### |
|
|
53 | # hooked function for hardened-gcc that appends |
|
|
54 | # -yet_exec {C,CXX,LD}FLAGS when hardened-gcc is installed |
|
|
55 | # and a package is filtering -fPIC,-fpic, -fPIE, -fpie |
|
|
56 | # |
|
|
57 | #### fstack-flags #### |
|
|
58 | # hooked function for hardened-gcc that appends |
|
|
59 | # -yno_propolice to {C,CXX,LD}FLAGS when hardened-gcc is installed |
|
|
60 | # and a package is filtering -fstack-protector, -fstack-protector-all |
|
|
61 | # |
| 17 | |
62 | |
|
|
63 | # C[XX]FLAGS that we allow in strip-flags |
|
|
64 | setup-allowed-flags() { |
|
|
65 | export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -pipe -g" |
|
|
66 | case "${ARCH}" in |
|
|
67 | mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; |
|
|
68 | amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
|
|
69 | alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
|
|
70 | ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
|
|
71 | esac |
| 18 | |
72 | |
|
|
73 | # C[XX]FLAGS that we are think is ok, but needs testing |
|
|
74 | # NOTE: currently -Os have issues with gcc3 and K6* arch's |
|
|
75 | export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" |
|
|
76 | return 0 |
|
|
77 | } |
|
|
78 | |
| 19 | filter-flags () { |
79 | filter-flags() { |
| 20 | |
|
|
| 21 | for x in $1; do |
80 | for x in "$@" ; do |
|
|
81 | case "${x}" in |
|
|
82 | -fPIC|-fpic|-fPIE|-fpie) etexec-flags;; |
|
|
83 | -fstack-protector|-fstack-protector-all) fstack-flags;; |
|
|
84 | *) ;; |
|
|
85 | esac |
|
|
86 | done |
|
|
87 | |
|
|
88 | # we do this fancy spacing stuff so as to not filter |
|
|
89 | # out part of a flag ... we want flag atoms ! :D |
|
|
90 | CFLAGS=" ${CFLAGS} " |
|
|
91 | CXXFLAGS=" ${CXXFLAGS} " |
|
|
92 | for x in "$@" ; do |
| 22 | CFLAGS="${CFLAGS/$x}" |
93 | CFLAGS="${CFLAGS// ${x} / }" |
| 23 | CXXFLAGS="${CXXFLAGS/$x}" |
94 | CXXFLAGS="${CXXFLAGS// ${x} / }" |
| 24 | done |
95 | done |
| 25 | |
96 | CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" |
|
|
97 | CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" |
|
|
98 | return 0 |
| 26 | } |
99 | } |
| 27 | |
100 | |
| 28 | append-flags () { |
101 | append-flags() { |
| 29 | |
|
|
| 30 | |
|
|
| 31 | CFLAGS="$CFLAGS $1" |
102 | export CFLAGS="${CFLAGS} $@" |
| 32 | CXXFLAGS="$CXXFLAGS $1" |
103 | export CXXFLAGS="${CXXFLAGS} $@" |
| 33 | |
104 | [ "`is-flag -fno-stack-protector`" -o "`is-flag -fno-stack-protector-all`" ] && fstack-flags |
|
|
105 | return 0 |
| 34 | } |
106 | } |
| 35 | |
107 | |
| 36 | replace-flags () { |
108 | replace-flags() { |
| 37 | |
109 | # we do this fancy spacing stuff so as to not filter |
|
|
110 | # out part of a flag ... we want flag atoms ! :D |
|
|
111 | CFLAGS=" ${CFLAGS} " |
|
|
112 | CXXFLAGS=" ${CXXFLAGS} " |
| 38 | CFLAGS="${CFLAGS/${1}/${2}}" |
113 | CFLAGS="${CFLAGS// ${1} / ${2} }" |
| 39 | CXXFLAGS="${CXXFLAGS/${1}/${2}}" |
114 | CXXFLAGS="${CXXFLAGS// ${1} / ${2} }" |
| 40 | |
115 | CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" |
|
|
116 | CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" |
|
|
117 | return 0 |
| 41 | } |
118 | } |
|
|
119 | |
|
|
120 | replace-cpu-flags() { |
|
|
121 | local newcpu="$1" ; shift |
|
|
122 | local oldcpu="" |
|
|
123 | for oldcpu in "$@" ; do |
|
|
124 | replace-flags -march=${oldcpu} -march=${newcpu} |
|
|
125 | replace-flags -mcpu=${oldcpu} -mcpu=${newcpu} |
|
|
126 | done |
|
|
127 | return 0 |
|
|
128 | } |
|
|
129 | |
|
|
130 | is-flag() { |
|
|
131 | for x in ${CFLAGS} ${CXXFLAGS} ; do |
|
|
132 | if [ "${x}" == "$1" ] ; then |
|
|
133 | echo true |
|
|
134 | return 0 |
|
|
135 | fi |
|
|
136 | done |
|
|
137 | return 1 |
|
|
138 | } |
|
|
139 | |
|
|
140 | filter-mfpmath() { |
|
|
141 | # save the original -mfpmath flag |
|
|
142 | local orig_mfpmath="`get-flag -mfpmath`" |
|
|
143 | # get the value of the current -mfpmath flag |
|
|
144 | local new_math=" `get-flag mfpmath | tr , ' '` " |
|
|
145 | # figure out which math values are to be removed |
|
|
146 | local prune_math="" |
|
|
147 | for prune_math in "$@" ; do |
|
|
148 | new_math="${new_math/ ${prune_math} / }" |
|
|
149 | done |
|
|
150 | new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`" |
|
|
151 | |
|
|
152 | if [ -z "${new_math}" ] ; then |
|
|
153 | # if we're removing all user specified math values are |
|
|
154 | # slated for removal, then we just filter the flag |
|
|
155 | filter-flags ${orig_mfpmath} |
|
|
156 | else |
|
|
157 | # if we only want to filter some of the user specified |
|
|
158 | # math values, then we replace the current flag |
|
|
159 | replace-flags ${orig_mfpmath} -mfpmath=${new_math} |
|
|
160 | fi |
|
|
161 | return 0 |
|
|
162 | } |
|
|
163 | |
|
|
164 | strip-flags() { |
|
|
165 | setup-allowed-flags |
|
|
166 | |
|
|
167 | local NEW_CFLAGS="" |
|
|
168 | local NEW_CXXFLAGS="" |
|
|
169 | |
|
|
170 | # Allow unstable C[XX]FLAGS if we are using unstable profile ... |
|
|
171 | if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then |
|
|
172 | [ `use debug` ] && einfo "Enabling the use of some unstable flags" |
|
|
173 | ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}" |
|
|
174 | fi |
|
|
175 | |
|
|
176 | set -f |
|
|
177 | |
|
|
178 | for x in ${CFLAGS} |
|
|
179 | do |
|
|
180 | for y in ${ALLOWED_FLAGS} |
|
|
181 | do |
|
|
182 | flag=${x%%=*} |
|
|
183 | if [ "${flag%%${y}}" = "" ] |
|
|
184 | then |
|
|
185 | NEW_CFLAGS="${NEW_CFLAGS} ${x}" |
|
|
186 | break |
|
|
187 | fi |
|
|
188 | done |
|
|
189 | done |
|
|
190 | |
|
|
191 | for x in ${CXXFLAGS} |
|
|
192 | do |
|
|
193 | for y in ${ALLOWED_FLAGS} |
|
|
194 | do |
|
|
195 | flag=${x%%=*} |
|
|
196 | if [ "${flag%%${y}}" = "" ] |
|
|
197 | then |
|
|
198 | NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}" |
|
|
199 | break |
|
|
200 | fi |
|
|
201 | done |
|
|
202 | done |
|
|
203 | |
|
|
204 | # In case we filtered out all optimization flags fallback to -O2 |
|
|
205 | if [ "${CFLAGS/-O}" != "${CFLAGS}" -a "${NEW_CFLAGS/-O}" = "${NEW_CFLAGS}" ]; then |
|
|
206 | NEW_CFLAGS="${NEW_CFLAGS} -O2" |
|
|
207 | fi |
|
|
208 | if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then |
|
|
209 | NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2" |
|
|
210 | fi |
|
|
211 | |
|
|
212 | set +f |
|
|
213 | |
|
|
214 | [ `use debug` ] \ |
|
|
215 | && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \ |
|
|
216 | && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\"" |
|
|
217 | |
|
|
218 | export CFLAGS="${NEW_CFLAGS}" |
|
|
219 | export CXXFLAGS="${NEW_CXXFLAGS}" |
|
|
220 | return 0 |
|
|
221 | } |
|
|
222 | |
|
|
223 | test_flag() { |
|
|
224 | if gcc -S -xc "$@" -o /dev/null /dev/null >/dev/null 2>&1; then |
|
|
225 | echo "$@" |
|
|
226 | return 0 |
|
|
227 | fi |
|
|
228 | return 1 |
|
|
229 | } |
|
|
230 | |
|
|
231 | strip-unsupported-flags() { |
|
|
232 | for x in ${CFLAGS} ; do |
|
|
233 | NEW_CFLAGS=${NEW_CFLAGS}" ""`test_flag ${x}`" |
|
|
234 | done |
|
|
235 | for x in ${CXXFLAGS} ; do |
|
|
236 | NEW_CXXFLAGS=${NEW_CXXFLAGS}" ""`test_flag ${x}`" |
|
|
237 | done |
|
|
238 | |
|
|
239 | export CFLAGS="${NEW_CFLAGS}" |
|
|
240 | export CXXFLAGS="${NEW_CXXFLAGS}" |
|
|
241 | } |
|
|
242 | |
|
|
243 | get-flag() { |
|
|
244 | # this code looks a little flaky but seems to work for |
|
|
245 | # everything we want ... |
|
|
246 | # for example, if CFLAGS="-march=i686": |
|
|
247 | # `get-flags -march` == "-march=i686" |
|
|
248 | # `get-flags march` == "i686" |
|
|
249 | local findflag="$1" |
|
|
250 | for f in ${CFLAGS} ${CXXFLAGS} ; do |
|
|
251 | if [ "${f/${findflag}}" != "${f}" ] ; then |
|
|
252 | echo "${f/-${findflag}=}" |
|
|
253 | return 0 |
|
|
254 | fi |
|
|
255 | done |
|
|
256 | return 1 |
|
|
257 | } |
|
|
258 | |
|
|
259 | replace-sparc64-flags() { |
|
|
260 | local SPARC64_CPUS="ultrasparc v9" |
|
|
261 | |
|
|
262 | if [ "${CFLAGS/mtune}" != "${CFLAGS}" ] |
|
|
263 | then |
|
|
264 | for x in ${SPARC64_CPUS} |
|
|
265 | do |
|
|
266 | CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}" |
|
|
267 | done |
|
|
268 | else |
|
|
269 | for x in ${SPARC64_CPUS} |
|
|
270 | do |
|
|
271 | CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" |
|
|
272 | done |
|
|
273 | fi |
|
|
274 | |
|
|
275 | if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ] |
|
|
276 | then |
|
|
277 | for x in ${SPARC64_CPUS} |
|
|
278 | do |
|
|
279 | CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}" |
|
|
280 | done |
|
|
281 | else |
|
|
282 | for x in ${SPARC64_CPUS} |
|
|
283 | do |
|
|
284 | CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" |
|
|
285 | done |
|
|
286 | fi |
|
|
287 | } |
|
|
288 | |
|
|
289 | append-ldflags() { |
|
|
290 | LDFLAGS="${LDFLAGS} $@" |
|
|
291 | return 0 |
|
|
292 | } |
|
|
293 | |
|
|
294 | etexec-flags() { |
|
|
295 | has_version sys-devel/hardened-gcc |
|
|
296 | if [ $? == 0 ] ; then |
|
|
297 | if [ "`is-flag -yet_exec`" != "true" ]; then |
|
|
298 | debug-print ">>> appending flags -yet_exec" |
|
|
299 | append-flags -yet_exec |
|
|
300 | append-ldflags -yet_exec |
|
|
301 | fi |
|
|
302 | fi |
|
|
303 | } |
|
|
304 | |
|
|
305 | fstack-flags() { |
|
|
306 | has_version sys-devel/hardened-gcc |
|
|
307 | if [ $? == 0 ] ; then |
|
|
308 | if [ "`is-flag -yno_propolice`" != "true" ]; then |
|
|
309 | debug-print ">>> appending flags -yno_propolice" |
|
|
310 | append-flags -yno_propolice |
|
|
311 | append-ldflags -yno_propolice |
|
|
312 | fi |
|
|
313 | fi |
|
|
314 | } |