/[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.28 Revision 1.41
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.28 2003/10/26 00:40:52 vapier Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.41 2004/03/16 21:37:09 solar 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.
32#
33#### strip-unsupported-flags ####
34# Strip C[XX]FLAGS of any flags not supported by
35# installed version of gcc
28# 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 ####
39# will leave the user with -mfpmath=386 47# will leave the user with -mfpmath=386
40# 48#
41#### append-ldflags #### 49#### append-ldflags ####
42# Add extra flags to your current LDFLAGS 50# Add extra flags to your current LDFLAGS
43# 51#
52#### filter-ldflags <flags> ####
53# Remove particular flags from LDFLAGS
54# Matches only complete flags
55#
44#### etexec-flags #### 56#### etexec-flags ####
45# hooked function for hardened-gcc that appends 57# hooked function for hardened-gcc that appends
46# -yet_exec {C,CXX,LD}FLAGS when hardened-gcc is installed 58# -yet_exec {C,CXX,LD}FLAGS when hardened-gcc is installed
47# and a package is filtering -fPIC 59# and a package is filtering -fPIC,-fpic, -fPIE, -fpie
48# 60#
49#### fstack-flags #### 61#### fstack-flags ####
50# hooked function for hardened-gcc that appends 62# hooked function for hardened-gcc that appends
51# -yno_propolice to {C,CXX,LD}FLAGS when hardened-gcc is installed 63# -yno_propolice to {C,CXX,LD}FLAGS when hardened-gcc is installed
52# and a package is filtering -fstack-protector 64# and a package is filtering -fstack-protector, -fstack-protector-all
53# 65#
54 66
55# C[XX]FLAGS that we allow in strip-flags 67# C[XX]FLAGS that we allow in strip-flags
68setup-allowed-flags() {
56ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -pipe -g" 69 export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -pipe -g"
57case "${ARCH}" in 70 case "${ARCH}" in
58 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; 71 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;;
59 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 72 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
60 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 73 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
74 ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
61esac 75 esac
62 76
63# C[XX]FLAGS that we are think is ok, but needs testing 77 # C[XX]FLAGS that we are think is ok, but needs testing
64# NOTE: currently -Os have issues with gcc3 and K6* arch's 78 # NOTE: currently -Os have issues with gcc3 and K6* arch's
65UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" 79 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays"
66 80 return 0
67filter-mfpmath() {
68 for a in $CFLAGS; do
69 if [ "${a:0:8}" == "-mfpmath" ]; then
70 orig_mfpmath=$a
71 fi
72 done
73
74 mfpmath="$( echo $orig_mfpmath | awk -F '=' '{print $2}' | tr "," " " )"
75 for b in $@; do
76 mfpmath="${mfpmath/$b}"
77 done
78
79 if [ -z "${mfpmath/ }" ]; then
80 filter-flags "$orig_mfpmath"
81 else
82 new_mfpmath="-mfpmath=$( echo $mfpmath | sed -e "s/ /,/g" -e "s/,,/,/g" )"
83 replace-flags "$orig_mfpmath" "$new_mfpmath"
84 fi
85} 81}
86 82
87filter-flags() { 83filter-flags() {
88 # we do two loops to avoid the first and last char from being chomped.
89 for x in $@ ; do 84 for x in "$@" ; do
90 case "${x}" in 85 case "${x}" in
91 -fPIC) etexec-flags;; 86 -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;;
92 -fstack-protector) fstack-flags;; 87 -fstack-protector|-fstack-protector-all) fstack-flags;;
93 *) ;; 88 *) ;;
94 esac 89 esac
95 done 90 done
91
96 # we do this fancy spacing stuff so as to not filter 92 # we do this fancy spacing stuff so as to not filter
97 # out part of a flag ... we want flag atoms ! :D 93 # out part of a flag ... we want flag atoms ! :D
98 CFLAGS=" ${CFLAGS} " 94 CFLAGS=" ${CFLAGS} "
99 CXXFLAGS=" ${CXXFLAGS} " 95 CXXFLAGS=" ${CXXFLAGS} "
100 for x in $@ ; do 96 for x in "$@" ; do
101 CFLAGS="${CFLAGS/ ${x} / }" 97 CFLAGS="${CFLAGS// ${x} / }"
102 CXXFLAGS="${CXXFLAGS/ ${x} / }" 98 CXXFLAGS="${CXXFLAGS// ${x} / }"
103 done 99 done
104 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" 100 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
105 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" 101 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
102 return 0
103}
104
105filter-lfs-flags() {
106 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
106} 107}
107 108
108append-flags() { 109append-flags() {
109 CFLAGS="${CFLAGS} $@" 110 export CFLAGS="${CFLAGS} $@"
110 CXXFLAGS="${CXXFLAGS} $@" 111 export CXXFLAGS="${CXXFLAGS} $@"
111 for x in $@; do 112 [ "`is-flag -fno-stack-protector`" -o "`is-flag -fno-stack-protector-all`" ] && fstack-flags
112 [ "${x}" = "-fno-stack-protector" ] && fstack-flags
113 done
114 return 0 113 return 0
115} 114}
116 115
117replace-flags() { 116replace-flags() {
117 # we do this fancy spacing stuff so as to not filter
118 # out part of a flag ... we want flag atoms ! :D
119 CFLAGS=" ${CFLAGS} "
120 CXXFLAGS=" ${CXXFLAGS} "
118 CFLAGS="${CFLAGS/${1}/${2} }" 121 CFLAGS="${CFLAGS// ${1} / ${2} }"
119 CXXFLAGS="${CXXFLAGS/${1}/${2} }" 122 CXXFLAGS="${CXXFLAGS// ${1} / ${2} }"
123 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
124 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
125 return 0
126}
127
128replace-cpu-flags() {
129 local newcpu="$1" ; shift
130 local oldcpu=""
131 for oldcpu in "$@" ; do
132 replace-flags -march=${oldcpu} -march=${newcpu}
133 replace-flags -mcpu=${oldcpu} -mcpu=${newcpu}
134 replace-flags -mtune=${oldcpu} -mtune=${newcpu}
135 done
120 return 0 136 return 0
121} 137}
122 138
123is-flag() { 139is-flag() {
124 for x in ${CFLAGS} ${CXXFLAGS} ; do 140 for x in ${CFLAGS} ${CXXFLAGS} ; do
128 fi 144 fi
129 done 145 done
130 return 1 146 return 1
131} 147}
132 148
149filter-mfpmath() {
150 # save the original -mfpmath flag
151 local orig_mfpmath="`get-flag -mfpmath`"
152 # get the value of the current -mfpmath flag
153 local new_math=" `get-flag mfpmath | tr , ' '` "
154 # figure out which math values are to be removed
155 local prune_math=""
156 for prune_math in "$@" ; do
157 new_math="${new_math/ ${prune_math} / }"
158 done
159 new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`"
160
161 if [ -z "${new_math}" ] ; then
162 # if we're removing all user specified math values are
163 # slated for removal, then we just filter the flag
164 filter-flags ${orig_mfpmath}
165 else
166 # if we only want to filter some of the user specified
167 # math values, then we replace the current flag
168 replace-flags ${orig_mfpmath} -mfpmath=${new_math}
169 fi
170 return 0
171}
172
133strip-flags() { 173strip-flags() {
174 setup-allowed-flags
175
134 local NEW_CFLAGS="" 176 local NEW_CFLAGS=""
135 local NEW_CXXFLAGS="" 177 local NEW_CXXFLAGS=""
136 178
137 # Allow unstable C[XX]FLAGS if we are using unstable profile ... 179 # Allow unstable C[XX]FLAGS if we are using unstable profile ...
138 if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then 180 if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then
166 break 208 break
167 fi 209 fi
168 done 210 done
169 done 211 done
170 212
213 # In case we filtered out all optimization flags fallback to -O2
214 if [ "${CFLAGS/-O}" != "${CFLAGS}" -a "${NEW_CFLAGS/-O}" = "${NEW_CFLAGS}" ]; then
215 NEW_CFLAGS="${NEW_CFLAGS} -O2"
216 fi
217 if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then
218 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2"
219 fi
220
171 set +f 221 set +f
172 222
173 [ `use debug` ] \ 223 [ `use debug` ] \
174 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \ 224 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \
175 && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\"" 225 && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\""
176 226
177 export CFLAGS="${NEW_CFLAGS}" 227 export CFLAGS="${NEW_CFLAGS}"
178 export CXXFLAGS="${NEW_CXXFLAGS}" 228 export CXXFLAGS="${NEW_CXXFLAGS}"
229 return 0
230}
231
232test_flag() {
233 if gcc -S -xc "$@" -o /dev/null /dev/null >/dev/null 2>&1; then
234 echo "$@"
235 return 0
236 fi
237 return 1
238}
239
240strip-unsupported-flags() {
241 for x in ${CFLAGS} ; do
242 NEW_CFLAGS=${NEW_CFLAGS}" ""`test_flag ${x}`"
243 done
244 for x in ${CXXFLAGS} ; do
245 NEW_CXXFLAGS=${NEW_CXXFLAGS}" ""`test_flag ${x}`"
246 done
247
248 export CFLAGS="${NEW_CFLAGS}"
249 export CXXFLAGS="${NEW_CXXFLAGS}"
179} 250}
180 251
181get-flag() { 252get-flag() {
253 # this code looks a little flaky but seems to work for
254 # everything we want ...
255 # for example, if CFLAGS="-march=i686":
256 # `get-flags -march` == "-march=i686"
257 # `get-flags march` == "i686"
182 local findflag="$1" 258 local findflag="$1"
183 for f in ${CFLAGS} ${CXXFLAGS} ; do 259 for f in ${CFLAGS} ${CXXFLAGS} ; do
184 if [ "${f/${findflag}}" != "${f}" ] ; then 260 if [ "${f/${findflag}}" != "${f}" ] ; then
185 echo "${f/-${findflag}=}" 261 echo "${f/-${findflag}=}"
186 return 0 262 return 0
222append-ldflags() { 298append-ldflags() {
223 LDFLAGS="${LDFLAGS} $@" 299 LDFLAGS="${LDFLAGS} $@"
224 return 0 300 return 0
225} 301}
226 302
303filter-ldflags() {
304 # we do this fancy spacing stuff so as to not filter
305 # out part of a flag ... we want flag atoms ! :D
306 LDFLAGS=" ${LDFLAGS} "
307 for x in "$@" ; do
308 LDFLAGS="${LDFLAGS// ${x} / }"
309 done
310 LDFLAGS="${LDFLAGS:1:${#LDFLAGS}-2}"
311 return 0
312}
313
227etexec-flags() { 314etexec-flags() {
228 has_version 'sys-devel/hardened-gcc' && { 315 has_version sys-devel/hardened-gcc
316 if [ $? == 0 ] ; then
317 if [ "`is-flag -yet_exec`" != "true" ]; then
229 debug-print ">>> appending flags -yet_exec" 318 debug-print ">>> appending flags -yet_exec"
230 append-flags -yet_exec 319 append-flags -yet_exec
231 append-ldflags -yet_exec 320 append-ldflags -yet_exec
232 } 321 fi
322 fi
233} 323}
234 324
235fstack-flags() { 325fstack-flags() {
236 has_version 'sys-devel/hardened-gcc' && { 326 has_version sys-devel/hardened-gcc
327 if [ $? == 0 ] ; then
328 if [ "`is-flag -yno_propolice`" != "true" ]; then
237 debug-print ">>> appending flags -yno_propolice" 329 debug-print ">>> appending flags -yno_propolice"
238 append-flags -yno_propolice 330 append-flags -yno_propolice
239 append-ldflags -yno_propolice 331 append-ldflags -yno_propolice
240 } 332 fi
333 fi
241} 334}

Legend:
Removed from v.1.28  
changed lines
  Added in v.1.41

  ViewVC Help
Powered by ViewVC 1.1.20