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