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

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

  ViewVC Help
Powered by ViewVC 1.1.20