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

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

  ViewVC Help
Powered by ViewVC 1.1.20