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

Legend:
Removed from v.1.40  
changed lines
  Added in v.1.58

  ViewVC Help
Powered by ViewVC 1.1.20