/[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.37 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.37 2004/02/21 07:19:29 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) 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 *) ;;
85 esac 97 esac
86 done 98 done
87 99
96 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" 108 CFLAGS="${CFLAGS:1:${#CFLAGS}-2}"
97 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" 109 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
98 return 0 110 return 0
99} 111}
100 112
113filter-lfs-flags() {
114 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
115}
116
117append-lfs-flags() {
118 append-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
119}
120
101append-flags() { 121append-flags() {
102 export CFLAGS="${CFLAGS} $@" 122 export CFLAGS="${CFLAGS} $*"
103 export CXXFLAGS="${CXXFLAGS} $@" 123 export CXXFLAGS="${CXXFLAGS} $*"
104 [ "`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
105 return 0 126 return 0
106} 127}
107 128
108replace-flags() { 129replace-flags() {
109 # we do this fancy spacing stuff so as to not filter 130 # we do this fancy spacing stuff so as to not filter
116 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" 137 CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}"
117 return 0 138 return 0
118} 139}
119 140
120replace-cpu-flags() { 141replace-cpu-flags() {
121 local newcpu="$1" ; shift 142 local oldcpu newcpu="$1" ; shift
122 local oldcpu=""
123 for oldcpu in "$@" ; do 143 for oldcpu in "$@" ; do
124 replace-flags -march=${oldcpu} -march=${newcpu} 144 replace-flags -march=${oldcpu} -march=${newcpu}
125 replace-flags -mcpu=${oldcpu} -mcpu=${newcpu} 145 replace-flags -mcpu=${oldcpu} -mcpu=${newcpu}
146 replace-flags -mtune=${oldcpu} -mtune=${newcpu}
126 done 147 done
127 return 0 148 return 0
128} 149}
129 150
130is-flag() { 151is-flag() {
152 local x
153
131 for x in ${CFLAGS} ${CXXFLAGS} ; do 154 for x in ${CFLAGS} ${CXXFLAGS} ; do
132 if [ "${x}" == "$1" ] ; then 155 if [ "${x}" == "$1" ] ; then
133 echo true 156 echo true
134 return 0 157 return 0
135 fi 158 fi
136 done 159 done
137 return 1 160 return 1
138} 161}
139 162
140filter-mfpmath() { 163filter-mfpmath() {
164 local orig_mfpmath new_math prune_math
165
141 # save the original -mfpmath flag 166 # save the original -mfpmath flag
142 local orig_mfpmath="`get-flag -mfpmath`" 167 orig_mfpmath="`get-flag -mfpmath`"
143 # get the value of the current -mfpmath flag 168 # get the value of the current -mfpmath flag
144 local new_math=" `get-flag mfpmath | tr , ' '` " 169 new_math=" `get-flag mfpmath | tr , ' '` "
145 # figure out which math values are to be removed 170 # figure out which math values are to be removed
146 local prune_math="" 171 prune_math=""
147 for prune_math in "$@" ; do 172 for prune_math in "$@" ; do
148 new_math="${new_math/ ${prune_math} / }" 173 new_math="${new_math/ ${prune_math} / }"
149 done 174 done
150 new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`" 175 new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`"
151 176
160 fi 185 fi
161 return 0 186 return 0
162} 187}
163 188
164strip-flags() { 189strip-flags() {
190 local x y flag NEW_CFLAGS NEW_CXXFLAGS
191
165 setup-allowed-flags 192 setup-allowed-flags
166 193
167 local NEW_CFLAGS="" 194 local NEW_CFLAGS=""
168 local NEW_CXXFLAGS="" 195 local NEW_CXXFLAGS=""
169 196
170 # Allow unstable C[XX]FLAGS if we are using unstable profile ... 197 # Allow unstable C[XX]FLAGS if we are using unstable profile ...
171 if [ `has ~${ARCH} ${ACCEPT_KEYWORDS}` ] ; then 198 if has ~${ARCH} ${ACCEPT_KEYWORDS} ; then
172 [ `use debug` ] && einfo "Enabling the use of some unstable flags" 199 use debug && einfo "Enabling the use of some unstable flags"
173 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}" 200 ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}"
174 fi 201 fi
175 202
176 set -f 203 set -f # disable pathname expansion
177 204
178 for x in ${CFLAGS} 205 for x in ${CFLAGS}; do
179 do
180 for y in ${ALLOWED_FLAGS} 206 for y in ${ALLOWED_FLAGS}; do
181 do
182 flag=${x%%=*} 207 flag=${x%%=*}
183 if [ "${flag%%${y}}" = "" ] 208 if [ "${flag%%${y}}" = "" ]; then
184 then
185 NEW_CFLAGS="${NEW_CFLAGS} ${x}" 209 NEW_CFLAGS="${NEW_CFLAGS} ${x}"
186 break 210 break
187 fi 211 fi
188 done 212 done
189 done 213 done
190 214
191 for x in ${CXXFLAGS} 215 for x in ${CXXFLAGS}; do
192 do
193 for y in ${ALLOWED_FLAGS} 216 for y in ${ALLOWED_FLAGS}; do
194 do
195 flag=${x%%=*} 217 flag=${x%%=*}
196 if [ "${flag%%${y}}" = "" ] 218 if [ "${flag%%${y}}" = "" ]; then
197 then
198 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}" 219 NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}"
199 break 220 break
200 fi 221 fi
201 done 222 done
202 done 223 done
207 fi 228 fi
208 if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then 229 if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then
209 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2" 230 NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2"
210 fi 231 fi
211 232
212 set +f 233 set +f # re-enable pathname expansion
213 234
214 [ `use debug` ] \ 235 useq debug \
215 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \ 236 && einfo "CFLAGS=\"${NEW_CFLAGS}\"" \
216 && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\"" 237 && einfo "CXXFLAGS=\"${NEW_CXXFLAGS}\""
217 238
218 export CFLAGS="${NEW_CFLAGS}" 239 export CFLAGS="${NEW_CFLAGS}"
219 export CXXFLAGS="${NEW_CXXFLAGS}" 240 export CXXFLAGS="${NEW_CXXFLAGS}"
220 return 0 241 return 0
221} 242}
222 243
223test_flag() { 244test_flag() {
245 local cc=${CC:-gcc} ; cc=${cc%% *}
224 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
225 echo "$@" 247 printf "%s\n" "$*"
226 return 0 248 return 0
227 fi 249 fi
228 return 1 250 return 1
229} 251}
230 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
231strip-unsupported-flags() { 262strip-unsupported-flags() {
263 local NEW_CFLAGS NEW_CXXFLAGS
264
232 for x in ${CFLAGS} ; do 265 for x in ${CFLAGS} ; do
233 NEW_CFLAGS=${NEW_CFLAGS}" ""`test_flag ${x}`" 266 NEW_CFLAGS="${NEW_CFLAGS} `test_flag ${x}`"
234 done 267 done
235 for x in ${CXXFLAGS} ; do 268 for x in ${CXXFLAGS} ; do
236 NEW_CXXFLAGS=${NEW_CXXFLAGS}" ""`test_flag ${x}`" 269 NEW_CXXFLAGS="${NEW_CXXFLAGS} `test_flag ${x}`"
237 done 270 done
238 271
239 export CFLAGS="${NEW_CFLAGS}" 272 export CFLAGS="${NEW_CFLAGS}"
240 export CXXFLAGS="${NEW_CXXFLAGS}" 273 export CXXFLAGS="${NEW_CXXFLAGS}"
241} 274}
242 275
243get-flag() { 276get-flag() {
277 local f findflag="$1"
278
244 # this code looks a little flaky but seems to work for 279 # this code looks a little flaky but seems to work for
245 # everything we want ... 280 # everything we want ...
246 # for example, if CFLAGS="-march=i686": 281 # for example, if CFLAGS="-march=i686":
247 # `get-flags -march` == "-march=i686" 282 # `get-flag -march` == "-march=i686"
248 # `get-flags march` == "i686" 283 # `get-flag march` == "i686"
249 local findflag="$1"
250 for f in ${CFLAGS} ${CXXFLAGS} ; do 284 for f in ${CFLAGS} ${CXXFLAGS} ; do
251 if [ "${f/${findflag}}" != "${f}" ] ; then 285 if [ "${f/${findflag}}" != "${f}" ] ; then
252 echo "${f/-${findflag}=}" 286 printf "%s\n" "${f/-${findflag}=}"
253 return 0 287 return 0
254 fi 288 fi
255 done 289 done
256 return 1 290 return 1
257} 291}
258 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
259replace-sparc64-flags() { 328replace-sparc64-flags() {
260 local SPARC64_CPUS="ultrasparc v9" 329 local SPARC64_CPUS="ultrasparc v9"
261 330
262 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ] 331 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
263 then
264 for x in ${SPARC64_CPUS} 332 for x in ${SPARC64_CPUS}; do
265 do
266 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}" 333 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
267 done 334 done
268 else 335 else
269 for x in ${SPARC64_CPUS} 336 for x in ${SPARC64_CPUS}; do
270 do
271 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" 337 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
272 done 338 done
273 fi 339 fi
274 340
275 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ] 341 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
276 then
277 for x in ${SPARC64_CPUS} 342 for x in ${SPARC64_CPUS}; do
278 do
279 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}" 343 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
280 done 344 done
281 else 345 else
282 for x in ${SPARC64_CPUS} 346 for x in ${SPARC64_CPUS}; do
283 do
284 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" 347 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
285 done 348 done
286 fi 349 fi
287} 350}
288 351
289append-ldflags() { 352append-ldflags() {
290 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}"
291 return 0 367 return 0
292} 368}
293 369
294etexec-flags() { 370etexec-flags() {
295 has_version sys-devel/hardened-gcc 371 # if you're not using a hardened compiler you wont need this
296 if [ $? == 0 ] ; then 372 # PIC/no-pic kludge in the first place.
297 if [ "`is-flag -yet_exec`" != "true" ]; then 373 has_hardened || return 0
298 debug-print ">>> appending flags -yet_exec" 374
299 append-flags -yet_exec 375 if has_pie || has_pic; then
300 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`"
301 fi 380 fi
302 fi 381 return 0
303} 382}
304 383
305fstack-flags() { 384fstack-flags() {
306 has_version sys-devel/hardened-gcc 385 if has_ssp; then
307 if [ $? == 0 ] ; then 386 [ -z "`is-flag -fno-stack-protector`" ] &&
308 if [ "`is-flag -yno_propolice`" != "true" ]; then 387 export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`"
309 debug-print ">>> appending flags -yno_propolice"
310 append-flags -yno_propolice
311 append-ldflags -yno_propolice
312 fi 388 fi
313 fi 389 return 0
314} 390}

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

  ViewVC Help
Powered by ViewVC 1.1.20