| 1 |
# Copyright 1999-2004 Gentoo Foundation |
| 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.62 2004/07/15 04:59:44 lv Exp $ |
| 4 |
# |
| 5 |
# Author Bart Verwilst <verwilst@gentoo.org> |
| 6 |
|
| 7 |
ECLASS=flag-o-matic |
| 8 |
INHERITED="$INHERITED $ECLASS" |
| 9 |
|
| 10 |
IUSE="debug" |
| 11 |
|
| 12 |
# |
| 13 |
#### filter-flags <flags> #### |
| 14 |
# Remove particular flags from C[XX]FLAGS |
| 15 |
# Matches only complete flags |
| 16 |
# |
| 17 |
#### append-flags <flags> #### |
| 18 |
# Add extra flags to your current C[XX]FLAGS |
| 19 |
# |
| 20 |
#### replace-flags <orig.flag> <new.flag> ### |
| 21 |
# Replace a flag by another one |
| 22 |
# |
| 23 |
#### replace-cpu-flags <new.cpu> <old.cpus> ### |
| 24 |
# Replace march/mcpu flags that specify <old.cpus> |
| 25 |
# with flags that specify <new.cpu> |
| 26 |
# |
| 27 |
#### is-flag <flag> #### |
| 28 |
# Returns "true" if flag is set in C[XX]FLAGS |
| 29 |
# Matches only complete a flag |
| 30 |
# |
| 31 |
#### strip-flags #### |
| 32 |
# Strip C[XX]FLAGS of everything except known |
| 33 |
# good options. |
| 34 |
# |
| 35 |
#### strip-unsupported-flags #### |
| 36 |
# Strip C[XX]FLAGS of any flags not supported by |
| 37 |
# installed version of gcc |
| 38 |
# |
| 39 |
#### get-flag <flag> #### |
| 40 |
# Find and echo the value for a particular flag |
| 41 |
# |
| 42 |
#### replace-sparc64-flags #### |
| 43 |
# Sets mcpu to v8 and uses the original value |
| 44 |
# as mtune if none specified. |
| 45 |
# |
| 46 |
#### filter-mfpmath <math types> #### |
| 47 |
# Remove specified math types from the fpmath specification |
| 48 |
# If the user has -mfpmath=sse,386, running `filter-mfpmath sse` |
| 49 |
# will leave the user with -mfpmath=386 |
| 50 |
# |
| 51 |
#### append-ldflags #### |
| 52 |
# Add extra flags to your current LDFLAGS |
| 53 |
# |
| 54 |
#### filter-ldflags <flags> #### |
| 55 |
# Remove particular flags from LDFLAGS |
| 56 |
# Matches only complete flags |
| 57 |
# |
| 58 |
#### etexec-flags #### |
| 59 |
# hooked function for hardened gcc that appends |
| 60 |
# -fno-pic to {C,CXX,LD}FLAGS |
| 61 |
# when a package is filtering -fpic, -fPIC, -fpie, -fPIE |
| 62 |
# |
| 63 |
#### fstack-flags #### |
| 64 |
# hooked function for hardened gcc that appends |
| 65 |
# -fno-stack-protector to {C,CXX,LD}FLAGS |
| 66 |
# when a package is filtering -fstack-protector, -fstack-protector-all |
| 67 |
# notice: modern automatic specs files will also suppress -fstack-protector-all |
| 68 |
# when only -fno-stack-protector is given |
| 69 |
# |
| 70 |
|
| 71 |
# C[XX]FLAGS that we allow in strip-flags |
| 72 |
setup-allowed-flags() { |
| 73 |
if [ -z "${ALLOWED_FLAGS}" ] ; then |
| 74 |
export ALLOWED_FLAGS="-O -O1 -O2 -mcpu -march -mtune -fstack-protector -fno-unit-at-a-time -pipe -g" |
| 75 |
case "${ARCH}" in |
| 76 |
mips) ALLOWED_FLAGS="${ALLOWED_FLAGS} -mips1 -mips2 -mips3 -mips4 -mabi" ;; |
| 77 |
amd64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC -m64" ;; |
| 78 |
x86) ALLOWED_FLAGS="${ALLOWED_FLAGS} -m32" ;; |
| 79 |
alpha) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
| 80 |
ia64) ALLOWED_FLAGS="${ALLOWED_FLAGS} -fPIC" ;; |
| 81 |
esac |
| 82 |
fi |
| 83 |
|
| 84 |
# C[XX]FLAGS that we are think is ok, but needs testing |
| 85 |
# NOTE: currently -Os have issues with gcc3 and K6* arch's |
| 86 |
export UNSTABLE_FLAGS="-Os -O3 -freorder-blocks -fprefetch-loop-arrays" |
| 87 |
return 0 |
| 88 |
} |
| 89 |
|
| 90 |
filter-flags() { |
| 91 |
local x f fset |
| 92 |
declare -a new_CFLAGS new_CXXFLAGS |
| 93 |
|
| 94 |
for x in "$@" ; do |
| 95 |
case "${x}" in |
| 96 |
-fPIC|-fpic|-fPIE|-fpie|-pie) etexec-flags;; |
| 97 |
-fstack-protector|-fstack-protector-all) fstack-flags;; |
| 98 |
esac |
| 99 |
done |
| 100 |
|
| 101 |
for fset in CFLAGS CXXFLAGS; do |
| 102 |
for f in ${!fset}; do |
| 103 |
for x in "$@"; do |
| 104 |
# Note this should work with globs like -O* |
| 105 |
[[ ${f} == ${x} ]] && continue 2 |
| 106 |
done |
| 107 |
eval new_${fset}\[\${\#new_${fset}\[@]}]=\${f} |
| 108 |
done |
| 109 |
eval export ${fset}=\${new_${fset}\[*]} |
| 110 |
done |
| 111 |
|
| 112 |
return 0 |
| 113 |
} |
| 114 |
|
| 115 |
filter-lfs-flags() { |
| 116 |
filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE |
| 117 |
} |
| 118 |
|
| 119 |
append-lfs-flags() { |
| 120 |
append-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE |
| 121 |
} |
| 122 |
|
| 123 |
append-flags() { |
| 124 |
export CFLAGS="${CFLAGS} $*" |
| 125 |
export CXXFLAGS="${CXXFLAGS} $*" |
| 126 |
[ -n "`is-flag -fno-stack-protector`" -o \ |
| 127 |
-n "`is-flag -fno-stack-protector-all`" ] && fstack-flags |
| 128 |
return 0 |
| 129 |
} |
| 130 |
|
| 131 |
replace-flags() { |
| 132 |
# we do this fancy spacing stuff so as to not filter |
| 133 |
# out part of a flag ... we want flag atoms ! :D |
| 134 |
CFLAGS=" ${CFLAGS} " |
| 135 |
CXXFLAGS=" ${CXXFLAGS} " |
| 136 |
CFLAGS="${CFLAGS// ${1} / ${2} }" |
| 137 |
CXXFLAGS="${CXXFLAGS// ${1} / ${2} }" |
| 138 |
CFLAGS="${CFLAGS:1:${#CFLAGS}-2}" |
| 139 |
CXXFLAGS="${CXXFLAGS:1:${#CXXFLAGS}-2}" |
| 140 |
export CFLAGS CXXFLAGS |
| 141 |
return 0 |
| 142 |
} |
| 143 |
|
| 144 |
replace-cpu-flags() { |
| 145 |
local oldcpu newcpu="$1" ; shift |
| 146 |
for oldcpu in "$@" ; do |
| 147 |
replace-flags -march=${oldcpu} -march=${newcpu} |
| 148 |
replace-flags -mcpu=${oldcpu} -mcpu=${newcpu} |
| 149 |
replace-flags -mtune=${oldcpu} -mtune=${newcpu} |
| 150 |
done |
| 151 |
return 0 |
| 152 |
} |
| 153 |
|
| 154 |
is-flag() { |
| 155 |
local x |
| 156 |
|
| 157 |
for x in ${CFLAGS} ${CXXFLAGS} ; do |
| 158 |
if [ "${x}" == "$1" ] ; then |
| 159 |
echo true |
| 160 |
return 0 |
| 161 |
fi |
| 162 |
done |
| 163 |
return 1 |
| 164 |
} |
| 165 |
|
| 166 |
filter-mfpmath() { |
| 167 |
local orig_mfpmath new_math prune_math |
| 168 |
|
| 169 |
# save the original -mfpmath flag |
| 170 |
orig_mfpmath="`get-flag -mfpmath`" |
| 171 |
# get the value of the current -mfpmath flag |
| 172 |
new_math=" `get-flag mfpmath | tr , ' '` " |
| 173 |
# figure out which math values are to be removed |
| 174 |
prune_math="" |
| 175 |
for prune_math in "$@" ; do |
| 176 |
new_math="${new_math/ ${prune_math} / }" |
| 177 |
done |
| 178 |
new_math="`echo ${new_math:1:${#new_math}-2} | tr ' ' ,`" |
| 179 |
|
| 180 |
if [ -z "${new_math}" ] ; then |
| 181 |
# if we're removing all user specified math values are |
| 182 |
# slated for removal, then we just filter the flag |
| 183 |
filter-flags ${orig_mfpmath} |
| 184 |
else |
| 185 |
# if we only want to filter some of the user specified |
| 186 |
# math values, then we replace the current flag |
| 187 |
replace-flags ${orig_mfpmath} -mfpmath=${new_math} |
| 188 |
fi |
| 189 |
return 0 |
| 190 |
} |
| 191 |
|
| 192 |
strip-flags() { |
| 193 |
local x y flag NEW_CFLAGS NEW_CXXFLAGS |
| 194 |
|
| 195 |
setup-allowed-flags |
| 196 |
|
| 197 |
local NEW_CFLAGS="" |
| 198 |
local NEW_CXXFLAGS="" |
| 199 |
|
| 200 |
# Allow unstable C[XX]FLAGS if we are using unstable profile ... |
| 201 |
if has ~${ARCH} ${ACCEPT_KEYWORDS} ; then |
| 202 |
use debug && einfo "Enabling the use of some unstable flags" |
| 203 |
ALLOWED_FLAGS="${ALLOWED_FLAGS} ${UNSTABLE_FLAGS}" |
| 204 |
fi |
| 205 |
|
| 206 |
set -f # disable pathname expansion |
| 207 |
|
| 208 |
for x in ${CFLAGS}; do |
| 209 |
for y in ${ALLOWED_FLAGS}; do |
| 210 |
flag=${x%%=*} |
| 211 |
if [ "${flag%%${y}}" = "" ] || [ "${flag:0:5}" = "-fno-" ] || [ "${flag:0:5}" = "-mno-" ] ; then |
| 212 |
NEW_CFLAGS="${NEW_CFLAGS} ${x}" |
| 213 |
break |
| 214 |
fi |
| 215 |
done |
| 216 |
done |
| 217 |
|
| 218 |
for x in ${CXXFLAGS}; do |
| 219 |
for y in ${ALLOWED_FLAGS}; do |
| 220 |
flag=${x%%=*} |
| 221 |
if [ "${flag%%${y}}" = "" ] || [ "${flag:0:5}" = "-fno-" ] || [ "${flag:0:5}" = "-mno-" ] ; then |
| 222 |
NEW_CXXFLAGS="${NEW_CXXFLAGS} ${x}" |
| 223 |
break |
| 224 |
fi |
| 225 |
done |
| 226 |
done |
| 227 |
|
| 228 |
# In case we filtered out all optimization flags fallback to -O2 |
| 229 |
if [ "${CFLAGS/-O}" != "${CFLAGS}" -a "${NEW_CFLAGS/-O}" = "${NEW_CFLAGS}" ]; then |
| 230 |
NEW_CFLAGS="${NEW_CFLAGS} -O2" |
| 231 |
fi |
| 232 |
if [ "${CXXFLAGS/-O}" != "${CXXFLAGS}" -a "${NEW_CXXFLAGS/-O}" = "${NEW_CXXFLAGS}" ]; then |
| 233 |
NEW_CXXFLAGS="${NEW_CXXFLAGS} -O2" |
| 234 |
fi |
| 235 |
|
| 236 |
set +f # re-enable pathname expansion |
| 237 |
|
| 238 |
export CFLAGS="${NEW_CFLAGS}" |
| 239 |
export CXXFLAGS="${NEW_CXXFLAGS}" |
| 240 |
return 0 |
| 241 |
} |
| 242 |
|
| 243 |
test_flag() { |
| 244 |
local cc=${CC:-gcc} ; cc=${cc%% *} |
| 245 |
if ${cc} -S -xc "$@" -o /dev/null /dev/null &>/dev/null; then |
| 246 |
printf "%s\n" "$*" |
| 247 |
return 0 |
| 248 |
fi |
| 249 |
return 1 |
| 250 |
} |
| 251 |
|
| 252 |
test_version_info() { |
| 253 |
local cc=${CC:-gcc} ; cc=${cc%% *} |
| 254 |
if [[ $(${cc} --version 2>&1) == *$1* ]]; then |
| 255 |
return 0 |
| 256 |
else |
| 257 |
return 1 |
| 258 |
fi |
| 259 |
} |
| 260 |
|
| 261 |
strip-unsupported-flags() { |
| 262 |
local NEW_CFLAGS NEW_CXXFLAGS |
| 263 |
|
| 264 |
for x in ${CFLAGS} ; do |
| 265 |
NEW_CFLAGS="${NEW_CFLAGS} `test_flag ${x}`" |
| 266 |
done |
| 267 |
for x in ${CXXFLAGS} ; do |
| 268 |
NEW_CXXFLAGS="${NEW_CXXFLAGS} `test_flag ${x}`" |
| 269 |
done |
| 270 |
|
| 271 |
export CFLAGS="${NEW_CFLAGS}" |
| 272 |
export CXXFLAGS="${NEW_CXXFLAGS}" |
| 273 |
} |
| 274 |
|
| 275 |
get-flag() { |
| 276 |
local f findflag="$1" |
| 277 |
|
| 278 |
# this code looks a little flaky but seems to work for |
| 279 |
# everything we want ... |
| 280 |
# for example, if CFLAGS="-march=i686": |
| 281 |
# `get-flag -march` == "-march=i686" |
| 282 |
# `get-flag march` == "i686" |
| 283 |
for f in ${CFLAGS} ${CXXFLAGS} ; do |
| 284 |
if [ "${f/${findflag}}" != "${f}" ] ; then |
| 285 |
printf "%s\n" "${f/-${findflag}=}" |
| 286 |
return 0 |
| 287 |
fi |
| 288 |
done |
| 289 |
return 1 |
| 290 |
} |
| 291 |
|
| 292 |
has_hardened() { |
| 293 |
test_version_info Hardened |
| 294 |
return $? |
| 295 |
} |
| 296 |
|
| 297 |
has_pic() { |
| 298 |
[ "${CFLAGS/-fPIC}" != "${CFLAGS}" ] && return 0 |
| 299 |
[ "${CFLAGS/-fpic}" != "${CFLAGS}" ] && return 0 |
| 300 |
test_version_info pie && return 0 |
| 301 |
return 1 |
| 302 |
} |
| 303 |
|
| 304 |
has_pie() { |
| 305 |
[ "${CFLAGS/-fPIE}" != "${CFLAGS}" ] && return 0 |
| 306 |
[ "${CFLAGS/-fpie}" != "${CFLAGS}" ] && return 0 |
| 307 |
test_version_info pie && return 0 |
| 308 |
return 1 |
| 309 |
} |
| 310 |
|
| 311 |
has_ssp() { |
| 312 |
[ "${CFLAGS/-fstack-protector}" != "${CFLAGS}" ] && return 0 |
| 313 |
test_version_info ssp && return 0 |
| 314 |
return 1 |
| 315 |
} |
| 316 |
|
| 317 |
has_m64() { |
| 318 |
# this doesnt test if the flag is accepted, it tests if the flag |
| 319 |
# actually -WORKS-. non-multilib gcc will take both -m32 and -m64! |
| 320 |
# please dont replace this function with test_flag in some future |
| 321 |
# clean-up! |
| 322 |
temp=`mktemp` |
| 323 |
echo "int main() { return(0); }" > ${temp}.c |
| 324 |
${CC/ .*/} -m64 -o /dev/null ${temp}.c > /dev/null 2>&1 |
| 325 |
ret=$? |
| 326 |
rm -f ${temp}.c |
| 327 |
[ "$ret" != "1" ] && return 0 |
| 328 |
return 1 |
| 329 |
} |
| 330 |
|
| 331 |
has_m32() { |
| 332 |
# this doesnt test if the flag is accepted, it tests if the flag |
| 333 |
# actually -WORKS-. non-multilib gcc will take both -m32 and -m64! |
| 334 |
# please dont replace this function with test_flag in some future |
| 335 |
# clean-up! |
| 336 |
temp=`mktemp` |
| 337 |
echo "int main() { return(0); }" > ${temp}.c |
| 338 |
${CC/ .*/} -m32 -o /dev/null ${temp}.c > /dev/null 2>&1 |
| 339 |
ret=$? |
| 340 |
rm -f ${temp}.c |
| 341 |
[ "$ret" != "1" ] && return 0 |
| 342 |
return 1 |
| 343 |
} |
| 344 |
|
| 345 |
replace-sparc64-flags() { |
| 346 |
local SPARC64_CPUS="ultrasparc v9" |
| 347 |
|
| 348 |
if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then |
| 349 |
for x in ${SPARC64_CPUS}; do |
| 350 |
CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}" |
| 351 |
done |
| 352 |
else |
| 353 |
for x in ${SPARC64_CPUS}; do |
| 354 |
CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" |
| 355 |
done |
| 356 |
fi |
| 357 |
|
| 358 |
if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then |
| 359 |
for x in ${SPARC64_CPUS}; do |
| 360 |
CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}" |
| 361 |
done |
| 362 |
else |
| 363 |
for x in ${SPARC64_CPUS}; do |
| 364 |
CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}" |
| 365 |
done |
| 366 |
fi |
| 367 |
|
| 368 |
export CFLAGS CXXFLAGS |
| 369 |
} |
| 370 |
|
| 371 |
append-ldflags() { |
| 372 |
export LDFLAGS="${LDFLAGS} $*" |
| 373 |
return 0 |
| 374 |
} |
| 375 |
|
| 376 |
filter-ldflags() { |
| 377 |
local x |
| 378 |
|
| 379 |
# we do this fancy spacing stuff so as to not filter |
| 380 |
# out part of a flag ... we want flag atoms ! :D |
| 381 |
LDFLAGS=" ${LDFLAGS} " |
| 382 |
for x in "$@" ; do |
| 383 |
LDFLAGS="${LDFLAGS// ${x} / }" |
| 384 |
done |
| 385 |
LDFLAGS="${LDFLAGS:1:${#LDFLAGS}-2}" |
| 386 |
export LDFLAGS |
| 387 |
return 0 |
| 388 |
} |
| 389 |
|
| 390 |
etexec-flags() { |
| 391 |
# if you're not using a hardened compiler you wont need this |
| 392 |
# PIC/no-pic kludge in the first place. |
| 393 |
has_hardened || return 0 |
| 394 |
|
| 395 |
if has_pie || has_pic; then |
| 396 |
[ -z "`is-flag -fno-pic`" ] && |
| 397 |
export CFLAGS="${CFLAGS} `test_flag -fno-pic`" |
| 398 |
[ -z "`is-flag -nopie`" ] && |
| 399 |
export CFLAGS="${CFLAGS} `test_flag -nopie`" |
| 400 |
fi |
| 401 |
return 0 |
| 402 |
} |
| 403 |
|
| 404 |
fstack-flags() { |
| 405 |
if has_ssp; then |
| 406 |
[ -z "`is-flag -fno-stack-protector`" ] && |
| 407 |
export CFLAGS="${CFLAGS} `test_flag -fno-stack-protector`" |
| 408 |
fi |
| 409 |
return 0 |
| 410 |
} |