| 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.47 2004/04/12 02:33:29 swtaylor 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 | #### filter-ldflags <flags> #### |
|
|
53 | # Remove particular flags from LDFLAGS |
|
|
54 | # Matches only complete flags |
|
|
55 | # |
|
|
56 | #### etexec-flags #### |
|
|
57 | # hooked function for hardened-gcc that appends |
|
|
58 | # -yet_exec {C,CXX,LD}FLAGS when hardened-gcc is installed |
|
|
59 | # and a package is filtering -fPIC,-fpic, -fPIE, -fpie |
|
|
60 | # |
|
|
61 | #### fstack-flags #### |
|
|
62 | # hooked function for hardened-gcc that appends |
|
|
63 | # -yno_propolice to {C,CXX,LD}FLAGS when hardened-gcc is installed |
|
|
64 | # and a package is filtering -fstack-protector, -fstack-protector-all |
|
|
65 | # |
| 37 | |
66 | |
| 38 | # C[XX]FLAGS that we allow in strip-flags |
67 | # C[XX]FLAGS that we allow in strip-flags |
|
|
68 | setup-allowed-flags() { |
|
|
69 | if [ -z "${ALLOWED_FLAGS}" ] ; then |
| 39 | ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -pipe -g" |
70 | export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -pipe -g" |
| 40 | case "${ARCH}" in |
71 | case "${ARCH}" in |
| 41 | mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; |
72 | mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; |
| 42 | amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
73 | amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
|
|
74 | alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
|
|
75 | ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
| 43 | esac |
76 | esac |
|
|
77 | fi |
| 44 | |
78 | |
| 45 | # C[XX]FLAGS that we are think is ok, but needs testing |
79 | # C[XX]FLAGS that we are think is ok, but needs testing |
| 46 | # NOTE: currently -Os have issues with gcc3 and K6* arch's |
80 | # NOTE: currently -Os have issues with gcc3 and K6* arch's |
| 47 | UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" |
81 | export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" |
| 48 | |
82 | 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 | } |
83 | } |
| 68 | |
84 | |
| 69 | filter-flags() { |
85 | filter-flags() { |
|
|
86 | for x in "$@" ; do |
|
|
87 | case "${x}" in |
|
|
88 | -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;; |
|
|
89 | -fstack-protector|-fstack-protector-all) fstack-flags;; |
|
|
90 | *) ;; |
|
|
91 | esac |
|
|
92 | done |
|
|
93 | |
| 70 | # we do this fancy spacing stuff so as to not filter |
94 | # we do this fancy spacing stuff so as to not filter |
| 71 | # out part of a flag ... we want flag atoms ! :D |
95 | # out part of a flag ... we want flag atoms ! :D |
| 72 | export CFLAGS=" ${CFLAGS} " |
96 | CFLAGS=" ${CFLAGS} " |
| 73 | export CXXFLAGS=" ${CXXFLAGS} " |
97 | CXXFLAGS=" ${CXXFLAGS} " |
| 74 | for x in $@ ; do |
98 | for x in "$@" ; do |
| 75 | export CFLAGS="${CFLAGS/ ${x} / }" |
99 | CFLAGS="${CFLAGS// ${x} / }" |
| 76 | export CXXFLAGS="${CXXFLAGS/ ${x} / }" |
100 | CXXFLAGS="${CXXFLAGS// ${x} / }" |
| 77 | done |
101 | done |
| 78 | export CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" |
102 | CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" |
| 79 | export CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" |
103 | CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" |
|
|
104 | return 0 |
|
|
105 | } |
|
|
106 | |
|
|
107 | filter-lfs-flags() { |
|
|
108 | filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE |
| 80 | } |
109 | } |
| 81 | |
110 | |
| 82 | append-flags() { |
111 | append-flags() { |
| 83 | CFLAGS="${CFLAGS} $@" |
112 | export CFLAGS="${CFLAGS} $@" |
| 84 | CXXFLAGS="${CXXFLAGS} $@" |
113 | export CXXFLAGS="${CXXFLAGS} $@" |
|
|
114 | [ "`is-flag -fno-stack-protector`" -o "`is-flag -fno-stack-protector-all`" ] && fstack-flags |
|
|
115 | return 0 |
| 85 | } |
116 | } |
| 86 | |
117 | |
| 87 | replace-flags() { |
118 | replace-flags() { |
|
|
119 | # we do this fancy spacing stuff so as to not filter |
|
|
120 | # out part of a flag ... we want flag atoms ! :D |
|
|
121 | CFLAGS=" ${CFLAGS} " |
|
|
122 | CXXFLAGS=" ${CXXFLAGS} " |
| 88 | CFLAGS="${CFLAGS/${1}/${2} }" |
123 | CFLAGS="${CFLAGS// ${1} / ${2} }" |
| 89 | CXXFLAGS="${CXXFLAGS/${1}/${2} }" |
124 | CXXFLAGS="${CXXFLAGS// ${1} / ${2} }" |
|
|
125 | CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" |
|
|
126 | CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" |
|
|
127 | return 0 |
|
|
128 | } |
|
|
129 | |
|
|
130 | replace-cpu-flags() { |
|
|
131 | local newcpu="$1" ; shift |
|
|
132 | local oldcpu="" |
|
|
133 | for oldcpu in "$@" ; do |
|
|
134 | replace-flags -march=${oldcpu} -march=${newcpu} |
|
|
135 | replace-flags -mcpu=${oldcpu} -mcpu=${newcpu} |
|
|
136 | replace-flags -mtune=${oldcpu} -mtune=${newcpu} |
|
|
137 | done |
|
|
138 | return 0 |
| 90 | } |
139 | } |
| 91 | |
140 | |
| 92 | is-flag() { |
141 | is-flag() { |
| 93 | for x in ${CFLAGS} ${CXXFLAGS} ; do |
142 | for x in ${CFLAGS} ${CXXFLAGS} ; do |
| 94 | if [ "${x}" == "$1" ] ; then |
143 | if [ "${x}" == "$1" ] ; then |
| … | |
… | |
| 97 | fi |
146 | fi |
| 98 | done |
147 | done |
| 99 | return 1 |
148 | return 1 |
| 100 | } |
149 | } |
| 101 | |
150 | |
|
|
151 | filter-mfpmath() { |
|
|
152 | # save the original -mfpmath flag |
|
|
153 | local orig_mfpmath="`get-flag -mfpmath`" |
|
|
154 | # get the value of the current -mfpmath flag |
|
|
155 | local new_math=" `get-flag mfpmath | tr , ' '` " |
|
|
156 | # figure out which math values are to be removed |
|
|
157 | local prune_math="" |
|
|
158 | for prune_math in "$@" ; do |
|
|
159 | new_math="${new_math/ ${prune_math} / }" |
|
|
160 | done |
|
|
161 | new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`" |
|
|
162 | |
|
|
163 | if [ -z "${new_math}" ] ; then |
|
|
164 | # if we're removing all user specified math values are |
|
|
165 | # slated for removal, then we just filter the flag |
|
|
166 | filter-flags ${orig_mfpmath} |
|
|
167 | else |
|
|
168 | # if we only want to filter some of the user specified |
|
|
169 | # math values, then we replace the current flag |
|
|
170 | replace-flags ${orig_mfpmath} -mfpmath=${new_math} |
|
|
171 | fi |
|
|
172 | return 0 |
|
|
173 | } |
|
|
174 | |
| 102 | strip-flags() { |
175 | strip-flags() { |
|
|
176 | setup-allowed-flags |
|
|
177 | |
| 103 | local NEW_CFLAGS="" |
178 | local NEW_CFLAGS="" |
| 104 | local NEW_CXXFLAGS="" |
179 | local NEW_CXXFLAGS="" |
| 105 | |
180 | |
| 106 | # Allow unstable C[XX]FLAGS if we are using unstable profile ... |
181 | # Allow unstable C[XX]FLAGS if we are using unstable profile ... |
| 107 | if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then |
182 | if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then |
| … | |
… | |
| 135 | break |
210 | break |
| 136 | fi |
211 | fi |
| 137 | done |
212 | done |
| 138 | done |
213 | done |
| 139 | |
214 | |
|
|
215 | # In case we filtered out all optimization flags fallback to -O2 |
|
|
216 | if [ "${CFLAGS/-O}" != "${CFLAGS}" -a "${NEW_CFLAGS/-O}" = "${NEW_CFLAGS}" ]; then |
|
|
217 | NEW_CFLAGS="${NEW_CFLAGS} -O2" |
|
|
218 | fi |
|
|
219 | if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then |
|
|
220 | NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2" |
|
|
221 | fi |
|
|
222 | |
| 140 | set +f |
223 | set +f |
| 141 | |
224 | |
| 142 | [ `use debug` ] \ |
225 | [ `use debug` ] \ |
| 143 | && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \ |
226 | && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \ |
| 144 | && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\"" |
227 | && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\"" |
| 145 | |
228 | |
| 146 | export CFLAGS="${NEW_CFLAGS}" |
229 | export CFLAGS="${NEW_CFLAGS}" |
| 147 | export CXXFLAGS="${NEW_CXXFLAGS}" |
230 | export CXXFLAGS="${NEW_CXXFLAGS}" |
|
|
231 | return 0 |
|
|
232 | } |
|
|
233 | |
|
|
234 | test_flag() { |
|
|
235 | if [ -z "`gcc -S -xc "$@" -o /dev/null /dev/null 2>&1`" ]; then |
|
|
236 | echo "$@" |
|
|
237 | return 0 |
|
|
238 | fi |
|
|
239 | return 1 |
|
|
240 | } |
|
|
241 | |
|
|
242 | strip-unsupported-flags() { |
|
|
243 | for x in ${CFLAGS} ; do |
|
|
244 | NEW_CFLAGS=${NEW_CFLAGS}" ""`test_flag ${x}`" |
|
|
245 | done |
|
|
246 | for x in ${CXXFLAGS} ; do |
|
|
247 | NEW_CXXFLAGS=${NEW_CXXFLAGS}" ""`test_flag ${x}`" |
|
|
248 | done |
|
|
249 | |
|
|
250 | export CFLAGS="${NEW_CFLAGS}" |
|
|
251 | export CXXFLAGS="${NEW_CXXFLAGS}" |
| 148 | } |
252 | } |
| 149 | |
253 | |
| 150 | get-flag() { |
254 | get-flag() { |
|
|
255 | # this code looks a little flaky but seems to work for |
|
|
256 | # everything we want ... |
|
|
257 | # for example, if CFLAGS="-march=i686": |
|
|
258 | # `get-flags -march` == "-march=i686" |
|
|
259 | # `get-flags march` == "i686" |
| 151 | local findflag="$1" |
260 | local findflag="$1" |
| 152 | |
|
|
| 153 | for f in ${CFLAGS} ${CXXFLAGS} ; do |
261 | for f in ${CFLAGS} ${CXXFLAGS} ; do |
| 154 | if [ "${f/${findflag}}" != "${f}" ] ; then |
262 | if [ "${f/${findflag}}" != "${f}" ] ; then |
| 155 | echo "${f/-${findflag}=}" |
263 | echo "${f/-${findflag}=}" |
| 156 | return |
264 | return 0 |
| 157 | fi |
265 | fi |
| 158 | done |
266 | done |
|
|
267 | return 1 |
|
|
268 | } |
|
|
269 | |
|
|
270 | has_pic() { |
|
|
271 | [ "${CFLAGS/-fPIC}" != "${CFLAGS}" ] && return 0 |
|
|
272 | [ "${CFLAGS/-fpic}" != "${CFLAGS}" ] && return 0 |
|
|
273 | has_version sys-devel/hardened-gcc && return 0 |
|
|
274 | [ ! -z "`${CC/ .*/} --version| grep pie`" ] && return 0 |
|
|
275 | return 1 |
|
|
276 | } |
|
|
277 | |
|
|
278 | has_pie() { |
|
|
279 | [ "${CFLAGS/-fPIE}" != "${CFLAGS}" ] && return 0 |
|
|
280 | [ "${CFLAGS/-fpie}" != "${CFLAGS}" ] && return 0 |
|
|
281 | has_version sys-devel/hardened-gcc && return 0 |
|
|
282 | [ ! -z "`${CC/ .*/} --version| grep pie`" ] && return 0 |
|
|
283 | return 1 |
|
|
284 | } |
|
|
285 | |
|
|
286 | has_ssp() { |
|
|
287 | [ "${CFLAGS/-fstack-protector}" != "${CFLAGS}" ] && return 0 |
|
|
288 | has_version sys-devel/hardened-gcc && return 0 |
|
|
289 | [ ! -z "`${CC/ .*/} --version| grep ssp`" ] && return 0 |
|
|
290 | return 1 |
| 159 | } |
291 | } |
| 160 | |
292 | |
| 161 | replace-sparc64-flags() { |
293 | replace-sparc64-flags() { |
| 162 | local SPARC64_CPUS="ultrasparc v9" |
294 | local SPARC64_CPUS="ultrasparc v9" |
| 163 | |
295 | |
| … | |
… | |
| 185 | do |
317 | do |
| 186 | CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" |
318 | CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" |
| 187 | done |
319 | done |
| 188 | fi |
320 | fi |
| 189 | } |
321 | } |
|
|
322 | |
|
|
323 | append-ldflags() { |
|
|
324 | LDFLAGS="${LDFLAGS} $@" |
|
|
325 | return 0 |
|
|
326 | } |
|
|
327 | |
|
|
328 | filter-ldflags() { |
|
|
329 | # we do this fancy spacing stuff so as to not filter |
|
|
330 | # out part of a flag ... we want flag atoms ! :D |
|
|
331 | LDFLAGS=" ${LDFLAGS} " |
|
|
332 | for x in "$@" ; do |
|
|
333 | LDFLAGS="${LDFLAGS// ${x} / }" |
|
|
334 | done |
|
|
335 | LDFLAGS="${LDFLAGS:1:${#LDFLAGS}-2}" |
|
|
336 | return 0 |
|
|
337 | } |
|
|
338 | |
|
|
339 | etexec-flags() { |
|
|
340 | has_pie || has_pic |
|
|
341 | if [ $? == 0 ] ; then |
|
|
342 | [ -z "`is-flag -yet_exec`" ] && |
|
|
343 | export CFLAGS="${CFLAGS} `test_flag -yet_exec`" |
|
|
344 | [ -z "`is-flag -nopie`" ] && |
|
|
345 | export CFLAGS="${CFLAGS} `test_flag -nopie`" |
|
|
346 | fi |
|
|
347 | } |
|
|
348 | |
|
|
349 | fstack-flags() { |
|
|
350 | has_ssp |
|
|
351 | if [ $? == 0 ] ; then |
|
|
352 | [ -z "`is-flag -yno_propolice`" ] && |
|
|
353 | export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`" |
|
|
354 | fi |
|
|
355 | } |