/[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.10 Revision 1.40
1# Copyright 1999-2002 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.40 2004/03/12 11:21:15 vapier Exp $
4#
3# Author Bart Verwilst <verwilst@gentoo.org> 5# Author Bart Verwilst <verwilst@gentoo.org>
4# $Header: /var/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.10 2002/11/25 04:39:33 vapier Exp $
5 6
6ECLASS=flag-o-matic 7ECLASS=flag-o-matic
7INHERITED="$INHERITED $ECLASS" 8INHERITED="$INHERITED $ECLASS"
8 9
9# 10#
10#### filter-flags <flag> #### 11#### filter-flags <flags> ####
11# Remove particular flags from C[XX]FLAGS 12# Remove particular flags from C[XX]FLAGS
13# Matches only complete flags
12# 14#
13#### append-flags <flag> #### 15#### append-flags <flags> ####
14# Add extra flags to your current C[XX]FLAGS 16# Add extra flags to your current C[XX]FLAGS
15# 17#
16#### replace-flags <orig.flag> <new.flag> ### 18#### replace-flags <orig.flag> <new.flag> ###
17# Replace a flag by another one 19# Replace a flag by another one
18# 20#
21#### replace-cpu-flags <new.cpu> <old.cpus> ###
22# Replace march/mcpu flags that specify <old.cpus>
23# with flags that specify <new.cpu>
24#
19#### is-flag <flag> #### 25#### is-flag <flag> ####
20# Returns "true" if flag is set in C[XX]FLAGS 26# Returns "true" if flag is set in C[XX]FLAGS
21# Matches only complete flag 27# Matches only complete a flag
22# 28#
23#### strip-flags #### 29#### strip-flags ####
24# Strip C[XX]FLAGS of everything except known 30# Strip C[XX]FLAGS of everything except known
25# good options. 31# good options.
26# 32#
33#### strip-unsupported-flags ####
34# Strip C[XX]FLAGS of any flags not supported by
35# installed version of gcc
36#
27#### get-flag <flag> #### 37#### get-flag <flag> ####
28# Find and echo the value for a particular flag 38# Find and echo the value for a particular flag
29# 39#
40#### replace-sparc64-flags ####
41# Sets mcpu to v8 and uses the original value
42# as mtune if none specified.
43#
44#### filter-mfpmath <math types> ####
45# Remove specified math types from the fpmath specification
46# If the user has -mfpmath=sse,386, running `filter-mfpmath sse`
47# will leave the user with -mfpmath=386
48#
49#### append-ldflags ####
50# Add extra flags to your current LDFLAGS
51#
52#### etexec-flags ####
53# hooked function for hardened-gcc that appends
54# -yet_exec {C,CXX,LD}FLAGS when hardened-gcc is installed
55# and a package is filtering -fPIC,-fpic, -fPIE, -fpie
56#
57#### fstack-flags ####
58# hooked function for hardened-gcc that appends
59# -yno_propolice to {C,CXX,LD}FLAGS when hardened-gcc is installed
60# and a package is filtering -fstack-protector, -fstack-protector-all
61#
30 62
63# C[XX]FLAGS that we allow in strip-flags
64setup-allowed-flags() {
65 export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -pipe -g"
66 case "${ARCH}" in
67 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;;
68 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
69 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
70 ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
71 esac
72
73 # C[XX]FLAGS that we are think is ok, but needs testing
74 # NOTE: currently -Os have issues with gcc3 and K6* arch's
75 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays"
76 return 0
77}
78
31filter-flags () { 79filter-flags() {
32 80 for x in "$@" ; do
33 for x in $1 81 case "${x}" in
82 -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;;
83 -fstack-protector|-fstack-protector-all) fstack-flags;;
84 *) ;;
85 esac
34 do 86 done
87
88 # we do this fancy spacing stuff so as to not filter
89 # out part of a flag ... we want flag atoms ! :D
90 CFLAGS=" ${CFLAGS} "
91 CXXFLAGS=" ${CXXFLAGS} "
92 for x in "$@" ; do
35 export CFLAGS="${CFLAGS/${x}}" 93 CFLAGS="${CFLAGS// ${x} / }"
36 export CXXFLAGS="${CXXFLAGS/${x}}" 94 CXXFLAGS="${CXXFLAGS// ${x} / }"
37 done 95 done
38 96 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
97 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
98 return 0
39} 99}
40 100
101filter-lfs-flags() {
102 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
103}
104
41append-flags () { 105append-flags() {
42
43 CFLAGS="${CFLAGS} $1" 106 export CFLAGS="${CFLAGS} $@"
44 CXXFLAGS="${CXXFLAGS} $1" 107 export CXXFLAGS="${CXXFLAGS} $@"
45 108 [ "`is-flag -fno-stack-protector`" -o "`is-flag -fno-stack-protector-all`" ] && fstack-flags
109 return 0
46} 110}
47 111
48replace-flags () { 112replace-flags() {
49 113 # we do this fancy spacing stuff so as to not filter
114 # out part of a flag ... we want flag atoms ! :D
115 CFLAGS=" ${CFLAGS} "
116 CXXFLAGS=" ${CXXFLAGS} "
50 CFLAGS="${CFLAGS/${1}/${2} }" 117 CFLAGS="${CFLAGS// ${1} / ${2} }"
51 CXXFLAGS="${CXXFLAGS/${1}/${2} }" 118 CXXFLAGS="${CXXFLAGS// ${1} / ${2} }"
119 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
120 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
121 return 0
122}
52 123
124replace-cpu-flags() {
125 local newcpu="$1" ; shift
126 local oldcpu=""
127 for oldcpu in "$@" ; do
128 replace-flags -march=${oldcpu} -march=${newcpu}
129 replace-flags -mcpu=${oldcpu} -mcpu=${newcpu}
130 replace-flags -mtune=${oldcpu} -mtune=${newcpu}
131 done
132 return 0
53} 133}
54 134
55is-flag() { 135is-flag() {
56
57 for x in ${CFLAGS} ${CXXFLAGS} 136 for x in ${CFLAGS} ${CXXFLAGS} ; do
58 do
59 if [ "${x}" = "$1" ] 137 if [ "${x}" == "$1" ] ; then
60 then
61 echo true 138 echo true
62 break 139 return 0
63 fi 140 fi
64 done 141 done
142 return 1
143}
65 144
145filter-mfpmath() {
146 # save the original -mfpmath flag
147 local orig_mfpmath="`get-flag -mfpmath`"
148 # get the value of the current -mfpmath flag
149 local new_math=" `get-flag mfpmath | tr , ' '` "
150 # figure out which math values are to be removed
151 local prune_math=""
152 for prune_math in "$@" ; do
153 new_math="${new_math/ ${prune_math} / }"
154 done
155 new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`"
156
157 if [ -z "${new_math}" ] ; then
158 # if we're removing all user specified math values are
159 # slated for removal, then we just filter the flag
160 filter-flags ${orig_mfpmath}
161 else
162 # if we only want to filter some of the user specified
163 # math values, then we replace the current flag
164 replace-flags ${orig_mfpmath} -mfpmath=${new_math}
165 fi
166 return 0
66} 167}
67 168
68strip-flags() { 169strip-flags() {
170 setup-allowed-flags
69 171
70 local NEW_CFLAGS="" 172 local NEW_CFLAGS=""
71 local NEW_CXXFLAGS="" 173 local NEW_CXXFLAGS=""
72 174
73 local ALLOWED_FLAGS="-O -mcpu -march -pipe" 175 # Allow unstable C[XX]FLAGS if we are using unstable profile ...
176 if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then
177 [ `use debug` ] && einfo "Enabling the use of some unstable flags"
178 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}"
179 fi
74 180
75 set -f 181 set -f
76 182
77 for x in ${CFLAGS} 183 for x in ${CFLAGS}
78 do 184 do
79 for y in ${ALLOWED_FLAGS} 185 for y in ${ALLOWED_FLAGS}
80 do 186 do
187 flag=${x%%=*}
81 if [ "${x/${y}}" != "${x}" ] 188 if [ "${flag%%${y}}" = "" ]
82 then 189 then
83 if [ -z "${NEW_CFLAGS}" ]
84 then
85 NEW_CFLAGS="${x}"
86 else
87 NEW_CFLAGS="${NEW_CFLAGS} ${x}" 190 NEW_CFLAGS="${NEW_CFLAGS} ${x}"
88 fi 191 break
89 fi 192 fi
90 done 193 done
91 done 194 done
92 195
93 for x in ${CXXFLAGS} 196 for x in ${CXXFLAGS}
94 do 197 do
95 for y in ${ALLOWED_FLAGS} 198 for y in ${ALLOWED_FLAGS}
96 do 199 do
200 flag=${x%%=*}
97 if [ "${x/${y}}" != "${x}" ] 201 if [ "${flag%%${y}}" = "" ]
98 then 202 then
99 if [ -z "${NEW_CXXFLAGS}" ]
100 then
101 NEW_CXXFLAGS="${x}"
102 else
103 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}" 203 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}"
104 fi 204 break
105 fi 205 fi
106 done 206 done
107 done 207 done
108 208
209 # In case we filtered out all optimization flags fallback to -O2
210 if [ "${CFLAGS/-O}" != "${CFLAGS}" -a "${NEW_CFLAGS/-O}" = "${NEW_CFLAGS}" ]; then
211 NEW_CFLAGS="${NEW_CFLAGS} -O2"
212 fi
213 if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then
214 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2"
215 fi
216
109 set +f 217 set +f
218
219 [ `use debug` ] \
220 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \
221 && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\""
110 222
111 export CFLAGS="${NEW_CFLAGS}" 223 export CFLAGS="${NEW_CFLAGS}"
112 export CXXFLAGS="${NEW_CXXFLAGS}" 224 export CXXFLAGS="${NEW_CXXFLAGS}"
225 return 0
226}
227
228test_flag() {
229 if gcc -S -xc "$@" -o /dev/null /dev/null >/dev/null 2>&1; then
230 echo "$@"
231 return 0
232 fi
233 return 1
234}
235
236strip-unsupported-flags() {
237 for x in ${CFLAGS} ; do
238 NEW_CFLAGS=${NEW_CFLAGS}" ""`test_flag ${x}`"
239 done
240 for x in ${CXXFLAGS} ; do
241 NEW_CXXFLAGS=${NEW_CXXFLAGS}" ""`test_flag ${x}`"
242 done
243
244 export CFLAGS="${NEW_CFLAGS}"
245 export CXXFLAGS="${NEW_CXXFLAGS}"
113} 246}
114 247
115get-flag() { 248get-flag() {
249 # this code looks a little flaky but seems to work for
250 # everything we want ...
251 # for example, if CFLAGS="-march=i686":
252 # `get-flags -march` == "-march=i686"
253 # `get-flags march` == "i686"
116 local findflag="$1" 254 local findflag="$1"
117
118 for f in ${CFLAGS} ${CXXFLAGS} ; do 255 for f in ${CFLAGS} ${CXXFLAGS} ; do
119 if [ "${f/${findflag}}" != "${f}" ] ; then 256 if [ "${f/${findflag}}" != "${f}" ] ; then
120 echo "${f/-${findflag}=}" 257 echo "${f/-${findflag}=}"
121 return 258 return 0
122 fi 259 fi
123 done 260 done
261 return 1
124} 262}
263
264replace-sparc64-flags() {
265 local SPARC64_CPUS="ultrasparc v9"
266
267 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]
268 then
269 for x in ${SPARC64_CPUS}
270 do
271 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
272 done
273 else
274 for x in ${SPARC64_CPUS}
275 do
276 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
277 done
278 fi
279
280 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]
281 then
282 for x in ${SPARC64_CPUS}
283 do
284 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
285 done
286 else
287 for x in ${SPARC64_CPUS}
288 do
289 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
290 done
291 fi
292}
293
294append-ldflags() {
295 LDFLAGS="${LDFLAGS} $@"
296 return 0
297}
298
299etexec-flags() {
300 has_version sys-devel/hardened-gcc
301 if [ $? == 0 ] ; then
302 if [ "`is-flag -yet_exec`" != "true" ]; then
303 debug-print ">>> appending flags -yet_exec"
304 append-flags -yet_exec
305 append-ldflags -yet_exec
306 fi
307 fi
308}
309
310fstack-flags() {
311 has_version sys-devel/hardened-gcc
312 if [ $? == 0 ] ; then
313 if [ "`is-flag -yno_propolice`" != "true" ]; then
314 debug-print ">>> appending flags -yno_propolice"
315 append-flags -yno_propolice
316 append-ldflags -yno_propolice
317 fi
318 fi
319}

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.40

  ViewVC Help
Powered by ViewVC 1.1.20