/[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.50 - (hide annotations) (download)
Wed May 12 13:36:17 2004 UTC (10 years, 4 months ago) by pappy
Branch: MAIN
Changes since 1.49: +16 -16 lines
added new switches for enabling and suppressing hardened PIE/SSP building

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

  ViewVC Help
Powered by ViewVC 1.1.20