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

Legend:
Removed from v.1.47  
changed lines
  Added in v.1.62

  ViewVC Help
Powered by ViewVC 1.1.20