| 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.33 2003/12/31 16:51:35 vapier Exp $ |
3 | # $Header: /var/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.40 2004/03/12 11:21:15 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" |
| … | |
… | |
| 15 | #### append-flags <flags> #### |
15 | #### append-flags <flags> #### |
| 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 | # |
|
|
21 | #### replace-cpu-flags <new.cpu> <old.cpus> ### |
|
|
22 | # Replace march/mcpu flags that specify <old.cpus> |
|
|
23 | # with flags that specify <new.cpu> |
| 20 | # |
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 | # |
| … | |
… | |
| 55 | # -yno_propolice to {C,CXX,LD}FLAGS when hardened-gcc is installed |
59 | # -yno_propolice to {C,CXX,LD}FLAGS when hardened-gcc is installed |
| 56 | # and a package is filtering -fstack-protector, -fstack-protector-all |
60 | # and a package is filtering -fstack-protector, -fstack-protector-all |
| 57 | # |
61 | # |
| 58 | |
62 | |
| 59 | # C[XX]FLAGS that we allow in strip-flags |
63 | # C[XX]FLAGS that we allow in strip-flags |
|
|
64 | setup-allowed-flags() { |
| 60 | 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" |
| 61 | case "${ARCH}" in |
66 | case "${ARCH}" in |
| 62 | mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; |
67 | mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; |
| 63 | amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
68 | amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
| 64 | alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
69 | alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
| 65 | ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
70 | ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
| 66 | esac |
71 | esac |
| 67 | |
72 | |
| 68 | # 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 |
| 69 | # NOTE: currently -Os have issues with gcc3 and K6* arch's |
74 | # NOTE: currently -Os have issues with gcc3 and K6* arch's |
| 70 | UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" |
75 | export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" |
| 71 | |
76 | return 0 |
| 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 | } |
77 | } |
| 91 | |
78 | |
| 92 | filter-flags() { |
79 | filter-flags() { |
| 93 | # we do two loops to avoid the first and last char from being chomped. |
|
|
| 94 | for x in $@ ; do |
80 | for x in "$@" ; do |
| 95 | case "${x}" in |
81 | case "${x}" in |
| 96 | -fPIC|-fpic|-fPIE|-fpie) etexec-flags;; |
82 | -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;; |
| 97 | -fstack-protector|-fstack-protector-all) fstack-flags;; |
83 | -fstack-protector|-fstack-protector-all) fstack-flags;; |
| 98 | *) ;; |
84 | *) ;; |
| 99 | esac |
85 | esac |
| 100 | done |
86 | done |
|
|
87 | |
| 101 | # we do this fancy spacing stuff so as to not filter |
88 | # we do this fancy spacing stuff so as to not filter |
| 102 | # out part of a flag ... we want flag atoms ! :D |
89 | # out part of a flag ... we want flag atoms ! :D |
| 103 | CFLAGS=" ${CFLAGS} " |
90 | CFLAGS=" ${CFLAGS} " |
| 104 | CXXFLAGS=" ${CXXFLAGS} " |
91 | CXXFLAGS=" ${CXXFLAGS} " |
| 105 | for x in $@ ; do |
92 | for x in "$@" ; do |
| 106 | CFLAGS="${CFLAGS/ ${x} / }" |
93 | CFLAGS="${CFLAGS// ${x} / }" |
| 107 | CXXFLAGS="${CXXFLAGS/ ${x} / }" |
94 | CXXFLAGS="${CXXFLAGS// ${x} / }" |
| 108 | done |
95 | done |
| 109 | CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" |
96 | CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" |
| 110 | CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" |
97 | CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" |
|
|
98 | return 0 |
|
|
99 | } |
|
|
100 | |
|
|
101 | filter-lfs-flags() { |
|
|
102 | filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE |
| 111 | } |
103 | } |
| 112 | |
104 | |
| 113 | append-flags() { |
105 | append-flags() { |
| 114 | CFLAGS="${CFLAGS} $@" |
106 | export CFLAGS="${CFLAGS} $@" |
| 115 | CXXFLAGS="${CXXFLAGS} $@" |
107 | export CXXFLAGS="${CXXFLAGS} $@" |
| 116 | [ "`is-flag -fno-stack-protector`" -o "`is-flag -fno-stack-protector-all`" ] && fstack-flags |
108 | [ "`is-flag -fno-stack-protector`" -o "`is-flag -fno-stack-protector-all`" ] && fstack-flags |
| 117 | return 0 |
109 | return 0 |
| 118 | } |
110 | } |
| 119 | |
111 | |
| 120 | replace-flags() { |
112 | replace-flags() { |
|
|
113 | # we do this fancy spacing stuff so as to not filter |
|
|
114 | # out part of a flag ... we want flag atoms ! :D |
|
|
115 | CFLAGS=" ${CFLAGS} " |
|
|
116 | CXXFLAGS=" ${CXXFLAGS} " |
| 121 | CFLAGS="${CFLAGS/${1}/${2} }" |
117 | CFLAGS="${CFLAGS// ${1} / ${2} }" |
| 122 | CXXFLAGS="${CXXFLAGS/${1}/${2} }" |
118 | CXXFLAGS="${CXXFLAGS// ${1} / ${2} }" |
|
|
119 | CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" |
|
|
120 | CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" |
|
|
121 | return 0 |
|
|
122 | } |
|
|
123 | |
|
|
124 | replace-cpu-flags() { |
|
|
125 | local newcpu="$1" ; shift |
|
|
126 | local oldcpu="" |
|
|
127 | for oldcpu in "$@" ; do |
|
|
128 | replace-flags -march=${oldcpu} -march=${newcpu} |
|
|
129 | replace-flags -mcpu=${oldcpu} -mcpu=${newcpu} |
|
|
130 | replace-flags -mtune=${oldcpu} -mtune=${newcpu} |
|
|
131 | done |
| 123 | return 0 |
132 | return 0 |
| 124 | } |
133 | } |
| 125 | |
134 | |
| 126 | is-flag() { |
135 | is-flag() { |
| 127 | for x in ${CFLAGS} ${CXXFLAGS} ; do |
136 | for x in ${CFLAGS} ${CXXFLAGS} ; do |
| … | |
… | |
| 131 | fi |
140 | fi |
| 132 | done |
141 | done |
| 133 | return 1 |
142 | return 1 |
| 134 | } |
143 | } |
| 135 | |
144 | |
|
|
145 | filter-mfpmath() { |
|
|
146 | # save the original -mfpmath flag |
|
|
147 | local orig_mfpmath="`get-flag -mfpmath`" |
|
|
148 | # get the value of the current -mfpmath flag |
|
|
149 | local new_math=" `get-flag mfpmath | tr , ' '` " |
|
|
150 | # figure out which math values are to be removed |
|
|
151 | local prune_math="" |
|
|
152 | for prune_math in "$@" ; do |
|
|
153 | new_math="${new_math/ ${prune_math} / }" |
|
|
154 | done |
|
|
155 | new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`" |
|
|
156 | |
|
|
157 | if [ -z "${new_math}" ] ; then |
|
|
158 | # if we're removing all user specified math values are |
|
|
159 | # slated for removal, then we just filter the flag |
|
|
160 | filter-flags ${orig_mfpmath} |
|
|
161 | else |
|
|
162 | # if we only want to filter some of the user specified |
|
|
163 | # math values, then we replace the current flag |
|
|
164 | replace-flags ${orig_mfpmath} -mfpmath=${new_math} |
|
|
165 | fi |
|
|
166 | return 0 |
|
|
167 | } |
|
|
168 | |
| 136 | strip-flags() { |
169 | strip-flags() { |
|
|
170 | setup-allowed-flags |
|
|
171 | |
| 137 | local NEW_CFLAGS="" |
172 | local NEW_CFLAGS="" |
| 138 | local NEW_CXXFLAGS="" |
173 | local NEW_CXXFLAGS="" |
| 139 | |
174 | |
| 140 | # Allow unstable C[XX]FLAGS if we are using unstable profile ... |
175 | # Allow unstable C[XX]FLAGS if we are using unstable profile ... |
| 141 | if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then |
176 | if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then |
| … | |
… | |
| 185 | && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \ |
220 | && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \ |
| 186 | && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\"" |
221 | && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\"" |
| 187 | |
222 | |
| 188 | export CFLAGS="${NEW_CFLAGS}" |
223 | export CFLAGS="${NEW_CFLAGS}" |
| 189 | export CXXFLAGS="${NEW_CXXFLAGS}" |
224 | export CXXFLAGS="${NEW_CXXFLAGS}" |
|
|
225 | return 0 |
| 190 | } |
226 | } |
| 191 | |
227 | |
| 192 | test_flag () { |
228 | test_flag() { |
| 193 | if gcc -S -xc $1 -o /dev/null /dev/null >/dev/null 2>&1; then |
229 | if gcc -S -xc "$@" -o /dev/null /dev/null >/dev/null 2>&1; then |
| 194 | echo "$1" |
230 | echo "$@" |
|
|
231 | return 0 |
| 195 | fi |
232 | fi |
|
|
233 | return 1 |
| 196 | } |
234 | } |
| 197 | |
235 | |
| 198 | strip-unsupported-flags() { |
236 | strip-unsupported-flags() { |
| 199 | for x in ${CFLAGS} |
237 | for x in ${CFLAGS} ; do |
| 200 | do |
|
|
| 201 | NEW_CFLAGS=${NEW_CFLAGS}" ""`test_flag ${x}`" |
238 | NEW_CFLAGS=${NEW_CFLAGS}" ""`test_flag ${x}`" |
| 202 | done |
239 | done |
| 203 | |
|
|
| 204 | for x in ${CXXFLAGS} |
240 | for x in ${CXXFLAGS} ; do |
| 205 | do |
|
|
| 206 | NEW_CXXFLAGS=${NEW_CXXFLAGS}" ""`test_flag ${x}`" |
241 | NEW_CXXFLAGS=${NEW_CXXFLAGS}" ""`test_flag ${x}`" |
| 207 | done |
242 | done |
| 208 | |
243 | |
| 209 | CFLAGS="${NEW_CFLAGS}" |
244 | export CFLAGS="${NEW_CFLAGS}" |
| 210 | CXXFLAGS="${NEW_CXXFLAGS}" |
245 | export CXXFLAGS="${NEW_CXXFLAGS}" |
| 211 | } |
246 | } |
| 212 | |
247 | |
| 213 | get-flag() { |
248 | get-flag() { |
|
|
249 | # this code looks a little flaky but seems to work for |
|
|
250 | # everything we want ... |
|
|
251 | # for example, if CFLAGS="-march=i686": |
|
|
252 | # `get-flags -march` == "-march=i686" |
|
|
253 | # `get-flags march` == "i686" |
| 214 | local findflag="$1" |
254 | local findflag="$1" |
| 215 | for f in ${CFLAGS} ${CXXFLAGS} ; do |
255 | for f in ${CFLAGS} ${CXXFLAGS} ; do |
| 216 | if [ "${f/${findflag}}" != "${f}" ] ; then |
256 | if [ "${f/${findflag}}" != "${f}" ] ; then |
| 217 | echo "${f/-${findflag}=}" |
257 | echo "${f/-${findflag}=}" |
| 218 | return 0 |
258 | return 0 |
| … | |
… | |
| 255 | LDFLAGS="${LDFLAGS} $@" |
295 | LDFLAGS="${LDFLAGS} $@" |
| 256 | return 0 |
296 | return 0 |
| 257 | } |
297 | } |
| 258 | |
298 | |
| 259 | etexec-flags() { |
299 | etexec-flags() { |
| 260 | has_version 'sys-devel/hardened-gcc' && { |
300 | has_version sys-devel/hardened-gcc |
|
|
301 | if [ $? == 0 ] ; then |
|
|
302 | if [ "`is-flag -yet_exec`" != "true" ]; then |
| 261 | debug-print ">>> appending flags -yet_exec" |
303 | debug-print ">>> appending flags -yet_exec" |
| 262 | append-flags -yet_exec |
304 | append-flags -yet_exec |
| 263 | append-ldflags -yet_exec |
305 | append-ldflags -yet_exec |
| 264 | } |
306 | fi |
|
|
307 | fi |
| 265 | } |
308 | } |
| 266 | |
309 | |
| 267 | fstack-flags() { |
310 | fstack-flags() { |
| 268 | has_version 'sys-devel/hardened-gcc' && { |
311 | has_version sys-devel/hardened-gcc |
|
|
312 | if [ $? == 0 ] ; then |
|
|
313 | if [ "`is-flag -yno_propolice`" != "true" ]; then |
| 269 | debug-print ">>> appending flags -yno_propolice" |
314 | debug-print ">>> appending flags -yno_propolice" |
| 270 | append-flags -yno_propolice |
315 | append-flags -yno_propolice |
| 271 | append-ldflags -yno_propolice |
316 | append-ldflags -yno_propolice |
| 272 | } |
317 | fi |
|
|
318 | fi |
| 273 | } |
319 | } |