| 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.36 2004/02/21 07:07:56 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" |
| … | |
… | |
| 24 | # |
24 | # |
| 25 | #### strip-flags #### |
25 | #### strip-flags #### |
| 26 | # Strip C[XX]FLAGS of everything except known |
26 | # Strip C[XX]FLAGS of everything except known |
| 27 | # good options. |
27 | # good options. |
| 28 | # |
28 | # |
|
|
29 | #### strip-unsupported-flags #### |
|
|
30 | # Strip C[XX]FLAGS of any flags not supported by |
|
|
31 | # installed version of gcc |
|
|
32 | # |
| 29 | #### get-flag <flag> #### |
33 | #### get-flag <flag> #### |
| 30 | # Find and echo the value for a particular flag |
34 | # Find and echo the value for a particular flag |
| 31 | # |
35 | # |
| 32 | #### replace-sparc64-flags #### |
36 | #### replace-sparc64-flags #### |
| 33 | # Sets mcpu to v8 and uses the original value |
37 | # Sets mcpu to v8 and uses the original value |
| 34 | # as mtune if none specified. |
38 | # as mtune if none specified. |
| 35 | # |
39 | # |
| 36 | |
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 | # |
| 37 | |
58 | |
| 38 | # C[XX]FLAGS that we allow in strip-flags |
59 | # C[XX]FLAGS that we allow in strip-flags |
|
|
60 | setup-allowed-flags() { |
| 39 | ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -pipe -g" |
61 | export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -pipe -g" |
| 40 | case "${ARCH}" in |
62 | case "${ARCH}" in |
| 41 | mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; |
63 | mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; |
| 42 | amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
64 | amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
|
|
65 | alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
|
|
66 | ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
| 43 | esac |
67 | esac |
| 44 | |
68 | |
| 45 | # C[XX]FLAGS that we are think is ok, but needs testing |
69 | # C[XX]FLAGS that we are think is ok, but needs testing |
| 46 | # NOTE: currently -Os have issues with gcc3 and K6* arch's |
70 | # NOTE: currently -Os have issues with gcc3 and K6* arch's |
| 47 | UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" |
71 | export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" |
| 48 | |
72 | 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 | } |
73 | } |
| 68 | |
74 | |
| 69 | filter-flags() { |
75 | filter-flags() { |
|
|
76 | for x in "$@" ; do |
|
|
77 | case "${x}" in |
|
|
78 | -fPIC|-fpic|-fPIE|-fpie) etexec-flags;; |
|
|
79 | -fstack-protector|-fstack-protector-all) fstack-flags;; |
|
|
80 | *) ;; |
|
|
81 | esac |
|
|
82 | done |
|
|
83 | |
| 70 | # we do this fancy spacing stuff so as to not filter |
84 | # we do this fancy spacing stuff so as to not filter |
| 71 | # out part of a flag ... we want flag atoms ! :D |
85 | # out part of a flag ... we want flag atoms ! :D |
| 72 | export CFLAGS=" ${CFLAGS} " |
86 | CFLAGS=" ${CFLAGS} " |
| 73 | export CXXFLAGS=" ${CXXFLAGS} " |
87 | CXXFLAGS=" ${CXXFLAGS} " |
| 74 | for x in $@ ; do |
88 | for x in "$@" ; do |
| 75 | export CFLAGS="${CFLAGS/ ${x} / }" |
89 | CFLAGS="${CFLAGS/ ${x} / }" |
| 76 | export CXXFLAGS="${CXXFLAGS/ ${x} / }" |
90 | CXXFLAGS="${CXXFLAGS/ ${x} / }" |
| 77 | done |
91 | done |
| 78 | export CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" |
92 | CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" |
| 79 | export CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" |
93 | CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" |
|
|
94 | return 0 |
| 80 | } |
95 | } |
| 81 | |
96 | |
| 82 | append-flags() { |
97 | append-flags() { |
| 83 | CFLAGS="${CFLAGS} $@" |
98 | export CFLAGS="${CFLAGS} $@" |
| 84 | CXXFLAGS="${CXXFLAGS} $@" |
99 | export CXXFLAGS="${CXXFLAGS} $@" |
|
|
100 | [ "`is-flag -fno-stack-protector`" -o "`is-flag -fno-stack-protector-all`" ] && fstack-flags |
|
|
101 | return 0 |
| 85 | } |
102 | } |
| 86 | |
103 | |
| 87 | replace-flags() { |
104 | replace-flags() { |
|
|
105 | # we do this fancy spacing stuff so as to not filter |
|
|
106 | # out part of a flag ... we want flag atoms ! :D |
|
|
107 | CFLAGS=" ${CFLAGS} " |
|
|
108 | CXXFLAGS=" ${CXXFLAGS} " |
| 88 | CFLAGS="${CFLAGS/${1}/${2} }" |
109 | CFLAGS="${CFLAGS/ ${1} / ${2} }" |
| 89 | CXXFLAGS="${CXXFLAGS/${1}/${2} }" |
110 | CXXFLAGS="${CXXFLAGS/ ${1} / ${2} }" |
|
|
111 | CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" |
|
|
112 | CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" |
|
|
113 | return 0 |
| 90 | } |
114 | } |
| 91 | |
115 | |
| 92 | is-flag() { |
116 | is-flag() { |
| 93 | for x in ${CFLAGS} ${CXXFLAGS} ; do |
117 | for x in ${CFLAGS} ${CXXFLAGS} ; do |
| 94 | if [ "${x}" == "$1" ] ; then |
118 | if [ "${x}" == "$1" ] ; then |
| … | |
… | |
| 97 | fi |
121 | fi |
| 98 | done |
122 | done |
| 99 | return 1 |
123 | return 1 |
| 100 | } |
124 | } |
| 101 | |
125 | |
|
|
126 | filter-mfpmath() { |
|
|
127 | # save the original -mfpmath flag |
|
|
128 | local orig_mfpmath="`get-flag -mfpmath`" |
|
|
129 | # get the value of the current -mfpmath flag |
|
|
130 | local new_math=" `get-flag mfpmath | tr , ' '` " |
|
|
131 | # figure out which math values are to be removed |
|
|
132 | local prune_math="" |
|
|
133 | for prune_math in "$@" ; do |
|
|
134 | new_math="${new_math/ ${prune_math} / }" |
|
|
135 | done |
|
|
136 | new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`" |
|
|
137 | |
|
|
138 | if [ -z "${new_math}" ] ; then |
|
|
139 | # if we're removing all user specified math values are |
|
|
140 | # slated for removal, then we just filter the flag |
|
|
141 | filter-flags ${orig_mfpmath} |
|
|
142 | else |
|
|
143 | # if we only want to filter some of the user specified |
|
|
144 | # math values, then we replace the current flag |
|
|
145 | replace-flags ${orig_mfpmath} -mfpmath=${new_math} |
|
|
146 | fi |
|
|
147 | return 0 |
|
|
148 | } |
|
|
149 | |
| 102 | strip-flags() { |
150 | strip-flags() { |
|
|
151 | setup-allowed-flags |
|
|
152 | |
| 103 | local NEW_CFLAGS="" |
153 | local NEW_CFLAGS="" |
| 104 | local NEW_CXXFLAGS="" |
154 | local NEW_CXXFLAGS="" |
| 105 | |
155 | |
| 106 | # Allow unstable C[XX]FLAGS if we are using unstable profile ... |
156 | # Allow unstable C[XX]FLAGS if we are using unstable profile ... |
| 107 | if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then |
157 | if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then |
| … | |
… | |
| 135 | break |
185 | break |
| 136 | fi |
186 | fi |
| 137 | done |
187 | done |
| 138 | done |
188 | done |
| 139 | |
189 | |
|
|
190 | # In case we filtered out all optimization flags fallback to -O2 |
|
|
191 | if [ "${CFLAGS/-O}" != "${CFLAGS}" -a "${NEW_CFLAGS/-O}" = "${NEW_CFLAGS}" ]; then |
|
|
192 | NEW_CFLAGS="${NEW_CFLAGS} -O2" |
|
|
193 | fi |
|
|
194 | if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then |
|
|
195 | NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2" |
|
|
196 | fi |
|
|
197 | |
| 140 | set +f |
198 | set +f |
| 141 | |
199 | |
| 142 | [ `use debug` ] \ |
200 | [ `use debug` ] \ |
| 143 | && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \ |
201 | && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \ |
| 144 | && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\"" |
202 | && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\"" |
| 145 | |
203 | |
| 146 | export CFLAGS="${NEW_CFLAGS}" |
204 | export CFLAGS="${NEW_CFLAGS}" |
| 147 | export CXXFLAGS="${NEW_CXXFLAGS}" |
205 | export CXXFLAGS="${NEW_CXXFLAGS}" |
|
|
206 | return 0 |
|
|
207 | } |
|
|
208 | |
|
|
209 | test_flag() { |
|
|
210 | if gcc -S -xc "$@" -o /dev/null /dev/null >/dev/null 2>&1; then |
|
|
211 | echo "$@" |
|
|
212 | return 0 |
|
|
213 | fi |
|
|
214 | return 1 |
|
|
215 | } |
|
|
216 | |
|
|
217 | strip-unsupported-flags() { |
|
|
218 | for x in ${CFLAGS} ; do |
|
|
219 | NEW_CFLAGS=${NEW_CFLAGS}" ""`test_flag ${x}`" |
|
|
220 | done |
|
|
221 | for x in ${CXXFLAGS} ; do |
|
|
222 | NEW_CXXFLAGS=${NEW_CXXFLAGS}" ""`test_flag ${x}`" |
|
|
223 | done |
|
|
224 | |
|
|
225 | export CFLAGS="${NEW_CFLAGS}" |
|
|
226 | export CXXFLAGS="${NEW_CXXFLAGS}" |
| 148 | } |
227 | } |
| 149 | |
228 | |
| 150 | get-flag() { |
229 | get-flag() { |
|
|
230 | # this code looks a little flaky but seems to work for |
|
|
231 | # everything we want ... |
|
|
232 | # for example, if CFLAGS="-march=i686": |
|
|
233 | # `get-flags -march` == "-march=i686" |
|
|
234 | # `get-flags march` == "i686" |
| 151 | local findflag="$1" |
235 | local findflag="$1" |
| 152 | |
|
|
| 153 | for f in ${CFLAGS} ${CXXFLAGS} ; do |
236 | for f in ${CFLAGS} ${CXXFLAGS} ; do |
| 154 | if [ "${f/${findflag}}" != "${f}" ] ; then |
237 | if [ "${f/${findflag}}" != "${f}" ] ; then |
| 155 | echo "${f/-${findflag}=}" |
238 | echo "${f/-${findflag}=}" |
| 156 | return |
239 | return 0 |
| 157 | fi |
240 | fi |
| 158 | done |
241 | done |
|
|
242 | return 1 |
| 159 | } |
243 | } |
| 160 | |
244 | |
| 161 | replace-sparc64-flags() { |
245 | replace-sparc64-flags() { |
| 162 | local SPARC64_CPUS="ultrasparc v9" |
246 | local SPARC64_CPUS="ultrasparc v9" |
| 163 | |
247 | |
| … | |
… | |
| 185 | do |
269 | do |
| 186 | CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" |
270 | CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" |
| 187 | done |
271 | done |
| 188 | fi |
272 | fi |
| 189 | } |
273 | } |
|
|
274 | |
|
|
275 | append-ldflags() { |
|
|
276 | LDFLAGS="${LDFLAGS} $@" |
|
|
277 | return 0 |
|
|
278 | } |
|
|
279 | |
|
|
280 | etexec-flags() { |
|
|
281 | has_version sys-devel/hardened-gcc |
|
|
282 | if [ $? == 0 ] ; then |
|
|
283 | if [ "`is-flag -yet_exec`" != "true" ]; then |
|
|
284 | debug-print ">>> appending flags -yet_exec" |
|
|
285 | append-flags -yet_exec |
|
|
286 | append-ldflags -yet_exec |
|
|
287 | fi |
|
|
288 | fi |
|
|
289 | } |
|
|
290 | |
|
|
291 | fstack-flags() { |
|
|
292 | has_version sys-devel/hardened-gcc |
|
|
293 | if [ $? == 0 ] ; then |
|
|
294 | if [ "`is-flag -yno_propolice`" != "true" ]; then |
|
|
295 | debug-print ">>> appending flags -yno_propolice" |
|
|
296 | append-flags -yno_propolice |
|
|
297 | append-ldflags -yno_propolice |
|
|
298 | fi |
|
|
299 | fi |
|
|
300 | } |