/[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.53 - (hide annotations) (download)
Tue Jun 1 04:28:38 2004 UTC (10 years, 3 months ago) by solar
Branch: MAIN
Changes since 1.52: +11 -1 lines
prevent etexec-flags function from rewriting CFLAGS when not using the hardened compiler. bug #52286

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

  ViewVC Help
Powered by ViewVC 1.1.20