| 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.40 2004/03/12 11:21:15 vapier Exp $ |
3 | # $Header: /var/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.55 2004/06/08 20:49:52 lv 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 <flags> #### |
12 | #### filter-flags <flags> #### |
| 12 | # Remove particular flags from C[XX]FLAGS |
13 | # Remove particular flags from C[XX]FLAGS |
| 13 | # Matches only complete flags |
14 | # Matches only complete flags |
| … | |
… | |
| 47 | # will leave the user with -mfpmath=386 |
48 | # will leave the user with -mfpmath=386 |
| 48 | # |
49 | # |
| 49 | #### append-ldflags #### |
50 | #### append-ldflags #### |
| 50 | # Add extra flags to your current LDFLAGS |
51 | # Add extra flags to your current LDFLAGS |
| 51 | # |
52 | # |
|
|
53 | #### filter-ldflags <flags> #### |
|
|
54 | # Remove particular flags from LDFLAGS |
|
|
55 | # Matches only complete flags |
|
|
56 | # |
| 52 | #### etexec-flags #### |
57 | #### etexec-flags #### |
| 53 | # hooked function for hardened-gcc that appends |
58 | # hooked function for hardened gcc that appends |
| 54 | # -yet_exec {C,CXX,LD}FLAGS when hardened-gcc is installed |
59 | # -fno-pic to {C,CXX,LD}FLAGS |
| 55 | # and a package is filtering -fPIC,-fpic, -fPIE, -fpie |
60 | # when a package is filtering -fpic, -fPIC, -fpie, -fPIE |
| 56 | # |
61 | # |
| 57 | #### fstack-flags #### |
62 | #### fstack-flags #### |
| 58 | # hooked function for hardened-gcc that appends |
63 | # hooked function for hardened gcc that appends |
| 59 | # -yno_propolice to {C,CXX,LD}FLAGS when hardened-gcc is installed |
64 | # -fno-stack-protector to {C,CXX,LD}FLAGS |
| 60 | # and a package is filtering -fstack-protector, -fstack-protector-all |
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 |
| 61 | # |
68 | # |
| 62 | |
69 | |
| 63 | # C[XX]FLAGS that we allow in strip-flags |
70 | # C[XX]FLAGS that we allow in strip-flags |
| 64 | setup-allowed-flags() { |
71 | setup-allowed-flags() { |
|
|
72 | if [ -z "${ALLOWED_FLAGS}" ] ; then |
| 65 | export ALLOWED_FLAGS="-O -O1 -O2 -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" |
| 66 | case "${ARCH}" in |
74 | case "${ARCH}" in |
| 67 | mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; |
75 | mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; |
| 68 | amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
76 | amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC -m64" ;; |
|
|
77 | x86) ALLOWED_FLAGS="${ALLOWED_FLAGS} -m32" ;; |
| 69 | alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
78 | alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
| 70 | ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
79 | ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
| 71 | esac |
80 | esac |
|
|
81 | fi |
| 72 | |
82 | |
| 73 | # C[XX]FLAGS that we are think is ok, but needs testing |
83 | # C[XX]FLAGS that we are think is ok, but needs testing |
| 74 | # NOTE: currently -Os have issues with gcc3 and K6* arch's |
84 | # NOTE: currently -Os have issues with gcc3 and K6* arch's |
| 75 | export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" |
85 | export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" |
| 76 | return 0 |
86 | return 0 |
| … | |
… | |
| 98 | return 0 |
108 | return 0 |
| 99 | } |
109 | } |
| 100 | |
110 | |
| 101 | filter-lfs-flags() { |
111 | filter-lfs-flags() { |
| 102 | filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE |
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 |
| 103 | } |
117 | } |
| 104 | |
118 | |
| 105 | append-flags() { |
119 | append-flags() { |
| 106 | export CFLAGS="${CFLAGS} $@" |
120 | export CFLAGS="${CFLAGS} $@" |
| 107 | export CXXFLAGS="${CXXFLAGS} $@" |
121 | export CXXFLAGS="${CXXFLAGS} $@" |
| … | |
… | |
| 171 | |
185 | |
| 172 | local NEW_CFLAGS="" |
186 | local NEW_CFLAGS="" |
| 173 | local NEW_CXXFLAGS="" |
187 | local NEW_CXXFLAGS="" |
| 174 | |
188 | |
| 175 | # Allow unstable C[XX]FLAGS if we are using unstable profile ... |
189 | # Allow unstable C[XX]FLAGS if we are using unstable profile ... |
| 176 | if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then |
190 | if has ~${ARCH} ${ACCEPT_KEYWORDS} ; then |
| 177 | [ `use debug` ] && einfo "Enabling the use of some unstable flags" |
191 | use debug && einfo "Enabling the use of some unstable flags" |
| 178 | ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}" |
192 | ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}" |
| 179 | fi |
193 | fi |
| 180 | |
194 | |
| 181 | set -f |
195 | set -f |
| 182 | |
196 | |
| … | |
… | |
| 214 | NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2" |
228 | NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2" |
| 215 | fi |
229 | fi |
| 216 | |
230 | |
| 217 | set +f |
231 | set +f |
| 218 | |
232 | |
| 219 | [ `use debug` ] \ |
233 | use debug \ |
| 220 | && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \ |
234 | && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \ |
| 221 | && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\"" |
235 | && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\"" |
| 222 | |
236 | |
| 223 | export CFLAGS="${NEW_CFLAGS}" |
237 | export CFLAGS="${NEW_CFLAGS}" |
| 224 | export CXXFLAGS="${NEW_CXXFLAGS}" |
238 | export CXXFLAGS="${NEW_CXXFLAGS}" |
| 225 | return 0 |
239 | return 0 |
| 226 | } |
240 | } |
| 227 | |
241 | |
| 228 | test_flag() { |
242 | test_flag() { |
| 229 | if gcc -S -xc "$@" -o /dev/null /dev/null >/dev/null 2>&1; then |
243 | if [ -z "`gcc -S -xc "$@" -o /dev/null /dev/null 2>&1`" ]; then |
| 230 | echo "$@" |
244 | echo "$@" |
| 231 | return 0 |
245 | return 0 |
| 232 | fi |
246 | fi |
| 233 | return 1 |
247 | return 1 |
| 234 | } |
248 | } |
| … | |
… | |
| 259 | fi |
273 | fi |
| 260 | done |
274 | done |
| 261 | return 1 |
275 | return 1 |
| 262 | } |
276 | } |
| 263 | |
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 | |
| 264 | replace-sparc64-flags() { |
304 | replace-sparc64-flags() { |
| 265 | local SPARC64_CPUS="ultrasparc v9" |
305 | local SPARC64_CPUS="ultrasparc v9" |
| 266 | |
306 | |
| 267 | if [ "${CFLAGS/mtune}" != "${CFLAGS}" ] |
307 | if [ "${CFLAGS/mtune}" != "${CFLAGS}" ] |
| 268 | then |
308 | then |
| … | |
… | |
| 294 | append-ldflags() { |
334 | append-ldflags() { |
| 295 | LDFLAGS="${LDFLAGS} $@" |
335 | LDFLAGS="${LDFLAGS} $@" |
| 296 | return 0 |
336 | return 0 |
| 297 | } |
337 | } |
| 298 | |
338 | |
|
|
339 | filter-ldflags() { |
|
|
340 | # we do this fancy spacing stuff so as to not filter |
|
|
341 | # out part of a flag ... we want flag atoms ! :D |
|
|
342 | LDFLAGS=" ${LDFLAGS} " |
|
|
343 | for x in "$@" ; do |
|
|
344 | LDFLAGS="${LDFLAGS// ${x} / }" |
|
|
345 | done |
|
|
346 | LDFLAGS="${LDFLAGS:1:${#LDFLAGS}-2}" |
|
|
347 | return 0 |
|
|
348 | } |
|
|
349 | |
| 299 | etexec-flags() { |
350 | etexec-flags() { |
| 300 | has_version sys-devel/hardened-gcc |
351 | # if your not using a hardened compiler you wont need this |
|
|
352 | # PIC/no-pic kludge in the first place. |
|
|
353 | has_hardened || return |
|
|
354 | |
|
|
355 | has_pie || has_pic |
| 301 | if [ $? == 0 ] ; then |
356 | if [ $? == 0 ] ; then |
| 302 | if [ "`is-flag -yet_exec`" != "true" ]; then |
357 | [ -z "`is-flag -fno-pic`" ] && |
| 303 | debug-print ">>> appending flags -yet_exec" |
358 | export CFLAGS="${CFLAGS} `test_flag -fno-pic`" |
| 304 | append-flags -yet_exec |
359 | |
| 305 | append-ldflags -yet_exec |
360 | [ -z "`is-flag -nopie`" ] && |
| 306 | fi |
361 | export CFLAGS="${CFLAGS} `test_flag -nopie`" |
| 307 | fi |
362 | fi |
| 308 | } |
363 | } |
| 309 | |
364 | |
| 310 | fstack-flags() { |
365 | fstack-flags() { |
| 311 | has_version sys-devel/hardened-gcc |
366 | has_ssp |
| 312 | if [ $? == 0 ] ; then |
367 | if [ $? == 0 ] ; then |
| 313 | if [ "`is-flag -yno_propolice`" != "true" ]; then |
368 | [ -z "`is-flag -fno-stack-protector`" ] && |
| 314 | debug-print ">>> appending flags -yno_propolice" |
369 | export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`" |
| 315 | append-flags -yno_propolice |
|
|
| 316 | append-ldflags -yno_propolice |
|
|
| 317 | fi |
370 | fi |
| 318 | fi |
|
|
| 319 | } |
371 | } |