/[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.24 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.24 2003/07/22 12:48:11 aliz 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
16# Add extra flags to your current C[XX]FLAGS 18# Add extra flags to your current C[XX]FLAGS
17# 19#
18#### replace-flags <orig.flag> <new.flag> ### 20#### replace-flags <orig.flag> <new.flag> ###
19# Replace a flag by another one 21# Replace a flag by another one
20# 22#
23#### replace-cpu-flags <new.cpu> <old.cpus> ###
24# Replace march/mcpu flags that specify <old.cpus>
25# with flags that specify <new.cpu>
26#
21#### is-flag <flag> #### 27#### is-flag <flag> ####
22# Returns "true" if flag is set in C[XX]FLAGS 28# Returns "true" if flag is set in C[XX]FLAGS
23# Matches only complete a flag 29# Matches only complete a flag
24# 30#
25#### strip-flags #### 31#### strip-flags ####
26# Strip C[XX]FLAGS of everything except known 32# Strip C[XX]FLAGS of everything except known
27# good options. 33# good options.
28# 34#
35#### strip-unsupported-flags ####
36# Strip C[XX]FLAGS of any flags not supported by
37# installed version of gcc
38#
29#### get-flag <flag> #### 39#### get-flag <flag> ####
30# Find and echo the value for a particular flag 40# Find and echo the value for a particular flag
31# 41#
32#### replace-sparc64-flags #### 42#### replace-sparc64-flags ####
33# Sets mcpu to v8 and uses the original value 43# Sets mcpu to v8 and uses the original value
34# as mtune if none specified. 44# as mtune if none specified.
35# 45#
36 46#### filter-mfpmath <math types> ####
47# Remove specified math types from the fpmath specification
48# If the user has -mfpmath=sse,386, running `filter-mfpmath sse`
49# will leave the user with -mfpmath=386
50#
51#### append-ldflags ####
52# Add extra flags to your current LDFLAGS
53#
54#### filter-ldflags <flags> ####
55# Remove particular flags from LDFLAGS
56# Matches only complete flags
57#
58#### etexec-flags ####
59# hooked function for hardened gcc that appends
60# -fno-pic to {C,CXX,LD}FLAGS
61# when a package is filtering -fpic, -fPIC, -fpie, -fPIE
62#
63#### fstack-flags ####
64# hooked function for hardened gcc that appends
65# -fno-stack-protector to {C,CXX,LD}FLAGS
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
69#
37 70
38# C[XX]FLAGS that we allow in strip-flags 71# C[XX]FLAGS that we allow in strip-flags
72setup-allowed-flags() {
73 if [ -z "${ALLOWED_FLAGS}" ] ; then
39ALLOWED_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"
40case "${ARCH}" in 75 case "${ARCH}" in
41 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; 76 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;;
42 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 77 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC -m64" ;;
78 x86) ALLOWED_FLAGS="${ALLOWED_FLAGS} -m32" ;;
79 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
80 ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
43esac 81 esac
82 fi
44 83
45# 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
46# NOTE: currently -Os have issues with gcc3 and K6* arch's 85 # NOTE: currently -Os have issues with gcc3 and K6* arch's
47UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" 86 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays"
48 87 return 0
49filter-mfpmath() {
50 for a in $CFLAGS; do
51 if [ "${a:0:8}" == "-mfpmath" ]; then
52 orig_mfpmath=$a
53 fi
54 done
55
56 mfpmath="$( echo $orig_mfpmath | awk -F '=' '{print $2}' | tr "," " " )"
57 for b in $@; do
58 mfpmath="${mfpmath/$b}"
59 done
60
61 if [ -z "${mfpmath/ }" ]; then
62 filter-flags "$orig_mfpmath"
63 else
64 new_mfpmath="-mfpmath=$( echo $mfpmath | sed -e "s/ /,/g" -e "s/,,/,/g" )"
65 replace-flags "$orig_mfpmath" "$new_mfpmath"
66 fi
67} 88}
68 89
69filter-flags() { 90filter-flags() {
91 local x f fset
92 declare -a new_CFLAGS new_CXXFLAGS
93
94 for x in "$@" ; do
95 case "${x}" in
96 -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;;
97 -fstack-protector|-fstack-protector-all) fstack-flags;;
98 esac
99 done
100
101 for fset in CFLAGS CXXFLAGS; do
102 for f in ${!fset}; do
103 for x in "$@"; do
104 # Note this should work with globs like -O*
105 [[ ${f} == ${x} ]] && continue 2
106 done
107 eval new_${fset}\[\${\#new_${fset}\[@]}]=\${f}
108 done
109 eval export ${fset}=\${new_${fset}\[*]}
110 done
111
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
121}
122
123append-flags() {
124 export CFLAGS="${CFLAGS} $*"
125 export CXXFLAGS="${CXXFLAGS} $*"
126 [ -n "`is-flag -fno-stack-protector`" -o \
127 -n "`is-flag -fno-stack-protector-all`" ] && fstack-flags
128 return 0
129}
130
131replace-flags() {
70 # we do this fancy spacing stuff so as to not filter 132 # we do this fancy spacing stuff so as to not filter
71 # out part of a flag ... we want flag atoms ! :D 133 # out part of a flag ... we want flag atoms ! :D
72 export CFLAGS=" ${CFLAGS} "
73 export CXXFLAGS=" ${CXXFLAGS} "
74 for x in $@ ; do
75 export CFLAGS="${CFLAGS/ ${x} / }"
76 export CXXFLAGS="${CXXFLAGS/ ${x} / }"
77 done
78 export CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
79 export CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
80}
81
82append-flags() {
83 CFLAGS="${CFLAGS} $@" 134 CFLAGS=" ${CFLAGS} "
84 CXXFLAGS="${CXXFLAGS} $@" 135 CXXFLAGS=" ${CXXFLAGS} "
85}
86
87replace-flags() {
88 CFLAGS="${CFLAGS/${1}/${2} }" 136 CFLAGS="${CFLAGS// ${1} / ${2} }"
89 CXXFLAGS="${CXXFLAGS/${1}/${2} }" 137 CXXFLAGS="${CXXFLAGS// ${1} / ${2} }"
138 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
139 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
140 export CFLAGS CXXFLAGS
141 return 0
142}
143
144replace-cpu-flags() {
145 local oldcpu newcpu="$1" ; shift
146 for oldcpu in "$@" ; do
147 replace-flags -march=${oldcpu} -march=${newcpu}
148 replace-flags -mcpu=${oldcpu} -mcpu=${newcpu}
149 replace-flags -mtune=${oldcpu} -mtune=${newcpu}
150 done
151 return 0
90} 152}
91 153
92is-flag() { 154is-flag() {
155 local x
156
93 for x in ${CFLAGS} ${CXXFLAGS} ; do 157 for x in ${CFLAGS} ${CXXFLAGS} ; do
94 if [ "${x}" == "$1" ] ; then 158 if [ "${x}" == "$1" ] ; then
95 echo true 159 echo true
96 return 0 160 return 0
97 fi 161 fi
98 done 162 done
99 return 1 163 return 1
100} 164}
101 165
166filter-mfpmath() {
167 local orig_mfpmath new_math prune_math
168
169 # save the original -mfpmath flag
170 orig_mfpmath="`get-flag -mfpmath`"
171 # get the value of the current -mfpmath flag
172 new_math=" `get-flag mfpmath | tr , ' '` "
173 # figure out which math values are to be removed
174 prune_math=""
175 for prune_math in "$@" ; do
176 new_math="${new_math/ ${prune_math} / }"
177 done
178 new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`"
179
180 if [ -z "${new_math}" ] ; then
181 # if we're removing all user specified math values are
182 # slated for removal, then we just filter the flag
183 filter-flags ${orig_mfpmath}
184 else
185 # if we only want to filter some of the user specified
186 # math values, then we replace the current flag
187 replace-flags ${orig_mfpmath} -mfpmath=${new_math}
188 fi
189 return 0
190}
191
102strip-flags() { 192strip-flags() {
193 local x y flag NEW_CFLAGS NEW_CXXFLAGS
194
195 setup-allowed-flags
196
103 local NEW_CFLAGS="" 197 local NEW_CFLAGS=""
104 local NEW_CXXFLAGS="" 198 local NEW_CXXFLAGS=""
105 199
106 # Allow unstable C[XX]FLAGS if we are using unstable profile ... 200 # Allow unstable C[XX]FLAGS if we are using unstable profile ...
107 if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then 201 if has ~${ARCH} ${ACCEPT_KEYWORDS} ; then
108 [ `use debug` ] && einfo "Enabling the use of some unstable flags" 202 use debug && einfo "Enabling the use of some unstable flags"
109 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}" 203 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}"
110 fi 204 fi
111 205
112 set -f 206 set -f # disable pathname expansion
113 207
114 for x in ${CFLAGS} 208 for x in ${CFLAGS}; do
115 do
116 for y in ${ALLOWED_FLAGS} 209 for y in ${ALLOWED_FLAGS}; do
117 do
118 flag=${x%%=*} 210 flag=${x%%=*}
119 if [ "${flag%%${y}}" = "" ] 211 if [ "${flag%%${y}}" = "" ] || [ "${flag:0:5}" = "-fno-" ] || [ "${flag:0:5}" = "-mno-" ] ; then
120 then
121 NEW_CFLAGS="${NEW_CFLAGS} ${x}" 212 NEW_CFLAGS="${NEW_CFLAGS} ${x}"
122 break 213 break
123 fi 214 fi
124 done 215 done
125 done 216 done
126 217
127 for x in ${CXXFLAGS} 218 for x in ${CXXFLAGS}; do
128 do
129 for y in ${ALLOWED_FLAGS} 219 for y in ${ALLOWED_FLAGS}; do
130 do
131 flag=${x%%=*} 220 flag=${x%%=*}
132 if [ "${flag%%${y}}" = "" ] 221 if [ "${flag%%${y}}" = "" ] || [ "${flag:0:5}" = "-fno-" ] || [ "${flag:0:5}" = "-mno-" ] ; then
133 then
134 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}" 222 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}"
135 break 223 break
136 fi 224 fi
137 done 225 done
138 done 226 done
139 227
140 set +f 228 # In case we filtered out all optimization flags fallback to -O2
229 if [ "${CFLAGS/-O}" != "${CFLAGS}" -a "${NEW_CFLAGS/-O}" = "${NEW_CFLAGS}" ]; then
230 NEW_CFLAGS="${NEW_CFLAGS} -O2"
231 fi
232 if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then
233 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2"
234 fi
141 235
142 [ `use debug` ] \ 236 set +f # re-enable pathname expansion
143 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \
144 && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\""
145 237
146 export CFLAGS="${NEW_CFLAGS}" 238 export CFLAGS="${NEW_CFLAGS}"
147 export CXXFLAGS="${NEW_CXXFLAGS}" 239 export CXXFLAGS="${NEW_CXXFLAGS}"
240 return 0
241}
242
243test_flag() {
244 local cc=${CC:-gcc} ; cc=${cc%% *}
245 if ${cc} -S -xc "$@" -o /dev/null /dev/null &>/dev/null; then
246 printf "%s\n" "$*"
247 return 0
248 fi
249 return 1
250}
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
261strip-unsupported-flags() {
262 local NEW_CFLAGS NEW_CXXFLAGS
263
264 for x in ${CFLAGS} ; do
265 NEW_CFLAGS="${NEW_CFLAGS} `test_flag ${x}`"
266 done
267 for x in ${CXXFLAGS} ; do
268 NEW_CXXFLAGS="${NEW_CXXFLAGS} `test_flag ${x}`"
269 done
270
271 export CFLAGS="${NEW_CFLAGS}"
272 export CXXFLAGS="${NEW_CXXFLAGS}"
148} 273}
149 274
150get-flag() { 275get-flag() {
151 local findflag="$1" 276 local f findflag="$1"
152 277
278 # this code looks a little flaky but seems to work for
279 # everything we want ...
280 # for example, if CFLAGS="-march=i686":
281 # `get-flag -march` == "-march=i686"
282 # `get-flag march` == "i686"
153 for f in ${CFLAGS} ${CXXFLAGS} ; do 283 for f in ${CFLAGS} ${CXXFLAGS} ; do
154 if [ "${f/${findflag}}" != "${f}" ] ; then 284 if [ "${f/${findflag}}" != "${f}" ] ; then
155 echo "${f/-${findflag}=}" 285 printf "%s\n" "${f/-${findflag}=}"
156 return 286 return 0
157 fi 287 fi
158 done 288 done
289 return 1
290}
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
159} 343}
160 344
161replace-sparc64-flags() { 345replace-sparc64-flags() {
162 local SPARC64_CPUS="ultrasparc v9" 346 local SPARC64_CPUS="ultrasparc v9"
163 347
164 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ] 348 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
165 then
166 for x in ${SPARC64_CPUS} 349 for x in ${SPARC64_CPUS}; do
167 do
168 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}" 350 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
169 done 351 done
170 else 352 else
171 for x in ${SPARC64_CPUS} 353 for x in ${SPARC64_CPUS}; do
172 do
173 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" 354 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
174 done 355 done
175 fi 356 fi
176 357
177 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ] 358 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
178 then
179 for x in ${SPARC64_CPUS} 359 for x in ${SPARC64_CPUS}; do
180 do
181 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}" 360 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
182 done 361 done
183 else 362 else
184 for x in ${SPARC64_CPUS} 363 for x in ${SPARC64_CPUS}; do
185 do
186 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" 364 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
187 done 365 done
188 fi 366 fi
367
368 export CFLAGS CXXFLAGS
189} 369}
370
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
381 LDFLAGS=" ${LDFLAGS} "
382 for x in "$@" ; do
383 LDFLAGS="${LDFLAGS// ${x} / }"
384 done
385 LDFLAGS="${LDFLAGS:1:${#LDFLAGS}-2}"
386 export LDFLAGS
387 return 0
388}
389
390etexec-flags() {
391 # if you're not using a hardened compiler you wont need this
392 # PIC/no-pic kludge in the first place.
393 has_hardened || return 0
394
395 if has_pie || has_pic; then
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`"
400 fi
401 return 0
402}
403
404fstack-flags() {
405 if has_ssp; then
406 [ -z "`is-flag -fno-stack-protector`" ] &&
407 export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`"
408 fi
409 return 0
410}

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

  ViewVC Help
Powered by ViewVC 1.1.20