/[gentoo-x86]/eclass/flag-o-matic.eclass
Gentoo

Contents of /eclass/flag-o-matic.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.58 - (hide annotations) (download)
Thu Jun 10 15:28:56 2004 UTC (9 years, 10 months ago) by agriffis
Branch: MAIN
Changes since 1.57: +76 -78 lines
- Localize variables in functions
- Rewrite test_flag to honor ${CC}
- Add function test_version_info to run ${CC} --version and look for
  patterns
- Use printf instead of echo in situations where flags are being
  printed.  This avoids the possibility that echo will interpret the
  flags, for example -n
- Modify has_hardened, has_pic, has_pie to use test_version_info.
  This also fixes mistakes in ${var/pattern} expressions; many
  patterns in this eclass were erroneously written to be regular
  expressions instead of globs.
- Modify has_m64, has_m32 to use test_flag

1 vapier 1.12 # Copyright 1999-2003 Gentoo Technologies, Inc.
2 verwilst 1.1 # Distributed under the terms of the GNU General Public License v2
3 agriffis 1.58 # $Header: /var/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.57 2004/06/10 00:41:44 lv Exp $
4 vapier 1.12 #
5 verwilst 1.1 # Author Bart Verwilst <verwilst@gentoo.org>
6 azarah 1.8
7 verwilst 1.1 ECLASS=flag-o-matic
8 danarmak 1.6 INHERITED="$INHERITED $ECLASS"
9 lv 1.55 IUSE="$IUSE debug"
10 azarah 1.8
11 verwilst 1.2 #
12 vapier 1.18 #### filter-flags <flags> ####
13 verwilst 1.3 # Remove particular flags from C[XX]FLAGS
14 vapier 1.18 # Matches only complete flags
15 verwilst 1.2 #
16 vapier 1.18 #### append-flags <flags> ####
17 verwilst 1.3 # Add extra flags to your current C[XX]FLAGS
18     #
19 verwilst 1.4 #### replace-flags <orig.flag> <new.flag> ###
20 verwilst 1.3 # Replace a flag by another one
21     #
22 vapier 1.37 #### replace-cpu-flags <new.cpu> <old.cpus> ###
23     # Replace march/mcpu flags that specify <old.cpus>
24     # with flags that specify <new.cpu>
25     #
26 danarmak 1.7 #### is-flag <flag> ####
27     # Returns "true" if flag is set in C[XX]FLAGS
28 vapier 1.18 # Matches only complete a flag
29 danarmak 1.7 #
30 azarah 1.8 #### strip-flags ####
31     # Strip C[XX]FLAGS of everything except known
32     # good options.
33     #
34 puggy 1.29 #### strip-unsupported-flags ####
35     # Strip C[XX]FLAGS of any flags not supported by
36     # installed version of gcc
37     #
38 vapier 1.10 #### get-flag <flag> ####
39     # Find and echo the value for a particular flag
40     #
41 joker 1.16 #### replace-sparc64-flags ####
42     # Sets mcpu to v8 and uses the original value
43     # as mtune if none specified.
44     #
45 vapier 1.26 #### 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 solar 1.27 #
50     #### append-ldflags ####
51     # Add extra flags to your current LDFLAGS
52     #
53 solar 1.41 #### filter-ldflags <flags> ####
54     # Remove particular flags from LDFLAGS
55     # Matches only complete flags
56     #
57 solar 1.27 #### etexec-flags ####
58 pappy 1.50 # 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 solar 1.27 #
62     #### fstack-flags ####
63 pappy 1.50 # 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 solar 1.27 #
69 verwilst 1.3
70 azarah 1.17 # C[XX]FLAGS that we allow in strip-flags
71 vapier 1.36 setup-allowed-flags() {
72 vapier 1.44 if [ -z "${ALLOWED_FLAGS}" ] ; then
73 lv 1.52 export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -fno-unit-at-a-time -pipe -g"
74 vapier 1.44 case "${ARCH}" in
75     mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;;
76 lv 1.51 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC -m64" ;;
77     x86) ALLOWED_FLAGS="${ALLOWED_FLAGS} -m32" ;;
78 vapier 1.44 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
79     ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
80     esac
81     fi
82 vapier 1.36
83     # C[XX]FLAGS that we are think is ok, but needs testing
84     # NOTE: currently -Os have issues with gcc3 and K6* arch's
85     export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays"
86     return 0
87 aliz 1.24 }
88 verwilst 1.13
89 vapier 1.18 filter-flags() {
90 agriffis 1.58 local x
91    
92 vapier 1.36 for x in "$@" ; do
93 solar 1.27 case "${x}" in
94 solar 1.39 -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;;
95 solar 1.32 -fstack-protector|-fstack-protector-all) fstack-flags;;
96 solar 1.27 *) ;;
97     esac
98     done
99 vapier 1.36
100 vapier 1.18 # we do this fancy spacing stuff so as to not filter
101     # out part of a flag ... we want flag atoms ! :D
102 agriffis 1.25 CFLAGS=" ${CFLAGS} "
103     CXXFLAGS=" ${CXXFLAGS} "
104 vapier 1.36 for x in "$@" ; do
105 vapier 1.37 CFLAGS="${CFLAGS// ${x} / }"
106     CXXFLAGS="${CXXFLAGS// ${x} / }"
107 verwilst 1.1 done
108 agriffis 1.25 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
109     CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
110 vapier 1.36 return 0
111 verwilst 1.2 }
112    
113 vapier 1.40 filter-lfs-flags() {
114     filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
115     }
116    
117 vapier 1.48 append-lfs-flags() {
118     append-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
119     }
120    
121 vapier 1.18 append-flags() {
122 agriffis 1.58 export CFLAGS="${CFLAGS} $*"
123     export CXXFLAGS="${CXXFLAGS} $*"
124     [ -n "`is-flag -fno-stack-protector`" -o \
125     -n "`is-flag -fno-stack-protector-all`" ] && fstack-flags
126 vapier 1.28 return 0
127 verwilst 1.1 }
128    
129 vapier 1.18 replace-flags() {
130 vapier 1.36 # 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} "
134 vapier 1.37 CFLAGS="${CFLAGS// ${1} / ${2} }"
135     CXXFLAGS="${CXXFLAGS// ${1} / ${2} }"
136 vapier 1.36 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
137     CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
138 vapier 1.28 return 0
139 danarmak 1.7 }
140    
141 vapier 1.37 replace-cpu-flags() {
142 agriffis 1.58 local oldcpu newcpu="$1" ; shift
143 vapier 1.37 for oldcpu in "$@" ; do
144     replace-flags -march=${oldcpu} -march=${newcpu}
145     replace-flags -mcpu=${oldcpu} -mcpu=${newcpu}
146 vapier 1.38 replace-flags -mtune=${oldcpu} -mtune=${newcpu}
147 vapier 1.37 done
148     return 0
149     }
150    
151 danarmak 1.7 is-flag() {
152 agriffis 1.58 local x
153    
154 vapier 1.18 for x in ${CFLAGS} ${CXXFLAGS} ; do
155     if [ "${x}" == "$1" ] ; then
156 azarah 1.8 echo true
157 vapier 1.18 return 0
158     fi
159 danarmak 1.7 done
160 vapier 1.18 return 1
161 verwilst 1.1 }
162 azarah 1.8
163 vapier 1.36 filter-mfpmath() {
164 agriffis 1.58 local orig_mfpmath new_math prune_math
165    
166 vapier 1.36 # save the original -mfpmath flag
167 agriffis 1.58 orig_mfpmath="`get-flag -mfpmath`"
168 vapier 1.36 # get the value of the current -mfpmath flag
169 agriffis 1.58 new_math=" `get-flag mfpmath | tr , ' '` "
170 vapier 1.36 # figure out which math values are to be removed
171 agriffis 1.58 prune_math=""
172 vapier 1.36 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    
189 azarah 1.8 strip-flags() {
190 agriffis 1.58 local x y flag NEW_CFLAGS NEW_CXXFLAGS
191    
192 vapier 1.36 setup-allowed-flags
193    
194 azarah 1.8 local NEW_CFLAGS=""
195     local NEW_CXXFLAGS=""
196    
197 azarah 1.17 # Allow unstable C[XX]FLAGS if we are using unstable profile ...
198 vapier 1.49 if has ~${ARCH} ${ACCEPT_KEYWORDS} ; then
199     use debug && einfo "Enabling the use of some unstable flags"
200 azarah 1.17 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}"
201     fi
202    
203 agriffis 1.58 set -f # disable pathname expansion
204 azarah 1.8
205 agriffis 1.58 for x in ${CFLAGS}; do
206     for y in ${ALLOWED_FLAGS}; do
207 lostlogic 1.14 flag=${x%%=*}
208 agriffis 1.58 if [ "${flag%%${y}}" = "" ]; then
209 lostlogic 1.14 NEW_CFLAGS="${NEW_CFLAGS} ${x}"
210     break
211 azarah 1.8 fi
212     done
213     done
214    
215 agriffis 1.58 for x in ${CXXFLAGS}; do
216     for y in ${ALLOWED_FLAGS}; do
217 lostlogic 1.14 flag=${x%%=*}
218 agriffis 1.58 if [ "${flag%%${y}}" = "" ]; then
219 lostlogic 1.14 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}"
220     break
221 azarah 1.8 fi
222     done
223     done
224 azarah 1.30
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
232 azarah 1.8
233 agriffis 1.58 set +f # re-enable pathname expansion
234 azarah 1.8
235 agriffis 1.58 useq debug \
236 vapier 1.20 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \
237     && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\""
238    
239 azarah 1.8 export CFLAGS="${NEW_CFLAGS}"
240     export CXXFLAGS="${NEW_CXXFLAGS}"
241 vapier 1.36 return 0
242 puggy 1.29 }
243    
244 vapier 1.36 test_flag() {
245 agriffis 1.58 local cc=${CC:-gcc} ; cc=${cc%% *}
246     if ${cc} -S -xc "$@" -o /dev/null /dev/null &>/dev/null; then
247     printf "%s\n" "$*"
248 vapier 1.36 return 0
249 puggy 1.29 fi
250 vapier 1.36 return 1
251 puggy 1.29 }
252    
253 agriffis 1.58 test_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    
262 puggy 1.29 strip-unsupported-flags() {
263 agriffis 1.58 local NEW_CFLAGS NEW_CXXFLAGS
264    
265 vapier 1.36 for x in ${CFLAGS} ; do
266 agriffis 1.58 NEW_CFLAGS="${NEW_CFLAGS} `test_flag ${x}`"
267 puggy 1.29 done
268 vapier 1.36 for x in ${CXXFLAGS} ; do
269 agriffis 1.58 NEW_CXXFLAGS="${NEW_CXXFLAGS} `test_flag ${x}`"
270 puggy 1.29 done
271    
272 vapier 1.36 export CFLAGS="${NEW_CFLAGS}"
273     export CXXFLAGS="${NEW_CXXFLAGS}"
274 azarah 1.8 }
275    
276 vapier 1.10 get-flag() {
277 agriffis 1.58 local f findflag="$1"
278    
279 vapier 1.36 # this code looks a little flaky but seems to work for
280     # everything we want ...
281     # for example, if CFLAGS="-march=i686":
282 agriffis 1.58 # `get-flag -march` == "-march=i686"
283     # `get-flag march` == "i686"
284 vapier 1.10 for f in ${CFLAGS} ${CXXFLAGS} ; do
285     if [ "${f/${findflag}}" != "${f}" ] ; then
286 agriffis 1.58 printf "%s\n" "${f/-${findflag}=}"
287 vapier 1.28 return 0
288 vapier 1.10 fi
289     done
290 vapier 1.28 return 1
291 joker 1.16 }
292    
293 solar 1.53 has_hardened() {
294 agriffis 1.58 test_version_info Hardened
295 solar 1.53 return $?
296     }
297    
298 tseng 1.42 has_pic() {
299     [ "${CFLAGS/-fPIC}" != "${CFLAGS}" ] && return 0
300     [ "${CFLAGS/-fpic}" != "${CFLAGS}" ] && return 0
301 agriffis 1.58 test_version_info pie && return 0
302 tseng 1.42 return 1
303     }
304    
305     has_pie() {
306 tseng 1.43 [ "${CFLAGS/-fPIE}" != "${CFLAGS}" ] && return 0
307     [ "${CFLAGS/-fpie}" != "${CFLAGS}" ] && return 0
308 agriffis 1.58 test_version_info pie && return 0
309 tseng 1.42 return 1
310     }
311    
312     has_ssp() {
313     [ "${CFLAGS/-fstack-protector}" != "${CFLAGS}" ] && return 0
314 agriffis 1.58 test_version_info ssp && return 0
315 tseng 1.42 return 1
316     }
317    
318 lv 1.56 has_m64() {
319 agriffis 1.58 test_flag -m64
320     return $?
321 lv 1.56 }
322    
323     has_m32() {
324 agriffis 1.58 test_flag -m32
325     return $?
326 lv 1.56 }
327    
328 vapier 1.18 replace-sparc64-flags() {
329 joker 1.16 local SPARC64_CPUS="ultrasparc v9"
330    
331 agriffis 1.58 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
332     for x in ${SPARC64_CPUS}; do
333 joker 1.16 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
334     done
335     else
336 agriffis 1.58 for x in ${SPARC64_CPUS}; do
337 joker 1.16 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
338     done
339     fi
340    
341 agriffis 1.58 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
342     for x in ${SPARC64_CPUS}; do
343 joker 1.16 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
344     done
345     else
346 agriffis 1.58 for x in ${SPARC64_CPUS}; do
347 joker 1.16 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
348     done
349     fi
350 solar 1.27 }
351    
352     append-ldflags() {
353 agriffis 1.58 LDFLAGS="${LDFLAGS} $*"
354 vapier 1.28 return 0
355 solar 1.27 }
356    
357 solar 1.41 filter-ldflags() {
358 agriffis 1.58 local x
359    
360 solar 1.41 # 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    
370 vapier 1.36 etexec-flags() {
371 agriffis 1.58 # if you're not using a hardened compiler you wont need this
372 solar 1.53 # PIC/no-pic kludge in the first place.
373 agriffis 1.58 has_hardened || return 0
374 pappy 1.50
375 agriffis 1.58 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 solar 1.35 fi
381 agriffis 1.58 return 0
382 solar 1.27 }
383    
384     fstack-flags() {
385 agriffis 1.58 if has_ssp; then
386     [ -z "`is-flag -fno-stack-protector`" ] &&
387     export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`"
388 solar 1.35 fi
389 agriffis 1.58 return 0
390 vapier 1.10 }

  ViewVC Help
Powered by ViewVC 1.1.20