/[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.41 Revision 1.60
1# Copyright 1999-2003 Gentoo Technologies, Inc. 1# Copyright 1999-2004 Gentoo Foundation
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.41 2004/03/16 21:37:09 solar Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/flag-o-matic.eclass,v 1.60 2004/06/25 00:39:48 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"
9IUSE="$IUSE debug"
9 10
10# 11#
11#### filter-flags <flags> #### 12#### filter-flags <flags> ####
12# Remove particular flags from C[XX]FLAGS 13# Remove particular flags from C[XX]FLAGS
13# Matches only complete flags 14# Matches only complete flags
52#### filter-ldflags <flags> #### 53#### filter-ldflags <flags> ####
53# Remove particular flags from LDFLAGS 54# Remove particular flags from LDFLAGS
54# Matches only complete flags 55# Matches only complete flags
55# 56#
56#### etexec-flags #### 57#### etexec-flags ####
57# hooked function for hardened-gcc that appends 58# hooked function for hardened gcc that appends
58# -yet_exec {C,CXX,LD}FLAGS when hardened-gcc is installed 59# -fno-pic to {C,CXX,LD}FLAGS
59# and a package is filtering -fPIC,-fpic, -fPIE, -fpie 60# when a package is filtering -fpic, -fPIC, -fpie, -fPIE
60# 61#
61#### fstack-flags #### 62#### fstack-flags ####
62# hooked function for hardened-gcc that appends 63# hooked function for hardened gcc that appends
63# -yno_propolice to {C,CXX,LD}FLAGS when hardened-gcc is installed 64# -fno-stack-protector to {C,CXX,LD}FLAGS
64# and a package is filtering -fstack-protector, -fstack-protector-all 65# when a package is filtering -fstack-protector, -fstack-protector-all
66# notice: modern automatic specs files will also suppress -fstack-protector-all
67# when only -fno-stack-protector is given
65# 68#
66 69
67# C[XX]FLAGS that we allow in strip-flags 70# C[XX]FLAGS that we allow in strip-flags
68setup-allowed-flags() { 71setup-allowed-flags() {
72 if [ -z "${ALLOWED_FLAGS}" ] ; then
69 export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -pipe -g" 73 export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -fno-unit-at-a-time -pipe -g"
70 case "${ARCH}" in 74 case "${ARCH}" in
71 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; 75 mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;;
72 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 76 amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC -m64" ;;
77 x86) ALLOWED_FLAGS="${ALLOWED_FLAGS} -m32" ;;
73 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 78 alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
74 ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; 79 ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;;
75 esac 80 esac
81 fi
76 82
77 # C[XX]FLAGS that we are think is ok, but needs testing 83 # C[XX]FLAGS that we are think is ok, but needs testing
78 # NOTE: currently -Os have issues with gcc3 and K6* arch's 84 # NOTE: currently -Os have issues with gcc3 and K6* arch's
79 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" 85 export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays"
80 return 0 86 return 0
81} 87}
82 88
83filter-flags() { 89filter-flags() {
90 local x
91
84 for x in "$@" ; do 92 for x in "$@" ; do
85 case "${x}" in 93 case "${x}" in
86 -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;; 94 -fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;;
87 -fstack-protector|-fstack-protector-all) fstack-flags;; 95 -fstack-protector|-fstack-protector-all) fstack-flags;;
88 *) ;; 96 *) ;;
97 CFLAGS="${CFLAGS// ${x} / }" 105 CFLAGS="${CFLAGS// ${x} / }"
98 CXXFLAGS="${CXXFLAGS// ${x} / }" 106 CXXFLAGS="${CXXFLAGS// ${x} / }"
99 done 107 done
100 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" 108 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
101 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" 109 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
110 export CFLAGS CXXFLAGS
102 return 0 111 return 0
103} 112}
104 113
105filter-lfs-flags() { 114filter-lfs-flags() {
106 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE 115 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
107} 116}
108 117
118append-lfs-flags() {
119 append-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
120}
121
109append-flags() { 122append-flags() {
110 export CFLAGS="${CFLAGS} $@" 123 export CFLAGS="${CFLAGS} $*"
111 export CXXFLAGS="${CXXFLAGS} $@" 124 export CXXFLAGS="${CXXFLAGS} $*"
112 [ "`is-flag -fno-stack-protector`" -o "`is-flag -fno-stack-protector-all`" ] && fstack-flags 125 [ -n "`is-flag -fno-stack-protector`" -o \
126 -n "`is-flag -fno-stack-protector-all`" ] && fstack-flags
113 return 0 127 return 0
114} 128}
115 129
116replace-flags() { 130replace-flags() {
117 # we do this fancy spacing stuff so as to not filter 131 # we do this fancy spacing stuff so as to not filter
120 CXXFLAGS=" ${CXXFLAGS} " 134 CXXFLAGS=" ${CXXFLAGS} "
121 CFLAGS="${CFLAGS// ${1} / ${2} }" 135 CFLAGS="${CFLAGS// ${1} / ${2} }"
122 CXXFLAGS="${CXXFLAGS// ${1} / ${2} }" 136 CXXFLAGS="${CXXFLAGS// ${1} / ${2} }"
123 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" 137 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
124 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" 138 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
139 export CFLAGS CXXFLAGS
125 return 0 140 return 0
126} 141}
127 142
128replace-cpu-flags() { 143replace-cpu-flags() {
129 local newcpu="$1" ; shift 144 local oldcpu newcpu="$1" ; shift
130 local oldcpu=""
131 for oldcpu in "$@" ; do 145 for oldcpu in "$@" ; do
132 replace-flags -march=${oldcpu} -march=${newcpu} 146 replace-flags -march=${oldcpu} -march=${newcpu}
133 replace-flags -mcpu=${oldcpu} -mcpu=${newcpu} 147 replace-flags -mcpu=${oldcpu} -mcpu=${newcpu}
134 replace-flags -mtune=${oldcpu} -mtune=${newcpu} 148 replace-flags -mtune=${oldcpu} -mtune=${newcpu}
135 done 149 done
136 return 0 150 return 0
137} 151}
138 152
139is-flag() { 153is-flag() {
154 local x
155
140 for x in ${CFLAGS} ${CXXFLAGS} ; do 156 for x in ${CFLAGS} ${CXXFLAGS} ; do
141 if [ "${x}" == "$1" ] ; then 157 if [ "${x}" == "$1" ] ; then
142 echo true 158 echo true
143 return 0 159 return 0
144 fi 160 fi
145 done 161 done
146 return 1 162 return 1
147} 163}
148 164
149filter-mfpmath() { 165filter-mfpmath() {
166 local orig_mfpmath new_math prune_math
167
150 # save the original -mfpmath flag 168 # save the original -mfpmath flag
151 local orig_mfpmath="`get-flag -mfpmath`" 169 orig_mfpmath="`get-flag -mfpmath`"
152 # get the value of the current -mfpmath flag 170 # get the value of the current -mfpmath flag
153 local new_math=" `get-flag mfpmath | tr , ' '` " 171 new_math=" `get-flag mfpmath | tr , ' '` "
154 # figure out which math values are to be removed 172 # figure out which math values are to be removed
155 local prune_math="" 173 prune_math=""
156 for prune_math in "$@" ; do 174 for prune_math in "$@" ; do
157 new_math="${new_math/ ${prune_math} / }" 175 new_math="${new_math/ ${prune_math} / }"
158 done 176 done
159 new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`" 177 new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`"
160 178
169 fi 187 fi
170 return 0 188 return 0
171} 189}
172 190
173strip-flags() { 191strip-flags() {
192 local x y flag NEW_CFLAGS NEW_CXXFLAGS
193
174 setup-allowed-flags 194 setup-allowed-flags
175 195
176 local NEW_CFLAGS="" 196 local NEW_CFLAGS=""
177 local NEW_CXXFLAGS="" 197 local NEW_CXXFLAGS=""
178 198
179 # Allow unstable C[XX]FLAGS if we are using unstable profile ... 199 # Allow unstable C[XX]FLAGS if we are using unstable profile ...
180 if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then 200 if has ~${ARCH} ${ACCEPT_KEYWORDS} ; then
181 [ `use debug` ] && einfo "Enabling the use of some unstable flags" 201 use debug && einfo "Enabling the use of some unstable flags"
182 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}" 202 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}"
183 fi 203 fi
184 204
185 set -f 205 set -f # disable pathname expansion
186 206
187 for x in ${CFLAGS} 207 for x in ${CFLAGS}; do
188 do
189 for y in ${ALLOWED_FLAGS} 208 for y in ${ALLOWED_FLAGS}; do
190 do
191 flag=${x%%=*} 209 flag=${x%%=*}
192 if [ "${flag%%${y}}" = "" ] 210 if [ "${flag%%${y}}" = "" ]; then
193 then
194 NEW_CFLAGS="${NEW_CFLAGS} ${x}" 211 NEW_CFLAGS="${NEW_CFLAGS} ${x}"
195 break 212 break
196 fi 213 fi
197 done 214 done
198 done 215 done
199 216
200 for x in ${CXXFLAGS} 217 for x in ${CXXFLAGS}; do
201 do
202 for y in ${ALLOWED_FLAGS} 218 for y in ${ALLOWED_FLAGS}; do
203 do
204 flag=${x%%=*} 219 flag=${x%%=*}
205 if [ "${flag%%${y}}" = "" ] 220 if [ "${flag%%${y}}" = "" ]; then
206 then
207 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}" 221 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}"
208 break 222 break
209 fi 223 fi
210 done 224 done
211 done 225 done
216 fi 230 fi
217 if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then 231 if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then
218 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2" 232 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2"
219 fi 233 fi
220 234
221 set +f 235 set +f # re-enable pathname expansion
222
223 [ `use debug` ] \
224 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \
225 && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\""
226 236
227 export CFLAGS="${NEW_CFLAGS}" 237 export CFLAGS="${NEW_CFLAGS}"
228 export CXXFLAGS="${NEW_CXXFLAGS}" 238 export CXXFLAGS="${NEW_CXXFLAGS}"
229 return 0 239 return 0
230} 240}
231 241
232test_flag() { 242test_flag() {
243 local cc=${CC:-gcc} ; cc=${cc%% *}
233 if gcc -S -xc "$@" -o /dev/null /dev/null >/dev/null 2>&1; then 244 if ${cc} -S -xc "$@" -o /dev/null /dev/null &>/dev/null; then
234 echo "$@" 245 printf "%s\n" "$*"
235 return 0 246 return 0
236 fi 247 fi
237 return 1 248 return 1
238} 249}
239 250
251test_version_info() {
252 local cc=${CC:-gcc} ; cc=${cc%% *}
253 if [[ $(${cc} --version 2>&1) == *$1* ]]; then
254 return 0
255 else
256 return 1
257 fi
258}
259
240strip-unsupported-flags() { 260strip-unsupported-flags() {
261 local NEW_CFLAGS NEW_CXXFLAGS
262
241 for x in ${CFLAGS} ; do 263 for x in ${CFLAGS} ; do
242 NEW_CFLAGS=${NEW_CFLAGS}" ""`test_flag ${x}`" 264 NEW_CFLAGS="${NEW_CFLAGS} `test_flag ${x}`"
243 done 265 done
244 for x in ${CXXFLAGS} ; do 266 for x in ${CXXFLAGS} ; do
245 NEW_CXXFLAGS=${NEW_CXXFLAGS}" ""`test_flag ${x}`" 267 NEW_CXXFLAGS="${NEW_CXXFLAGS} `test_flag ${x}`"
246 done 268 done
247 269
248 export CFLAGS="${NEW_CFLAGS}" 270 export CFLAGS="${NEW_CFLAGS}"
249 export CXXFLAGS="${NEW_CXXFLAGS}" 271 export CXXFLAGS="${NEW_CXXFLAGS}"
250} 272}
251 273
252get-flag() { 274get-flag() {
275 local f findflag="$1"
276
253 # this code looks a little flaky but seems to work for 277 # this code looks a little flaky but seems to work for
254 # everything we want ... 278 # everything we want ...
255 # for example, if CFLAGS="-march=i686": 279 # for example, if CFLAGS="-march=i686":
256 # `get-flags -march` == "-march=i686" 280 # `get-flag -march` == "-march=i686"
257 # `get-flags march` == "i686" 281 # `get-flag march` == "i686"
258 local findflag="$1"
259 for f in ${CFLAGS} ${CXXFLAGS} ; do 282 for f in ${CFLAGS} ${CXXFLAGS} ; do
260 if [ "${f/${findflag}}" != "${f}" ] ; then 283 if [ "${f/${findflag}}" != "${f}" ] ; then
261 echo "${f/-${findflag}=}" 284 printf "%s\n" "${f/-${findflag}=}"
262 return 0 285 return 0
263 fi 286 fi
264 done 287 done
265 return 1 288 return 1
266} 289}
267 290
291has_hardened() {
292 test_version_info Hardened
293 return $?
294}
295
296has_pic() {
297 [ "${CFLAGS/-fPIC}" != "${CFLAGS}" ] && return 0
298 [ "${CFLAGS/-fpic}" != "${CFLAGS}" ] && return 0
299 test_version_info pie && return 0
300 return 1
301}
302
303has_pie() {
304 [ "${CFLAGS/-fPIE}" != "${CFLAGS}" ] && return 0
305 [ "${CFLAGS/-fpie}" != "${CFLAGS}" ] && return 0
306 test_version_info pie && return 0
307 return 1
308}
309
310has_ssp() {
311 [ "${CFLAGS/-fstack-protector}" != "${CFLAGS}" ] && return 0
312 test_version_info ssp && return 0
313 return 1
314}
315
316has_m64() {
317 test_flag -m64
318 return $?
319}
320
321has_m32() {
322 test_flag -m32
323 return $?
324}
325
268replace-sparc64-flags() { 326replace-sparc64-flags() {
269 local SPARC64_CPUS="ultrasparc v9" 327 local SPARC64_CPUS="ultrasparc v9"
270 328
271 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ] 329 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
272 then
273 for x in ${SPARC64_CPUS} 330 for x in ${SPARC64_CPUS}; do
274 do
275 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}" 331 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
276 done 332 done
277 else 333 else
278 for x in ${SPARC64_CPUS} 334 for x in ${SPARC64_CPUS}; do
279 do
280 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" 335 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
281 done 336 done
282 fi 337 fi
283 338
284 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ] 339 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
285 then
286 for x in ${SPARC64_CPUS} 340 for x in ${SPARC64_CPUS}; do
287 do
288 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}" 341 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
289 done 342 done
290 else 343 else
291 for x in ${SPARC64_CPUS} 344 for x in ${SPARC64_CPUS}; do
292 do
293 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" 345 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
294 done 346 done
295 fi 347 fi
348
349 export CFLAGS CXXFLAGS
296} 350}
297 351
298append-ldflags() { 352append-ldflags() {
299 LDFLAGS="${LDFLAGS} $@" 353 export LDFLAGS="${LDFLAGS} $*"
300 return 0 354 return 0
301} 355}
302 356
303filter-ldflags() { 357filter-ldflags() {
358 local x
359
304 # we do this fancy spacing stuff so as to not filter 360 # we do this fancy spacing stuff so as to not filter
305 # out part of a flag ... we want flag atoms ! :D 361 # out part of a flag ... we want flag atoms ! :D
306 LDFLAGS=" ${LDFLAGS} " 362 LDFLAGS=" ${LDFLAGS} "
307 for x in "$@" ; do 363 for x in "$@" ; do
308 LDFLAGS="${LDFLAGS// ${x} / }" 364 LDFLAGS="${LDFLAGS// ${x} / }"
309 done 365 done
310 LDFLAGS="${LDFLAGS:1:${#LDFLAGS}-2}" 366 LDFLAGS="${LDFLAGS:1:${#LDFLAGS}-2}"
367 export LDFLAGS
311 return 0 368 return 0
312} 369}
313 370
314etexec-flags() { 371etexec-flags() {
315 has_version sys-devel/hardened-gcc 372 # if you're not using a hardened compiler you wont need this
316 if [ $? == 0 ] ; then 373 # PIC/no-pic kludge in the first place.
317 if [ "`is-flag -yet_exec`" != "true" ]; then 374 has_hardened || return 0
318 debug-print ">>> appending flags -yet_exec" 375
319 append-flags -yet_exec 376 if has_pie || has_pic; then
320 append-ldflags -yet_exec 377 [ -z "`is-flag -fno-pic`" ] &&
378 export CFLAGS="${CFLAGS} `test_flag -fno-pic`"
379 [ -z "`is-flag -nopie`" ] &&
380 export CFLAGS="${CFLAGS} `test_flag -nopie`"
321 fi 381 fi
322 fi 382 return 0
323} 383}
324 384
325fstack-flags() { 385fstack-flags() {
326 has_version sys-devel/hardened-gcc 386 if has_ssp; then
327 if [ $? == 0 ] ; then 387 [ -z "`is-flag -fno-stack-protector`" ] &&
328 if [ "`is-flag -yno_propolice`" != "true" ]; then 388 export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`"
329 debug-print ">>> appending flags -yno_propolice"
330 append-flags -yno_propolice
331 append-ldflags -yno_propolice
332 fi 389 fi
333 fi 390 return 0
334} 391}

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

  ViewVC Help
Powered by ViewVC 1.1.20