| 1 | # Copyright 1999-2003 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.24 2003/07/22 12:48:11 aliz Exp $ |
3 | # $Header: /var/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.38 2004/02/26 06:47:23 vapier Exp $ |
| 4 | # |
4 | # |
| 5 | # Author Bart Verwilst <verwilst@gentoo.org> |
5 | # Author Bart Verwilst <verwilst@gentoo.org> |
| 6 | |
6 | |
| 7 | ECLASS=flag-o-matic |
7 | ECLASS=flag-o-matic |
| 8 | INHERITED="$INHERITED $ECLASS" |
8 | INHERITED="$INHERITED $ECLASS" |
| … | |
… | |
| 16 | # Add extra flags to your current C[XX]FLAGS |
16 | # Add extra flags to your current C[XX]FLAGS |
| 17 | # |
17 | # |
| 18 | #### replace-flags <orig.flag> <new.flag> ### |
18 | #### replace-flags <orig.flag> <new.flag> ### |
| 19 | # Replace a flag by another one |
19 | # Replace a flag by another one |
| 20 | # |
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 | # |
| 21 | #### is-flag <flag> #### |
25 | #### is-flag <flag> #### |
| 22 | # Returns "true" if flag is set in C[XX]FLAGS |
26 | # Returns "true" if flag is set in C[XX]FLAGS |
| 23 | # Matches only complete a flag |
27 | # Matches only complete a flag |
| 24 | # |
28 | # |
| 25 | #### strip-flags #### |
29 | #### strip-flags #### |
| 26 | # Strip C[XX]FLAGS of everything except known |
30 | # Strip C[XX]FLAGS of everything except known |
| 27 | # good options. |
31 | # good options. |
| 28 | # |
32 | # |
|
|
33 | #### strip-unsupported-flags #### |
|
|
34 | # Strip C[XX]FLAGS of any flags not supported by |
|
|
35 | # installed version of gcc |
|
|
36 | # |
| 29 | #### get-flag <flag> #### |
37 | #### get-flag <flag> #### |
| 30 | # Find and echo the value for a particular flag |
38 | # Find and echo the value for a particular flag |
| 31 | # |
39 | # |
| 32 | #### replace-sparc64-flags #### |
40 | #### replace-sparc64-flags #### |
| 33 | # Sets mcpu to v8 and uses the original value |
41 | # Sets mcpu to v8 and uses the original value |
| 34 | # as mtune if none specified. |
42 | # as mtune if none specified. |
| 35 | # |
43 | # |
| 36 | |
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 | #### etexec-flags #### |
|
|
53 | # hooked function for hardened-gcc that appends |
|
|
54 | # -yet_exec {C,CXX,LD}FLAGS when hardened-gcc is installed |
|
|
55 | # and a package is filtering -fPIC,-fpic, -fPIE, -fpie |
|
|
56 | # |
|
|
57 | #### fstack-flags #### |
|
|
58 | # hooked function for hardened-gcc that appends |
|
|
59 | # -yno_propolice to {C,CXX,LD}FLAGS when hardened-gcc is installed |
|
|
60 | # and a package is filtering -fstack-protector, -fstack-protector-all |
|
|
61 | # |
| 37 | |
62 | |
| 38 | # C[XX]FLAGS that we allow in strip-flags |
63 | # C[XX]FLAGS that we allow in strip-flags |
|
|
64 | setup-allowed-flags() { |
| 39 | ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -pipe -g" |
65 | export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -pipe -g" |
| 40 | case "${ARCH}" in |
66 | case "${ARCH}" in |
| 41 | mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; |
67 | mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; |
| 42 | amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
68 | amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
|
|
69 | alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
|
|
70 | ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
| 43 | esac |
71 | esac |
| 44 | |
72 | |
| 45 | # C[XX]FLAGS that we are think is ok, but needs testing |
73 | # C[XX]FLAGS that we are think is ok, but needs testing |
| 46 | # NOTE: currently -Os have issues with gcc3 and K6* arch's |
74 | # NOTE: currently -Os have issues with gcc3 and K6* arch's |
| 47 | UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" |
75 | export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" |
| 48 | |
76 | return 0 |
| 49 | filter-mfpmath() { |
|
|
| 50 | for a in $CFLAGS; do |
|
|
| 51 | if [ "${a:0:8}" == "-mfpmath" ]; then |
|
|
| 52 | orig_mfpmath=$a |
|
|
| 53 | fi |
|
|
| 54 | done |
|
|
| 55 | |
|
|
| 56 | mfpmath="$( echo $orig_mfpmath | awk -F '=' '{print $2}' | tr "," " " )" |
|
|
| 57 | for b in $@; do |
|
|
| 58 | mfpmath="${mfpmath/$b}" |
|
|
| 59 | done |
|
|
| 60 | |
|
|
| 61 | if [ -z "${mfpmath/ }" ]; then |
|
|
| 62 | filter-flags "$orig_mfpmath" |
|
|
| 63 | else |
|
|
| 64 | new_mfpmath="-mfpmath=$( echo $mfpmath | sed -e "s/ /,/g" -e "s/,,/,/g" )" |
|
|
| 65 | replace-flags "$orig_mfpmath" "$new_mfpmath" |
|
|
| 66 | fi |
|
|
| 67 | } |
77 | } |
| 68 | |
78 | |
| 69 | filter-flags() { |
79 | filter-flags() { |
|
|
80 | for x in "$@" ; do |
|
|
81 | case "${x}" in |
|
|
82 | -fPIC|-fpic|-fPIE|-fpie) etexec-flags;; |
|
|
83 | -fstack-protector|-fstack-protector-all) fstack-flags;; |
|
|
84 | *) ;; |
|
|
85 | esac |
|
|
86 | done |
|
|
87 | |
| 70 | # we do this fancy spacing stuff so as to not filter |
88 | # we do this fancy spacing stuff so as to not filter |
| 71 | # out part of a flag ... we want flag atoms ! :D |
89 | # out part of a flag ... we want flag atoms ! :D |
| 72 | export CFLAGS=" ${CFLAGS} " |
90 | CFLAGS=" ${CFLAGS} " |
| 73 | export CXXFLAGS=" ${CXXFLAGS} " |
91 | CXXFLAGS=" ${CXXFLAGS} " |
| 74 | for x in $@ ; do |
92 | for x in "$@" ; do |
| 75 | export CFLAGS="${CFLAGS/ ${x} / }" |
93 | CFLAGS="${CFLAGS// ${x} / }" |
| 76 | export CXXFLAGS="${CXXFLAGS/ ${x} / }" |
94 | CXXFLAGS="${CXXFLAGS// ${x} / }" |
| 77 | done |
95 | done |
| 78 | export CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" |
96 | CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" |
| 79 | export CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" |
97 | CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" |
|
|
98 | return 0 |
| 80 | } |
99 | } |
| 81 | |
100 | |
| 82 | append-flags() { |
101 | append-flags() { |
| 83 | CFLAGS="${CFLAGS} $@" |
102 | export CFLAGS="${CFLAGS} $@" |
| 84 | CXXFLAGS="${CXXFLAGS} $@" |
103 | export CXXFLAGS="${CXXFLAGS} $@" |
|
|
104 | [ "`is-flag -fno-stack-protector`" -o "`is-flag -fno-stack-protector-all`" ] && fstack-flags |
|
|
105 | return 0 |
| 85 | } |
106 | } |
| 86 | |
107 | |
| 87 | replace-flags() { |
108 | replace-flags() { |
|
|
109 | # we do this fancy spacing stuff so as to not filter |
|
|
110 | # out part of a flag ... we want flag atoms ! :D |
|
|
111 | CFLAGS=" ${CFLAGS} " |
|
|
112 | CXXFLAGS=" ${CXXFLAGS} " |
| 88 | CFLAGS="${CFLAGS/${1}/${2} }" |
113 | CFLAGS="${CFLAGS// ${1} / ${2} }" |
| 89 | CXXFLAGS="${CXXFLAGS/${1}/${2} }" |
114 | CXXFLAGS="${CXXFLAGS// ${1} / ${2} }" |
|
|
115 | CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" |
|
|
116 | CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" |
|
|
117 | return 0 |
|
|
118 | } |
|
|
119 | |
|
|
120 | replace-cpu-flags() { |
|
|
121 | local newcpu="$1" ; shift |
|
|
122 | local oldcpu="" |
|
|
123 | for oldcpu in "$@" ; do |
|
|
124 | replace-flags -march=${oldcpu} -march=${newcpu} |
|
|
125 | replace-flags -mcpu=${oldcpu} -mcpu=${newcpu} |
|
|
126 | replace-flags -mtune=${oldcpu} -mtune=${newcpu} |
|
|
127 | done |
|
|
128 | return 0 |
| 90 | } |
129 | } |
| 91 | |
130 | |
| 92 | is-flag() { |
131 | is-flag() { |
| 93 | for x in ${CFLAGS} ${CXXFLAGS} ; do |
132 | for x in ${CFLAGS} ${CXXFLAGS} ; do |
| 94 | if [ "${x}" == "$1" ] ; then |
133 | if [ "${x}" == "$1" ] ; then |
| … | |
… | |
| 97 | fi |
136 | fi |
| 98 | done |
137 | done |
| 99 | return 1 |
138 | return 1 |
| 100 | } |
139 | } |
| 101 | |
140 | |
|
|
141 | filter-mfpmath() { |
|
|
142 | # save the original -mfpmath flag |
|
|
143 | local orig_mfpmath="`get-flag -mfpmath`" |
|
|
144 | # get the value of the current -mfpmath flag |
|
|
145 | local new_math=" `get-flag mfpmath | tr , ' '` " |
|
|
146 | # figure out which math values are to be removed |
|
|
147 | local prune_math="" |
|
|
148 | for prune_math in "$@" ; do |
|
|
149 | new_math="${new_math/ ${prune_math} / }" |
|
|
150 | done |
|
|
151 | new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`" |
|
|
152 | |
|
|
153 | if [ -z "${new_math}" ] ; then |
|
|
154 | # if we're removing all user specified math values are |
|
|
155 | # slated for removal, then we just filter the flag |
|
|
156 | filter-flags ${orig_mfpmath} |
|
|
157 | else |
|
|
158 | # if we only want to filter some of the user specified |
|
|
159 | # math values, then we replace the current flag |
|
|
160 | replace-flags ${orig_mfpmath} -mfpmath=${new_math} |
|
|
161 | fi |
|
|
162 | return 0 |
|
|
163 | } |
|
|
164 | |
| 102 | strip-flags() { |
165 | strip-flags() { |
|
|
166 | setup-allowed-flags |
|
|
167 | |
| 103 | local NEW_CFLAGS="" |
168 | local NEW_CFLAGS="" |
| 104 | local NEW_CXXFLAGS="" |
169 | local NEW_CXXFLAGS="" |
| 105 | |
170 | |
| 106 | # Allow unstable C[XX]FLAGS if we are using unstable profile ... |
171 | # Allow unstable C[XX]FLAGS if we are using unstable profile ... |
| 107 | if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then |
172 | if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then |
| … | |
… | |
| 135 | break |
200 | break |
| 136 | fi |
201 | fi |
| 137 | done |
202 | done |
| 138 | done |
203 | done |
| 139 | |
204 | |
|
|
205 | # In case we filtered out all optimization flags fallback to -O2 |
|
|
206 | if [ "${CFLAGS/-O}" != "${CFLAGS}" -a "${NEW_CFLAGS/-O}" = "${NEW_CFLAGS}" ]; then |
|
|
207 | NEW_CFLAGS="${NEW_CFLAGS} -O2" |
|
|
208 | fi |
|
|
209 | if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then |
|
|
210 | NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2" |
|
|
211 | fi |
|
|
212 | |
| 140 | set +f |
213 | set +f |
| 141 | |
214 | |
| 142 | [ `use debug` ] \ |
215 | [ `use debug` ] \ |
| 143 | && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \ |
216 | && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \ |
| 144 | && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\"" |
217 | && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\"" |
| 145 | |
218 | |
| 146 | export CFLAGS="${NEW_CFLAGS}" |
219 | export CFLAGS="${NEW_CFLAGS}" |
| 147 | export CXXFLAGS="${NEW_CXXFLAGS}" |
220 | export CXXFLAGS="${NEW_CXXFLAGS}" |
|
|
221 | return 0 |
|
|
222 | } |
|
|
223 | |
|
|
224 | test_flag() { |
|
|
225 | if gcc -S -xc "$@" -o /dev/null /dev/null >/dev/null 2>&1; then |
|
|
226 | echo "$@" |
|
|
227 | return 0 |
|
|
228 | fi |
|
|
229 | return 1 |
|
|
230 | } |
|
|
231 | |
|
|
232 | strip-unsupported-flags() { |
|
|
233 | for x in ${CFLAGS} ; do |
|
|
234 | NEW_CFLAGS=${NEW_CFLAGS}" ""`test_flag ${x}`" |
|
|
235 | done |
|
|
236 | for x in ${CXXFLAGS} ; do |
|
|
237 | NEW_CXXFLAGS=${NEW_CXXFLAGS}" ""`test_flag ${x}`" |
|
|
238 | done |
|
|
239 | |
|
|
240 | export CFLAGS="${NEW_CFLAGS}" |
|
|
241 | export CXXFLAGS="${NEW_CXXFLAGS}" |
| 148 | } |
242 | } |
| 149 | |
243 | |
| 150 | get-flag() { |
244 | get-flag() { |
|
|
245 | # this code looks a little flaky but seems to work for |
|
|
246 | # everything we want ... |
|
|
247 | # for example, if CFLAGS="-march=i686": |
|
|
248 | # `get-flags -march` == "-march=i686" |
|
|
249 | # `get-flags march` == "i686" |
| 151 | local findflag="$1" |
250 | local findflag="$1" |
| 152 | |
|
|
| 153 | for f in ${CFLAGS} ${CXXFLAGS} ; do |
251 | for f in ${CFLAGS} ${CXXFLAGS} ; do |
| 154 | if [ "${f/${findflag}}" != "${f}" ] ; then |
252 | if [ "${f/${findflag}}" != "${f}" ] ; then |
| 155 | echo "${f/-${findflag}=}" |
253 | echo "${f/-${findflag}=}" |
| 156 | return |
254 | return 0 |
| 157 | fi |
255 | fi |
| 158 | done |
256 | done |
|
|
257 | return 1 |
| 159 | } |
258 | } |
| 160 | |
259 | |
| 161 | replace-sparc64-flags() { |
260 | replace-sparc64-flags() { |
| 162 | local SPARC64_CPUS="ultrasparc v9" |
261 | local SPARC64_CPUS="ultrasparc v9" |
| 163 | |
262 | |
| … | |
… | |
| 185 | do |
284 | do |
| 186 | CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" |
285 | CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" |
| 187 | done |
286 | done |
| 188 | fi |
287 | fi |
| 189 | } |
288 | } |
|
|
289 | |
|
|
290 | append-ldflags() { |
|
|
291 | LDFLAGS="${LDFLAGS} $@" |
|
|
292 | return 0 |
|
|
293 | } |
|
|
294 | |
|
|
295 | etexec-flags() { |
|
|
296 | has_version sys-devel/hardened-gcc |
|
|
297 | if [ $? == 0 ] ; then |
|
|
298 | if [ "`is-flag -yet_exec`" != "true" ]; then |
|
|
299 | debug-print ">>> appending flags -yet_exec" |
|
|
300 | append-flags -yet_exec |
|
|
301 | append-ldflags -yet_exec |
|
|
302 | fi |
|
|
303 | fi |
|
|
304 | } |
|
|
305 | |
|
|
306 | fstack-flags() { |
|
|
307 | has_version sys-devel/hardened-gcc |
|
|
308 | if [ $? == 0 ] ; then |
|
|
309 | if [ "`is-flag -yno_propolice`" != "true" ]; then |
|
|
310 | debug-print ">>> appending flags -yno_propolice" |
|
|
311 | append-flags -yno_propolice |
|
|
312 | append-ldflags -yno_propolice |
|
|
313 | fi |
|
|
314 | fi |
|
|
315 | } |