/[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.63
1# Copyright 1999-2003 Gentoo Technologies, Inc. 1# Copyright 1999-2004 Gentoo Foundation
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.63 2004/07/18 04:59:30 vapier 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"
9
10IUSE="debug"
9 11
10# 12#
11#### filter-flags <flags> #### 13#### filter-flags <flags> ####
12# Remove particular flags from C[XX]FLAGS 14# Remove particular flags from C[XX]FLAGS
13# Matches only complete flags 15# Matches only complete flags
47# will leave the user with -mfpmath=386 49# will leave the user with -mfpmath=386
48# 50#
49#### append-ldflags #### 51#### append-ldflags ####
50# Add extra flags to your current LDFLAGS 52# Add extra flags to your current LDFLAGS
51# 53#
54#### filter-ldflags <flags> ####
55# Remove particular flags from LDFLAGS
56# Matches only complete flags
57#
52#### etexec-flags #### 58#### etexec-flags ####
53# hooked function for hardened-gcc that appends 59# hooked function for hardened gcc that appends
54# -yet_exec {C,CXX,LD}FLAGS when hardened-gcc is installed 60# -fno-pic to {C,CXX,LD}FLAGS
55# and a package is filtering -fPIC,-fpic, -fPIE, -fpie 61# when a package is filtering -fpic, -fPIC, -fpie, -fPIE
56# 62#
57#### fstack-flags #### 63#### fstack-flags ####
58# hooked function for hardened-gcc that appends 64# hooked function for hardened gcc that appends
59# -yno_propolice to {C,CXX,LD}FLAGS when hardened-gcc is installed 65# -fno-stack-protector to {C,CXX,LD}FLAGS
60# and a package is filtering -fstack-protector, -fstack-protector-all 66# when a package is filtering -fstack-protector, -fstack-protector-all
67# notice: modern automatic specs files will also suppress -fstack-protector-all
68# when only -fno-stack-protector is given
61# 69#
62 70
63# C[XX]FLAGS that we allow in strip-flags 71# C[XX]FLAGS that we allow in strip-flags
64setup-allowed-flags() { 72setup-allowed-flags() {
73 if [ -z "${ALLOWED_FLAGS}" ] ; then
65 export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -pipe -g" 74 export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -fno-unit-at-a-time -pipe -g"
66 case "${ARCH}" in 75 case "${ARCH}" in
67 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; 76 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;;
68 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 77 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC -m64" ;;
78 x86) ALLOWED_FLAGS="${ALLOWED_FLAGS} -m32" ;;
69 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 79 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
70 ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 80 ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
71 esac 81 esac
82 fi
72 83
73 # C[XX]FLAGS that we are think is ok, but needs testing 84 # C[XX]FLAGS that we are think is ok, but needs testing
74 # NOTE: currently -Os have issues with gcc3 and K6* arch's 85 # NOTE: currently -Os have issues with gcc3 and K6* arch's
75 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" 86 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays"
76 return 0 87 return 0
77} 88}
78 89
79filter-flags() { 90filter-flags() {
91 local x f fset
92 declare -a new_CFLAGS new_CXXFLAGS
93
80 for x in "$@" ; do 94 for x in "$@" ; do
81 case "${x}" in 95 case "${x}" in
82 -fPIC|-fpic|-fPIE|-fpie) etexec-flags;; 96 -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;;
83 -fstack-protector|-fstack-protector-all) fstack-flags;; 97 -fstack-protector|-fstack-protector-all) fstack-flags;;
84 *) ;;
85 esac 98 esac
86 done 99 done
87 100
88 # we do this fancy spacing stuff so as to not filter 101 for fset in CFLAGS CXXFLAGS; do
89 # out part of a flag ... we want flag atoms ! :D 102 for f in ${!fset}; do
90 CFLAGS=" ${CFLAGS} "
91 CXXFLAGS=" ${CXXFLAGS} "
92 for x in "$@" ; do 103 for x in "$@"; do
93 CFLAGS="${CFLAGS// ${x} / }" 104 # Note this should work with globs like -O*
94 CXXFLAGS="${CXXFLAGS// ${x} / }" 105 [[ ${f} == ${x} ]] && continue 2
106 done
107 eval new_${fset}\[\${\#new_${fset}\[@]}]=\${f}
95 done 108 done
96 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" 109 eval export ${fset}=\${new_${fset}\[*]}
97 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" 110 done
111
98 return 0 112 return 0
113}
114
115filter-lfs-flags() {
116 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
117}
118
119append-lfs-flags() {
120 append-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
99} 121}
100 122
101append-flags() { 123append-flags() {
102 export CFLAGS="${CFLAGS} $@" 124 export CFLAGS="${CFLAGS} $*"
103 export CXXFLAGS="${CXXFLAGS} $@" 125 export CXXFLAGS="${CXXFLAGS} $*"
104 [ "`is-flag -fno-stack-protector`" -o "`is-flag -fno-stack-protector-all`" ] && fstack-flags 126 [ -n "`is-flag -fno-stack-protector`" -o \
127 -n "`is-flag -fno-stack-protector-all`" ] && fstack-flags
105 return 0 128 return 0
106} 129}
107 130
108replace-flags() { 131replace-flags() {
109 # we do this fancy spacing stuff so as to not filter 132 # we do this fancy spacing stuff so as to not filter
112 CXXFLAGS=" ${CXXFLAGS} " 135 CXXFLAGS=" ${CXXFLAGS} "
113 CFLAGS="${CFLAGS// ${1} / ${2} }" 136 CFLAGS="${CFLAGS// ${1} / ${2} }"
114 CXXFLAGS="${CXXFLAGS// ${1} / ${2} }" 137 CXXFLAGS="${CXXFLAGS// ${1} / ${2} }"
115 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" 138 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
116 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" 139 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
140 export CFLAGS CXXFLAGS
117 return 0 141 return 0
118} 142}
119 143
120replace-cpu-flags() { 144replace-cpu-flags() {
121 local newcpu="$1" ; shift 145 local oldcpu newcpu="$1" ; shift
122 local oldcpu=""
123 for oldcpu in "$@" ; do 146 for oldcpu in "$@" ; do
124 replace-flags -march=${oldcpu} -march=${newcpu} 147 replace-flags -march=${oldcpu} -march=${newcpu}
125 replace-flags -mcpu=${oldcpu} -mcpu=${newcpu} 148 replace-flags -mcpu=${oldcpu} -mcpu=${newcpu}
149 replace-flags -mtune=${oldcpu} -mtune=${newcpu}
126 done 150 done
127 return 0 151 return 0
128} 152}
129 153
130is-flag() { 154is-flag() {
155 local x
156
131 for x in ${CFLAGS} ${CXXFLAGS} ; do 157 for x in ${CFLAGS} ${CXXFLAGS} ; do
132 if [ "${x}" == "$1" ] ; then 158 if [ "${x}" == "$1" ] ; then
133 echo true 159 echo true
134 return 0 160 return 0
135 fi 161 fi
136 done 162 done
137 return 1 163 return 1
138} 164}
139 165
140filter-mfpmath() { 166filter-mfpmath() {
167 local orig_mfpmath new_math prune_math
168
141 # save the original -mfpmath flag 169 # save the original -mfpmath flag
142 local orig_mfpmath="`get-flag -mfpmath`" 170 orig_mfpmath="`get-flag -mfpmath`"
143 # get the value of the current -mfpmath flag 171 # get the value of the current -mfpmath flag
144 local new_math=" `get-flag mfpmath | tr , ' '` " 172 new_math=" `get-flag mfpmath | tr , ' '` "
145 # figure out which math values are to be removed 173 # figure out which math values are to be removed
146 local prune_math="" 174 prune_math=""
147 for prune_math in "$@" ; do 175 for prune_math in "$@" ; do
148 new_math="${new_math/ ${prune_math} / }" 176 new_math="${new_math/ ${prune_math} / }"
149 done 177 done
150 new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`" 178 new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`"
151 179
160 fi 188 fi
161 return 0 189 return 0
162} 190}
163 191
164strip-flags() { 192strip-flags() {
193 local x y flag NEW_CFLAGS NEW_CXXFLAGS
194
165 setup-allowed-flags 195 setup-allowed-flags
166 196
167 local NEW_CFLAGS="" 197 local NEW_CFLAGS=""
168 local NEW_CXXFLAGS="" 198 local NEW_CXXFLAGS=""
169 199
170 # Allow unstable C[XX]FLAGS if we are using unstable profile ... 200 # Allow unstable C[XX]FLAGS if we are using unstable profile ...
171 if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then 201 if has ~${ARCH} ${ACCEPT_KEYWORDS} ; then
172 [ `use debug` ] && einfo "Enabling the use of some unstable flags" 202 use debug && einfo "Enabling the use of some unstable flags"
173 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}" 203 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}"
174 fi 204 fi
175 205
176 set -f 206 set -f # disable pathname expansion
177 207
178 for x in ${CFLAGS} 208 for x in ${CFLAGS}; do
179 do
180 for y in ${ALLOWED_FLAGS} 209 for y in ${ALLOWED_FLAGS}; do
181 do
182 flag=${x%%=*} 210 flag=${x%%=*}
183 if [ "${flag%%${y}}" = "" ] 211 if [ "${flag%%${y}}" = "" ] || [ "${flag:0:5}" = "-fno-" ] || [ "${flag:0:5}" = "-mno-" ] ; then
184 then
185 NEW_CFLAGS="${NEW_CFLAGS} ${x}" 212 NEW_CFLAGS="${NEW_CFLAGS} ${x}"
186 break 213 break
187 fi 214 fi
188 done 215 done
189 done 216 done
190 217
191 for x in ${CXXFLAGS} 218 for x in ${CXXFLAGS}; do
192 do
193 for y in ${ALLOWED_FLAGS} 219 for y in ${ALLOWED_FLAGS}; do
194 do
195 flag=${x%%=*} 220 flag=${x%%=*}
196 if [ "${flag%%${y}}" = "" ] 221 if [ "${flag%%${y}}" = "" ] || [ "${flag:0:5}" = "-fno-" ] || [ "${flag:0:5}" = "-mno-" ] ; then
197 then
198 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}" 222 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}"
199 break 223 break
200 fi 224 fi
201 done 225 done
202 done 226 done
207 fi 231 fi
208 if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then 232 if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then
209 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2" 233 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2"
210 fi 234 fi
211 235
212 set +f 236 set +f # re-enable pathname expansion
213
214 [ `use debug` ] \
215 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \
216 && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\""
217 237
218 export CFLAGS="${NEW_CFLAGS}" 238 export CFLAGS="${NEW_CFLAGS}"
219 export CXXFLAGS="${NEW_CXXFLAGS}" 239 export CXXFLAGS="${NEW_CXXFLAGS}"
220 return 0 240 return 0
221} 241}
222 242
223test_flag() { 243test_flag() {
244 local cc=${CC:-gcc} ; cc=${cc%% *}
224 if gcc -S -xc "$@" -o /dev/null /dev/null >/dev/null 2>&1; then 245 if ${cc} -S -xc "$@" -o /dev/null /dev/null &>/dev/null; then
225 echo "$@" 246 printf "%s\n" "$*"
226 return 0 247 return 0
227 fi 248 fi
228 return 1 249 return 1
229} 250}
230 251
252test_version_info() {
253 local cc=${CC:-gcc} ; cc=${cc%% *}
254 if [[ $(${cc} --version 2>&1) == *$1* ]]; then
255 return 0
256 else
257 return 1
258 fi
259}
260
231strip-unsupported-flags() { 261strip-unsupported-flags() {
262 local NEW_CFLAGS NEW_CXXFLAGS
263
232 for x in ${CFLAGS} ; do 264 for x in ${CFLAGS} ; do
233 NEW_CFLAGS=${NEW_CFLAGS}" ""`test_flag ${x}`" 265 NEW_CFLAGS="${NEW_CFLAGS} `test_flag ${x}`"
234 done 266 done
235 for x in ${CXXFLAGS} ; do 267 for x in ${CXXFLAGS} ; do
236 NEW_CXXFLAGS=${NEW_CXXFLAGS}" ""`test_flag ${x}`" 268 NEW_CXXFLAGS="${NEW_CXXFLAGS} `test_flag ${x}`"
237 done 269 done
238 270
239 export CFLAGS="${NEW_CFLAGS}" 271 export CFLAGS="${NEW_CFLAGS}"
240 export CXXFLAGS="${NEW_CXXFLAGS}" 272 export CXXFLAGS="${NEW_CXXFLAGS}"
241} 273}
242 274
243get-flag() { 275get-flag() {
276 local f findflag="$1"
277
244 # this code looks a little flaky but seems to work for 278 # this code looks a little flaky but seems to work for
245 # everything we want ... 279 # everything we want ...
246 # for example, if CFLAGS="-march=i686": 280 # for example, if CFLAGS="-march=i686":
247 # `get-flags -march` == "-march=i686" 281 # `get-flag -march` == "-march=i686"
248 # `get-flags march` == "i686" 282 # `get-flag march` == "i686"
249 local findflag="$1"
250 for f in ${CFLAGS} ${CXXFLAGS} ; do 283 for f in ${CFLAGS} ${CXXFLAGS} ; do
251 if [ "${f/${findflag}}" != "${f}" ] ; then 284 if [ "${f/${findflag}}" != "${f}" ] ; then
252 echo "${f/-${findflag}=}" 285 printf "%s\n" "${f/-${findflag}=}"
253 return 0 286 return 0
254 fi 287 fi
255 done 288 done
256 return 1 289 return 1
257} 290}
258 291
292has_hardened() {
293 test_version_info Hardened
294 return $?
295}
296
297has_pic() {
298 [ "${CFLAGS/-fPIC}" != "${CFLAGS}" ] && return 0
299 [ "${CFLAGS/-fpic}" != "${CFLAGS}" ] && return 0
300 test_version_info pie && return 0
301 return 1
302}
303
304has_pie() {
305 [ "${CFLAGS/-fPIE}" != "${CFLAGS}" ] && return 0
306 [ "${CFLAGS/-fpie}" != "${CFLAGS}" ] && return 0
307 test_version_info pie && return 0
308 return 1
309}
310
311has_ssp() {
312 [ "${CFLAGS/-fstack-protector}" != "${CFLAGS}" ] && return 0
313 test_version_info ssp && return 0
314 return 1
315}
316
317has_m64() {
318 # this doesnt test if the flag is accepted, it tests if the flag
319 # actually -WORKS-. non-multilib gcc will take both -m32 and -m64!
320 # please dont replace this function with test_flag in some future
321 # clean-up!
322 temp=`mktemp`
323 echo "int main() { return(0); }" > ${temp}.c
324 ${CC/ .*/} -m64 -o /dev/null ${temp}.c > /dev/null 2>&1
325 ret=$?
326 rm -f ${temp}.c
327 [ "$ret" != "1" ] && return 0
328 return 1
329}
330
331has_m32() {
332 # this doesnt test if the flag is accepted, it tests if the flag
333 # actually -WORKS-. non-multilib gcc will take both -m32 and -m64!
334 # please dont replace this function with test_flag in some future
335 # clean-up!
336 temp=`mktemp`
337 echo "int main() { return(0); }" > ${temp}.c
338 ${CC/ .*/} -m32 -o /dev/null ${temp}.c > /dev/null 2>&1
339 ret=$?
340 rm -f ${temp}.c
341 [ "$ret" != "1" ] && return 0
342 return 1
343}
344
259replace-sparc64-flags() { 345replace-sparc64-flags() {
260 local SPARC64_CPUS="ultrasparc v9" 346 local SPARC64_CPUS="ultrasparc v9"
261 347
262 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ] 348 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
263 then
264 for x in ${SPARC64_CPUS} 349 for x in ${SPARC64_CPUS}; do
265 do
266 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}" 350 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
267 done 351 done
268 else 352 else
269 for x in ${SPARC64_CPUS} 353 for x in ${SPARC64_CPUS}; do
270 do
271 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" 354 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
272 done 355 done
273 fi 356 fi
274 357
275 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ] 358 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
276 then
277 for x in ${SPARC64_CPUS} 359 for x in ${SPARC64_CPUS}; do
278 do
279 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}" 360 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
280 done 361 done
281 else 362 else
282 for x in ${SPARC64_CPUS} 363 for x in ${SPARC64_CPUS}; do
283 do
284 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" 364 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
285 done 365 done
286 fi 366 fi
367
368 export CFLAGS CXXFLAGS
287} 369}
288 370
289append-ldflags() { 371append-ldflags() {
372 export LDFLAGS="${LDFLAGS} $*"
373 return 0
374}
375
376filter-ldflags() {
377 local x
378
379 # we do this fancy spacing stuff so as to not filter
380 # out part of a flag ... we want flag atoms ! :D
290 LDFLAGS="${LDFLAGS} $@" 381 LDFLAGS=" ${LDFLAGS} "
382 for x in "$@" ; do
383 LDFLAGS="${LDFLAGS// ${x} / }"
384 done
385 LDFLAGS="${LDFLAGS:1:${#LDFLAGS}-2}"
386 export LDFLAGS
291 return 0 387 return 0
292} 388}
293 389
294etexec-flags() { 390etexec-flags() {
295 has_version sys-devel/hardened-gcc 391 # if you're not using a hardened compiler you wont need this
296 if [ $? == 0 ] ; then 392 # PIC/no-pic kludge in the first place.
297 if [ "`is-flag -yet_exec`" != "true" ]; then 393 has_hardened || return 0
298 debug-print ">>> appending flags -yet_exec" 394
299 append-flags -yet_exec 395 if has_pie || has_pic; then
300 append-ldflags -yet_exec 396 [ -z "`is-flag -fno-pic`" ] &&
397 export CFLAGS="${CFLAGS} `test_flag -fno-pic`"
398 [ -z "`is-flag -nopie`" ] &&
399 export CFLAGS="${CFLAGS} `test_flag -nopie`"
301 fi 400 fi
302 fi 401 return 0
303} 402}
304 403
305fstack-flags() { 404fstack-flags() {
306 has_version sys-devel/hardened-gcc 405 if has_ssp; then
307 if [ $? == 0 ] ; then 406 [ -z "`is-flag -fno-stack-protector`" ] &&
308 if [ "`is-flag -yno_propolice`" != "true" ]; then 407 export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`"
309 debug-print ">>> appending flags -yno_propolice"
310 append-flags -yno_propolice
311 append-ldflags -yno_propolice
312 fi 408 fi
313 fi 409 return 0
314} 410}

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

  ViewVC Help
Powered by ViewVC 1.1.20