/[gentoo-x86]/eclass/flag-o-matic.eclass
Gentoo

Diff of /eclass/flag-o-matic.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.37 Revision 1.56
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.56 2004/06/10 00:24:57 lv Exp $
4# 4#
5# Author Bart Verwilst <verwilst@gentoo.org> 5# Author Bart Verwilst <verwilst@gentoo.org>
6 6
7ECLASS=flag-o-matic 7ECLASS=flag-o-matic
8INHERITED="$INHERITED $ECLASS" 8INHERITED="$INHERITED $ECLASS"
9IUSE="$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
64setup-allowed-flags() { 71setup-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
77} 87}
78 88
79filter-flags() { 89filter-flags() {
80 for x in "$@" ; do 90 for x in "$@" ; do
81 case "${x}" in 91 case "${x}" in
82 -fPIC|-fpic|-fPIE|-fpie) etexec-flags;; 92 -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;;
83 -fstack-protector|-fstack-protector-all) fstack-flags;; 93 -fstack-protector|-fstack-protector-all) fstack-flags;;
84 *) ;; 94 *) ;;
85 esac 95 esac
86 done 96 done
87 97
94 CXXFLAGS="${CXXFLAGS// ${x} / }" 104 CXXFLAGS="${CXXFLAGS// ${x} / }"
95 done 105 done
96 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" 106 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
97 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" 107 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
98 return 0 108 return 0
109}
110
111filter-lfs-flags() {
112 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
113}
114
115append-lfs-flags() {
116 append-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
99} 117}
100 118
101append-flags() { 119append-flags() {
102 export CFLAGS="${CFLAGS} $@" 120 export CFLAGS="${CFLAGS} $@"
103 export CXXFLAGS="${CXXFLAGS} $@" 121 export CXXFLAGS="${CXXFLAGS} $@"
121 local newcpu="$1" ; shift 139 local newcpu="$1" ; shift
122 local oldcpu="" 140 local oldcpu=""
123 for oldcpu in "$@" ; do 141 for oldcpu in "$@" ; do
124 replace-flags -march=${oldcpu} -march=${newcpu} 142 replace-flags -march=${oldcpu} -march=${newcpu}
125 replace-flags -mcpu=${oldcpu} -mcpu=${newcpu} 143 replace-flags -mcpu=${oldcpu} -mcpu=${newcpu}
144 replace-flags -mtune=${oldcpu} -mtune=${newcpu}
126 done 145 done
127 return 0 146 return 0
128} 147}
129 148
130is-flag() { 149is-flag() {
166 185
167 local NEW_CFLAGS="" 186 local NEW_CFLAGS=""
168 local NEW_CXXFLAGS="" 187 local NEW_CXXFLAGS=""
169 188
170 # Allow unstable C[XX]FLAGS if we are using unstable profile ... 189 # Allow unstable C[XX]FLAGS if we are using unstable profile ...
171 if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then 190 if has ~${ARCH} ${ACCEPT_KEYWORDS} ; then
172 [ `use debug` ] && einfo "Enabling the use of some unstable flags" 191 use debug && einfo "Enabling the use of some unstable flags"
173 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}" 192 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}"
174 fi 193 fi
175 194
176 set -f 195 set -f
177 196
209 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2" 228 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2"
210 fi 229 fi
211 230
212 set +f 231 set +f
213 232
214 [ `use debug` ] \ 233 use debug \
215 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \ 234 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \
216 && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\"" 235 && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\""
217 236
218 export CFLAGS="${NEW_CFLAGS}" 237 export CFLAGS="${NEW_CFLAGS}"
219 export CXXFLAGS="${NEW_CXXFLAGS}" 238 export CXXFLAGS="${NEW_CXXFLAGS}"
220 return 0 239 return 0
221} 240}
222 241
223test_flag() { 242test_flag() {
224 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
225 echo "$@" 244 echo "$@"
226 return 0 245 return 0
227 fi 246 fi
228 return 1 247 return 1
229} 248}
254 fi 273 fi
255 done 274 done
256 return 1 275 return 1
257} 276}
258 277
278has_hardened() {
279 local cc=${CC:-gcc}
280 [[ $(${cc%% *} --version 2>&1) == *Hardened* ]]
281 return $?
282}
283
284has_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
291has_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
298has_ssp() {
299 [ "${CFLAGS/-fstack-protector}" != "${CFLAGS}" ] && return 0
300 [ ! -z "`${CC/ .*/} --version| grep ssp`" ] && return 0
301 return 1
302}
303
304has_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
314has_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
259replace-sparc64-flags() { 324replace-sparc64-flags() {
260 local SPARC64_CPUS="ultrasparc v9" 325 local SPARC64_CPUS="ultrasparc v9"
261 326
262 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ] 327 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]
263 then 328 then
289append-ldflags() { 354append-ldflags() {
290 LDFLAGS="${LDFLAGS} $@" 355 LDFLAGS="${LDFLAGS} $@"
291 return 0 356 return 0
292} 357}
293 358
359filter-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
294etexec-flags() { 370etexec-flags() {
295 has_version sys-devel/hardened-gcc 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
296 if [ $? == 0 ] ; then 376 if [ $? == 0 ] ; then
297 if [ "`is-flag -yet_exec`" != "true" ]; then 377 [ -z "`is-flag -fno-pic`" ] &&
298 debug-print ">>> appending flags -yet_exec" 378 export CFLAGS="${CFLAGS} `test_flag -fno-pic`"
299 append-flags -yet_exec 379
300 append-ldflags -yet_exec 380 [ -z "`is-flag -nopie`" ] &&
301 fi 381 export CFLAGS="${CFLAGS} `test_flag -nopie`"
302 fi 382 fi
303} 383}
304 384
305fstack-flags() { 385fstack-flags() {
306 has_version sys-devel/hardened-gcc 386 has_ssp
307 if [ $? == 0 ] ; then 387 if [ $? == 0 ] ; then
308 if [ "`is-flag -yno_propolice`" != "true" ]; then 388 [ -z "`is-flag -fno-stack-protector`" ] &&
309 debug-print ">>> appending flags -yno_propolice" 389 export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`"
310 append-flags -yno_propolice
311 append-ldflags -yno_propolice
312 fi 390 fi
313 fi
314} 391}
392

Legend:
Removed from v.1.37  
changed lines
  Added in v.1.56

  ViewVC Help
Powered by ViewVC 1.1.20