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