/[gentoo-x86]/eclass/flag-o-matic.eclass
Gentoo

Diff of /eclass/flag-o-matic.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.1.24  
changed lines
  Added in v.1.52

  ViewVC Help
Powered by ViewVC 1.1.20