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.35 2004/01/20 10:37:19 solar 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 | |
5 | ECLASS=flag-o-matic |
7 | ECLASS=flag-o-matic |
|
|
8 | INHERITED="$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 | #### is-flag <flag> #### |
|
|
22 | # Returns "true" if flag is set in C[XX]FLAGS |
|
|
23 | # Matches only complete a flag |
|
|
24 | # |
|
|
25 | #### strip-flags #### |
|
|
26 | # Strip C[XX]FLAGS of everything except known |
|
|
27 | # good options. |
|
|
28 | # |
|
|
29 | #### strip-unsupported-flags #### |
|
|
30 | # Strip C[XX]FLAGS of any flags not supported by |
|
|
31 | # installed version of gcc |
|
|
32 | # |
|
|
33 | #### get-flag <flag> #### |
|
|
34 | # Find and echo the value for a particular flag |
|
|
35 | # |
|
|
36 | #### replace-sparc64-flags #### |
|
|
37 | # Sets mcpu to v8 and uses the original value |
|
|
38 | # as mtune if none specified. |
|
|
39 | # |
|
|
40 | #### filter-mfpmath <math types> #### |
|
|
41 | # Remove specified math types from the fpmath specification |
|
|
42 | # If the user has -mfpmath=sse,386, running `filter-mfpmath sse` |
|
|
43 | # will leave the user with -mfpmath=386 |
|
|
44 | # |
|
|
45 | #### append-ldflags #### |
|
|
46 | # Add extra flags to your current LDFLAGS |
|
|
47 | # |
|
|
48 | #### etexec-flags #### |
|
|
49 | # hooked function for hardened-gcc that appends |
|
|
50 | # -yet_exec {C,CXX,LD}FLAGS when hardened-gcc is installed |
|
|
51 | # and a package is filtering -fPIC,-fpic, -fPIE, -fpie |
|
|
52 | # |
|
|
53 | #### fstack-flags #### |
|
|
54 | # hooked function for hardened-gcc that appends |
|
|
55 | # -yno_propolice to {C,CXX,LD}FLAGS when hardened-gcc is installed |
|
|
56 | # and a package is filtering -fstack-protector, -fstack-protector-all |
|
|
57 | # |
17 | |
58 | |
|
|
59 | # C[XX]FLAGS that we allow in strip-flags |
|
|
60 | ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -pipe -g" |
|
|
61 | case "${ARCH}" in |
|
|
62 | mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; |
|
|
63 | amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
|
|
64 | alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
|
|
65 | ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
|
|
66 | esac |
18 | |
67 | |
|
|
68 | # C[XX]FLAGS that we are think is ok, but needs testing |
|
|
69 | # NOTE: currently -Os have issues with gcc3 and K6* arch's |
|
|
70 | UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" |
|
|
71 | |
|
|
72 | filter-mfpmath() { |
|
|
73 | for a in $CFLAGS; do |
|
|
74 | if [ "${a:0:8}" == "-mfpmath" ]; then |
|
|
75 | orig_mfpmath=$a |
|
|
76 | fi |
|
|
77 | done |
|
|
78 | |
|
|
79 | mfpmath="$( echo $orig_mfpmath | awk -F '=' '{print $2}' | tr "," " " )" |
|
|
80 | for b in $@; do |
|
|
81 | mfpmath="${mfpmath/$b}" |
|
|
82 | done |
|
|
83 | |
|
|
84 | if [ -z "${mfpmath/ }" ]; then |
|
|
85 | filter-flags "$orig_mfpmath" |
|
|
86 | else |
|
|
87 | new_mfpmath="-mfpmath=$( echo $mfpmath | sed -e "s/ /,/g" -e "s/,,/,/g" )" |
|
|
88 | replace-flags "$orig_mfpmath" "$new_mfpmath" |
|
|
89 | fi |
|
|
90 | } |
|
|
91 | |
19 | filter-flags () { |
92 | filter-flags() { |
20 | |
93 | # we do two loops to avoid the first and last char from being chomped. |
21 | for x in $1; do |
94 | for x in $@ ; do |
|
|
95 | case "${x}" in |
|
|
96 | -fPIC|-fpic|-fPIE|-fpie) etexec-flags;; |
|
|
97 | -fstack-protector|-fstack-protector-all) fstack-flags;; |
|
|
98 | *) ;; |
|
|
99 | esac |
|
|
100 | done |
|
|
101 | # we do this fancy spacing stuff so as to not filter |
|
|
102 | # out part of a flag ... we want flag atoms ! :D |
|
|
103 | CFLAGS=" ${CFLAGS} " |
|
|
104 | CXXFLAGS=" ${CXXFLAGS} " |
|
|
105 | for x in $@ ; do |
22 | CFLAGS="${CFLAGS/$x}" |
106 | CFLAGS="${CFLAGS/ ${x} / }" |
23 | CXXFLAGS="${CXXFLAGS/$x}" |
107 | CXXFLAGS="${CXXFLAGS/ ${x} / }" |
24 | done |
108 | done |
25 | |
109 | CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" |
|
|
110 | CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" |
26 | } |
111 | } |
27 | |
112 | |
28 | append-flags () { |
113 | append-flags() { |
29 | |
|
|
30 | |
|
|
31 | CFLAGS="$CFLAGS $1" |
114 | CFLAGS="${CFLAGS} $@" |
32 | CXXFLAGS="$CXXFLAGS $1" |
115 | CXXFLAGS="${CXXFLAGS} $@" |
33 | |
116 | [ "`is-flag -fno-stack-protector`" -o "`is-flag -fno-stack-protector-all`" ] && fstack-flags |
|
|
117 | return 0 |
34 | } |
118 | } |
35 | |
119 | |
36 | change-flags () { |
120 | replace-flags() { |
37 | |
|
|
38 | CFLAGS="${CFLAGS/${1}/${2}" |
121 | CFLAGS="${CFLAGS/${1}/${2} }" |
39 | CXXFLAGS="${CXXFLAGS/${1}/${2}" |
122 | CXXFLAGS="${CXXFLAGS/${1}/${2} }" |
40 | |
123 | return 0 |
41 | } |
124 | } |
|
|
125 | |
|
|
126 | is-flag() { |
|
|
127 | for x in ${CFLAGS} ${CXXFLAGS} ; do |
|
|
128 | if [ "${x}" == "$1" ] ; then |
|
|
129 | echo true |
|
|
130 | return 0 |
|
|
131 | fi |
|
|
132 | done |
|
|
133 | return 1 |
|
|
134 | } |
|
|
135 | |
|
|
136 | strip-flags() { |
|
|
137 | local NEW_CFLAGS="" |
|
|
138 | local NEW_CXXFLAGS="" |
|
|
139 | |
|
|
140 | # Allow unstable C[XX]FLAGS if we are using unstable profile ... |
|
|
141 | if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then |
|
|
142 | [ `use debug` ] && einfo "Enabling the use of some unstable flags" |
|
|
143 | ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}" |
|
|
144 | fi |
|
|
145 | |
|
|
146 | set -f |
|
|
147 | |
|
|
148 | for x in ${CFLAGS} |
|
|
149 | do |
|
|
150 | for y in ${ALLOWED_FLAGS} |
|
|
151 | do |
|
|
152 | flag=${x%%=*} |
|
|
153 | if [ "${flag%%${y}}" = "" ] |
|
|
154 | then |
|
|
155 | NEW_CFLAGS="${NEW_CFLAGS} ${x}" |
|
|
156 | break |
|
|
157 | fi |
|
|
158 | done |
|
|
159 | done |
|
|
160 | |
|
|
161 | for x in ${CXXFLAGS} |
|
|
162 | do |
|
|
163 | for y in ${ALLOWED_FLAGS} |
|
|
164 | do |
|
|
165 | flag=${x%%=*} |
|
|
166 | if [ "${flag%%${y}}" = "" ] |
|
|
167 | then |
|
|
168 | NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}" |
|
|
169 | break |
|
|
170 | fi |
|
|
171 | done |
|
|
172 | done |
|
|
173 | |
|
|
174 | # In case we filtered out all optimization flags fallback to -O2 |
|
|
175 | if [ "${CFLAGS/-O}" != "${CFLAGS}" -a "${NEW_CFLAGS/-O}" = "${NEW_CFLAGS}" ]; then |
|
|
176 | NEW_CFLAGS="${NEW_CFLAGS} -O2" |
|
|
177 | fi |
|
|
178 | if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then |
|
|
179 | NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2" |
|
|
180 | fi |
|
|
181 | |
|
|
182 | set +f |
|
|
183 | |
|
|
184 | [ `use debug` ] \ |
|
|
185 | && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \ |
|
|
186 | && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\"" |
|
|
187 | |
|
|
188 | export CFLAGS="${NEW_CFLAGS}" |
|
|
189 | export CXXFLAGS="${NEW_CXXFLAGS}" |
|
|
190 | } |
|
|
191 | |
|
|
192 | test_flag () { |
|
|
193 | if gcc -S -xc $1 -o /dev/null /dev/null >/dev/null 2>&1; then |
|
|
194 | echo "$1" |
|
|
195 | fi |
|
|
196 | } |
|
|
197 | |
|
|
198 | strip-unsupported-flags() { |
|
|
199 | for x in ${CFLAGS} |
|
|
200 | do |
|
|
201 | NEW_CFLAGS=${NEW_CFLAGS}" ""`test_flag ${x}`" |
|
|
202 | done |
|
|
203 | |
|
|
204 | for x in ${CXXFLAGS} |
|
|
205 | do |
|
|
206 | NEW_CXXFLAGS=${NEW_CXXFLAGS}" ""`test_flag ${x}`" |
|
|
207 | done |
|
|
208 | |
|
|
209 | CFLAGS="${NEW_CFLAGS}" |
|
|
210 | CXXFLAGS="${NEW_CXXFLAGS}" |
|
|
211 | } |
|
|
212 | |
|
|
213 | get-flag() { |
|
|
214 | local findflag="$1" |
|
|
215 | for f in ${CFLAGS} ${CXXFLAGS} ; do |
|
|
216 | if [ "${f/${findflag}}" != "${f}" ] ; then |
|
|
217 | echo "${f/-${findflag}=}" |
|
|
218 | return 0 |
|
|
219 | fi |
|
|
220 | done |
|
|
221 | return 1 |
|
|
222 | } |
|
|
223 | |
|
|
224 | replace-sparc64-flags() { |
|
|
225 | local SPARC64_CPUS="ultrasparc v9" |
|
|
226 | |
|
|
227 | if [ "${CFLAGS/mtune}" != "${CFLAGS}" ] |
|
|
228 | then |
|
|
229 | for x in ${SPARC64_CPUS} |
|
|
230 | do |
|
|
231 | CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}" |
|
|
232 | done |
|
|
233 | else |
|
|
234 | for x in ${SPARC64_CPUS} |
|
|
235 | do |
|
|
236 | CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" |
|
|
237 | done |
|
|
238 | fi |
|
|
239 | |
|
|
240 | if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ] |
|
|
241 | then |
|
|
242 | for x in ${SPARC64_CPUS} |
|
|
243 | do |
|
|
244 | CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}" |
|
|
245 | done |
|
|
246 | else |
|
|
247 | for x in ${SPARC64_CPUS} |
|
|
248 | do |
|
|
249 | CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" |
|
|
250 | done |
|
|
251 | fi |
|
|
252 | } |
|
|
253 | |
|
|
254 | append-ldflags() { |
|
|
255 | LDFLAGS="${LDFLAGS} $@" |
|
|
256 | return 0 |
|
|
257 | } |
|
|
258 | |
|
|
259 | etexec-flags() { |
|
|
260 | has_version sys-devel/hardened-gcc |
|
|
261 | if [ $? == 0 ]; then |
|
|
262 | if [ "`is-flag -yet_exec`" != "true" ]; then |
|
|
263 | debug-print ">>> appending flags -yet_exec" |
|
|
264 | append-flags -yet_exec |
|
|
265 | append-ldflags -yet_exec |
|
|
266 | fi |
|
|
267 | fi |
|
|
268 | } |
|
|
269 | |
|
|
270 | fstack-flags() { |
|
|
271 | has_version sys-devel/hardened-gcc |
|
|
272 | if [ $? == 0 ]; then |
|
|
273 | if [ "`is-flag -yno_propolice`" != "true" ]; then |
|
|
274 | debug-print ">>> appending flags -yno_propolice" |
|
|
275 | append-flags -yno_propolice |
|
|
276 | append-ldflags -yno_propolice |
|
|
277 | fi |
|
|
278 | fi |
|
|
279 | } |