/[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.27 Revision 1.37
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.27 2003/09/24 05:51:32 solar 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
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 ####
42# Add extra flags to your current LDFLAGS 50# Add extra flags to your current LDFLAGS
43# 51#
44#### etexec-flags #### 52#### etexec-flags ####
45# hooked function for hardened-gcc that appends 53# hooked function for hardened-gcc that appends
46# -yet_exec {C,CXX,LD}FLAGS when hardened-gcc is installed 54# -yet_exec {C,CXX,LD}FLAGS when hardened-gcc is installed
47# and a package is filtering -fPIC 55# and a package is filtering -fPIC,-fpic, -fPIE, -fpie
48# 56#
49#### fstack-flags #### 57#### fstack-flags ####
50# hooked function for hardened-gcc that appends 58# hooked function for hardened-gcc that appends
51# -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
52# and a package is filtering -fstack-protector 60# and a package is filtering -fstack-protector, -fstack-protector-all
53# 61#
54 62
55# C[XX]FLAGS that we allow in strip-flags 63# C[XX]FLAGS that we allow in strip-flags
64setup-allowed-flags() {
56ALLOWED_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"
57case "${ARCH}" in 66 case "${ARCH}" in
58 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; 67 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;;
59 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 68 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
60 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 69 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
70 ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
61esac 71 esac
62 72
63# 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
64# NOTE: currently -Os have issues with gcc3 and K6* arch's 74 # NOTE: currently -Os have issues with gcc3 and K6* arch's
65UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" 75 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays"
66 76 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} 77}
86 78
87filter-flags() { 79filter-flags() {
88 # we do two loops to avoid the first and last char from being chomped.
89 for x in $@ ; do 80 for x in "$@" ; do
90 case "${x}" in 81 case "${x}" in
91 -fPIC) etexec-flags;; 82 -fPIC|-fpic|-fPIE|-fpie) etexec-flags;;
92 -fstack-protector) fstack-flags;; 83 -fstack-protector|-fstack-protector-all) fstack-flags;;
93 *) ;; 84 *) ;;
94 esac 85 esac
95 done 86 done
87
96 # we do this fancy spacing stuff so as to not filter 88 # we do this fancy spacing stuff so as to not filter
97 # out part of a flag ... we want flag atoms ! :D 89 # out part of a flag ... we want flag atoms ! :D
98 CFLAGS=" ${CFLAGS} " 90 CFLAGS=" ${CFLAGS} "
99 CXXFLAGS=" ${CXXFLAGS} " 91 CXXFLAGS=" ${CXXFLAGS} "
100 for x in $@ ; do 92 for x in "$@" ; do
101 CFLAGS="${CFLAGS/ ${x} / }" 93 CFLAGS="${CFLAGS// ${x} / }"
102 CXXFLAGS="${CXXFLAGS/ ${x} / }" 94 CXXFLAGS="${CXXFLAGS// ${x} / }"
103 done 95 done
104 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" 96 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
105 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" 97 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
98 return 0
106} 99}
107 100
108append-flags() { 101append-flags() {
109 CFLAGS="${CFLAGS} $@" 102 export CFLAGS="${CFLAGS} $@"
110 CXXFLAGS="${CXXFLAGS} $@" 103 export CXXFLAGS="${CXXFLAGS} $@"
111 for x in $@; do 104 [ "`is-flag -fno-stack-protector`" -o "`is-flag -fno-stack-protector-all`" ] && fstack-flags
112 [ "${x}" = "-fno-stack-protector" ] && 105 return 0
113 fstack-flags
114 done
115} 106}
116 107
117replace-flags() { 108replace-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} "
118 CFLAGS="${CFLAGS/${1}/${2} }" 113 CFLAGS="${CFLAGS// ${1} / ${2} }"
119 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
120replace-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
127 return 0
120} 128}
121 129
122is-flag() { 130is-flag() {
123 for x in ${CFLAGS} ${CXXFLAGS} ; do 131 for x in ${CFLAGS} ${CXXFLAGS} ; do
124 if [ "${x}" == "$1" ] ; then 132 if [ "${x}" == "$1" ] ; then
127 fi 135 fi
128 done 136 done
129 return 1 137 return 1
130} 138}
131 139
140filter-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
132strip-flags() { 164strip-flags() {
165 setup-allowed-flags
166
133 local NEW_CFLAGS="" 167 local NEW_CFLAGS=""
134 local NEW_CXXFLAGS="" 168 local NEW_CXXFLAGS=""
135 169
136 # Allow unstable C[XX]FLAGS if we are using unstable profile ... 170 # Allow unstable C[XX]FLAGS if we are using unstable profile ...
137 if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then 171 if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then
165 break 199 break
166 fi 200 fi
167 done 201 done
168 done 202 done
169 203
204 # In case we filtered out all optimization flags fallback to -O2
205 if [ "${CFLAGS/-O}" != "${CFLAGS}" -a "${NEW_CFLAGS/-O}" = "${NEW_CFLAGS}" ]; then
206 NEW_CFLAGS="${NEW_CFLAGS} -O2"
207 fi
208 if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then
209 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2"
210 fi
211
170 set +f 212 set +f
171 213
172 [ `use debug` ] \ 214 [ `use debug` ] \
173 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \ 215 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \
174 && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\"" 216 && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\""
175 217
176 export CFLAGS="${NEW_CFLAGS}" 218 export CFLAGS="${NEW_CFLAGS}"
177 export CXXFLAGS="${NEW_CXXFLAGS}" 219 export CXXFLAGS="${NEW_CXXFLAGS}"
220 return 0
221}
222
223test_flag() {
224 if gcc -S -xc "$@" -o /dev/null /dev/null >/dev/null 2>&1; then
225 echo "$@"
226 return 0
227 fi
228 return 1
229}
230
231strip-unsupported-flags() {
232 for x in ${CFLAGS} ; do
233 NEW_CFLAGS=${NEW_CFLAGS}" ""`test_flag ${x}`"
234 done
235 for x in ${CXXFLAGS} ; do
236 NEW_CXXFLAGS=${NEW_CXXFLAGS}" ""`test_flag ${x}`"
237 done
238
239 export CFLAGS="${NEW_CFLAGS}"
240 export CXXFLAGS="${NEW_CXXFLAGS}"
178} 241}
179 242
180get-flag() { 243get-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"
181 local findflag="$1" 249 local findflag="$1"
182
183 for f in ${CFLAGS} ${CXXFLAGS} ; do 250 for f in ${CFLAGS} ${CXXFLAGS} ; do
184 if [ "${f/${findflag}}" != "${f}" ] ; then 251 if [ "${f/${findflag}}" != "${f}" ] ; then
185 echo "${f/-${findflag}=}" 252 echo "${f/-${findflag}=}"
186 return 253 return 0
187 fi 254 fi
188 done 255 done
256 return 1
189} 257}
190 258
191replace-sparc64-flags() { 259replace-sparc64-flags() {
192 local SPARC64_CPUS="ultrasparc v9" 260 local SPARC64_CPUS="ultrasparc v9"
193 261
218 fi 286 fi
219} 287}
220 288
221append-ldflags() { 289append-ldflags() {
222 LDFLAGS="${LDFLAGS} $@" 290 LDFLAGS="${LDFLAGS} $@"
291 return 0
223} 292}
224 293
225etexec-flags() { 294etexec-flags() {
226 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
227 debug-print ">>> appending flags -yet_exec" 298 debug-print ">>> appending flags -yet_exec"
228 append-flags -yet_exec 299 append-flags -yet_exec
229 append-ldflags -yet_exec 300 append-ldflags -yet_exec
230 } 301 fi
302 fi
231} 303}
232 304
233fstack-flags() { 305fstack-flags() {
234 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
235 debug-print ">>> appending flags -yno_propolice" 309 debug-print ">>> appending flags -yno_propolice"
236 append-flags -yno_propolice 310 append-flags -yno_propolice
237 append-ldflags -yno_propolice 311 append-ldflags -yno_propolice
238 } 312 fi
313 fi
239} 314}

Legend:
Removed from v.1.27  
changed lines
  Added in v.1.37

  ViewVC Help
Powered by ViewVC 1.1.20