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

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

  ViewVC Help
Powered by ViewVC 1.1.20