| 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.37 2004/02/21 07:19:29 vapier Exp $ |
3 | # $Header: /var/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.53 2004/06/01 04:28:38 solar 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" |
| … | |
… | |
| 47 | # will leave the user with -mfpmath=386 |
47 | # will leave the user with -mfpmath=386 |
| 48 | # |
48 | # |
| 49 | #### append-ldflags #### |
49 | #### append-ldflags #### |
| 50 | # Add extra flags to your current LDFLAGS |
50 | # Add extra flags to your current LDFLAGS |
| 51 | # |
51 | # |
|
|
52 | #### filter-ldflags <flags> #### |
|
|
53 | # Remove particular flags from LDFLAGS |
|
|
54 | # Matches only complete flags |
|
|
55 | # |
| 52 | #### etexec-flags #### |
56 | #### etexec-flags #### |
| 53 | # hooked function for hardened-gcc that appends |
57 | # hooked function for hardened gcc that appends |
| 54 | # -yet_exec {C,CXX,LD}FLAGS when hardened-gcc is installed |
58 | # -fno-pic to {C,CXX,LD}FLAGS |
| 55 | # and a package is filtering -fPIC,-fpic, -fPIE, -fpie |
59 | # when a package is filtering -fpic, -fPIC, -fpie, -fPIE |
| 56 | # |
60 | # |
| 57 | #### fstack-flags #### |
61 | #### fstack-flags #### |
| 58 | # hooked function for hardened-gcc that appends |
62 | # hooked function for hardened gcc that appends |
| 59 | # -yno_propolice to {C,CXX,LD}FLAGS when hardened-gcc is installed |
63 | # -fno-stack-protector to {C,CXX,LD}FLAGS |
| 60 | # and a package is filtering -fstack-protector, -fstack-protector-all |
64 | # when a package is filtering -fstack-protector, -fstack-protector-all |
|
|
65 | # notice: modern automatic specs files will also suppress -fstack-protector-all |
|
|
66 | # when only -fno-stack-protector is given |
| 61 | # |
67 | # |
| 62 | |
68 | |
| 63 | # C[XX]FLAGS that we allow in strip-flags |
69 | # C[XX]FLAGS that we allow in strip-flags |
| 64 | setup-allowed-flags() { |
70 | setup-allowed-flags() { |
|
|
71 | if [ -z "${ALLOWED_FLAGS}" ] ; then |
| 65 | export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -pipe -g" |
72 | export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -fno-unit-at-a-time -pipe -g" |
| 66 | case "${ARCH}" in |
73 | case "${ARCH}" in |
| 67 | mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; |
74 | mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; |
| 68 | amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
75 | amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC -m64" ;; |
|
|
76 | x86) ALLOWED_FLAGS="${ALLOWED_FLAGS} -m32" ;; |
| 69 | alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
77 | alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
| 70 | ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
78 | ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
| 71 | esac |
79 | esac |
|
|
80 | fi |
| 72 | |
81 | |
| 73 | # C[XX]FLAGS that we are think is ok, but needs testing |
82 | # C[XX]FLAGS that we are think is ok, but needs testing |
| 74 | # NOTE: currently -Os have issues with gcc3 and K6* arch's |
83 | # NOTE: currently -Os have issues with gcc3 and K6* arch's |
| 75 | export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" |
84 | export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" |
| 76 | return 0 |
85 | return 0 |
| 77 | } |
86 | } |
| 78 | |
87 | |
| 79 | filter-flags() { |
88 | filter-flags() { |
| 80 | for x in "$@" ; do |
89 | for x in "$@" ; do |
| 81 | case "${x}" in |
90 | case "${x}" in |
| 82 | -fPIC|-fpic|-fPIE|-fpie) etexec-flags;; |
91 | -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;; |
| 83 | -fstack-protector|-fstack-protector-all) fstack-flags;; |
92 | -fstack-protector|-fstack-protector-all) fstack-flags;; |
| 84 | *) ;; |
93 | *) ;; |
| 85 | esac |
94 | esac |
| 86 | done |
95 | done |
| 87 | |
96 | |
| … | |
… | |
| 94 | CXXFLAGS="${CXXFLAGS// ${x} / }" |
103 | CXXFLAGS="${CXXFLAGS// ${x} / }" |
| 95 | done |
104 | done |
| 96 | CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" |
105 | CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" |
| 97 | CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" |
106 | CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" |
| 98 | return 0 |
107 | return 0 |
|
|
108 | } |
|
|
109 | |
|
|
110 | filter-lfs-flags() { |
|
|
111 | filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE |
|
|
112 | } |
|
|
113 | |
|
|
114 | append-lfs-flags() { |
|
|
115 | append-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE |
| 99 | } |
116 | } |
| 100 | |
117 | |
| 101 | append-flags() { |
118 | append-flags() { |
| 102 | export CFLAGS="${CFLAGS} $@" |
119 | export CFLAGS="${CFLAGS} $@" |
| 103 | export CXXFLAGS="${CXXFLAGS} $@" |
120 | export CXXFLAGS="${CXXFLAGS} $@" |
| … | |
… | |
| 121 | local newcpu="$1" ; shift |
138 | local newcpu="$1" ; shift |
| 122 | local oldcpu="" |
139 | local oldcpu="" |
| 123 | for oldcpu in "$@" ; do |
140 | for oldcpu in "$@" ; do |
| 124 | replace-flags -march=${oldcpu} -march=${newcpu} |
141 | replace-flags -march=${oldcpu} -march=${newcpu} |
| 125 | replace-flags -mcpu=${oldcpu} -mcpu=${newcpu} |
142 | replace-flags -mcpu=${oldcpu} -mcpu=${newcpu} |
|
|
143 | replace-flags -mtune=${oldcpu} -mtune=${newcpu} |
| 126 | done |
144 | done |
| 127 | return 0 |
145 | return 0 |
| 128 | } |
146 | } |
| 129 | |
147 | |
| 130 | is-flag() { |
148 | is-flag() { |
| … | |
… | |
| 166 | |
184 | |
| 167 | local NEW_CFLAGS="" |
185 | local NEW_CFLAGS="" |
| 168 | local NEW_CXXFLAGS="" |
186 | local NEW_CXXFLAGS="" |
| 169 | |
187 | |
| 170 | # Allow unstable C[XX]FLAGS if we are using unstable profile ... |
188 | # Allow unstable C[XX]FLAGS if we are using unstable profile ... |
| 171 | if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then |
189 | if has ~${ARCH} ${ACCEPT_KEYWORDS} ; then |
| 172 | [ `use debug` ] && einfo "Enabling the use of some unstable flags" |
190 | use debug && einfo "Enabling the use of some unstable flags" |
| 173 | ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}" |
191 | ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}" |
| 174 | fi |
192 | fi |
| 175 | |
193 | |
| 176 | set -f |
194 | set -f |
| 177 | |
195 | |
| … | |
… | |
| 209 | NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2" |
227 | NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2" |
| 210 | fi |
228 | fi |
| 211 | |
229 | |
| 212 | set +f |
230 | set +f |
| 213 | |
231 | |
| 214 | [ `use debug` ] \ |
232 | use debug \ |
| 215 | && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \ |
233 | && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \ |
| 216 | && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\"" |
234 | && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\"" |
| 217 | |
235 | |
| 218 | export CFLAGS="${NEW_CFLAGS}" |
236 | export CFLAGS="${NEW_CFLAGS}" |
| 219 | export CXXFLAGS="${NEW_CXXFLAGS}" |
237 | export CXXFLAGS="${NEW_CXXFLAGS}" |
| 220 | return 0 |
238 | return 0 |
| 221 | } |
239 | } |
| 222 | |
240 | |
| 223 | test_flag() { |
241 | test_flag() { |
| 224 | if gcc -S -xc "$@" -o /dev/null /dev/null >/dev/null 2>&1; then |
242 | if [ -z "`gcc -S -xc "$@" -o /dev/null /dev/null 2>&1`" ]; then |
| 225 | echo "$@" |
243 | echo "$@" |
| 226 | return 0 |
244 | return 0 |
| 227 | fi |
245 | fi |
| 228 | return 1 |
246 | return 1 |
| 229 | } |
247 | } |
| … | |
… | |
| 254 | fi |
272 | fi |
| 255 | done |
273 | done |
| 256 | return 1 |
274 | return 1 |
| 257 | } |
275 | } |
| 258 | |
276 | |
|
|
277 | has_hardened() { |
|
|
278 | local cc=${CC:-gcc} |
|
|
279 | [[ $(${cc%% *} --version 2>&1) == *Hardened* ]] |
|
|
280 | return $? |
|
|
281 | } |
|
|
282 | |
|
|
283 | has_pic() { |
|
|
284 | [ "${CFLAGS/-fPIC}" != "${CFLAGS}" ] && return 0 |
|
|
285 | [ "${CFLAGS/-fpic}" != "${CFLAGS}" ] && return 0 |
|
|
286 | [ ! -z "`${CC/ .*/} --version| grep pie`" ] && return 0 |
|
|
287 | return 1 |
|
|
288 | } |
|
|
289 | |
|
|
290 | has_pie() { |
|
|
291 | [ "${CFLAGS/-fPIE}" != "${CFLAGS}" ] && return 0 |
|
|
292 | [ "${CFLAGS/-fpie}" != "${CFLAGS}" ] && return 0 |
|
|
293 | [ ! -z "`${CC/ .*/} --version| grep pie`" ] && return 0 |
|
|
294 | return 1 |
|
|
295 | } |
|
|
296 | |
|
|
297 | has_ssp() { |
|
|
298 | [ "${CFLAGS/-fstack-protector}" != "${CFLAGS}" ] && return 0 |
|
|
299 | [ ! -z "`${CC/ .*/} --version| grep ssp`" ] && return 0 |
|
|
300 | return 1 |
|
|
301 | } |
|
|
302 | |
| 259 | replace-sparc64-flags() { |
303 | replace-sparc64-flags() { |
| 260 | local SPARC64_CPUS="ultrasparc v9" |
304 | local SPARC64_CPUS="ultrasparc v9" |
| 261 | |
305 | |
| 262 | if [ "${CFLAGS/mtune}" != "${CFLAGS}" ] |
306 | if [ "${CFLAGS/mtune}" != "${CFLAGS}" ] |
| 263 | then |
307 | then |
| … | |
… | |
| 289 | append-ldflags() { |
333 | append-ldflags() { |
| 290 | LDFLAGS="${LDFLAGS} $@" |
334 | LDFLAGS="${LDFLAGS} $@" |
| 291 | return 0 |
335 | return 0 |
| 292 | } |
336 | } |
| 293 | |
337 | |
|
|
338 | filter-ldflags() { |
|
|
339 | # we do this fancy spacing stuff so as to not filter |
|
|
340 | # out part of a flag ... we want flag atoms ! :D |
|
|
341 | LDFLAGS=" ${LDFLAGS} " |
|
|
342 | for x in "$@" ; do |
|
|
343 | LDFLAGS="${LDFLAGS// ${x} / }" |
|
|
344 | done |
|
|
345 | LDFLAGS="${LDFLAGS:1:${#LDFLAGS}-2}" |
|
|
346 | return 0 |
|
|
347 | } |
|
|
348 | |
| 294 | etexec-flags() { |
349 | etexec-flags() { |
| 295 | has_version sys-devel/hardened-gcc |
350 | # if your not using a hardened compiler you wont need this |
|
|
351 | # PIC/no-pic kludge in the first place. |
|
|
352 | has_hardened || return |
|
|
353 | |
|
|
354 | has_pie || has_pic |
| 296 | if [ $? == 0 ] ; then |
355 | if [ $? == 0 ] ; then |
| 297 | if [ "`is-flag -yet_exec`" != "true" ]; then |
356 | [ -z "`is-flag -fno-pic`" ] && |
| 298 | debug-print ">>> appending flags -yet_exec" |
357 | export CFLAGS="${CFLAGS} `test_flag -fno-pic`" |
| 299 | append-flags -yet_exec |
358 | |
| 300 | append-ldflags -yet_exec |
359 | [ -z "`is-flag -nopie`" ] && |
| 301 | fi |
360 | export CFLAGS="${CFLAGS} `test_flag -nopie`" |
| 302 | fi |
361 | fi |
| 303 | } |
362 | } |
| 304 | |
363 | |
| 305 | fstack-flags() { |
364 | fstack-flags() { |
| 306 | has_version sys-devel/hardened-gcc |
365 | has_ssp |
| 307 | if [ $? == 0 ] ; then |
366 | if [ $? == 0 ] ; then |
| 308 | if [ "`is-flag -yno_propolice`" != "true" ]; then |
367 | [ -z "`is-flag -fno-stack-protector`" ] && |
| 309 | debug-print ">>> appending flags -yno_propolice" |
368 | export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`" |
| 310 | append-flags -yno_propolice |
|
|
| 311 | append-ldflags -yno_propolice |
|
|
| 312 | fi |
369 | fi |
| 313 | fi |
|
|
| 314 | } |
370 | } |