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

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.49

  ViewVC Help
Powered by ViewVC 1.1.20