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