flag-o-matic.eclass 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. # Copyright 1999-2017 Gentoo Foundation
  2. # Distributed under the terms of the GNU General Public License v2
  3. # @ECLASS: flag-o-matic.eclass
  4. # @MAINTAINER:
  5. # toolchain@gentoo.org
  6. # @BLURB: common functions to manipulate and query toolchain flags
  7. # @DESCRIPTION:
  8. # This eclass contains a suite of functions to help developers sanely
  9. # and safely manage toolchain flags in their builds.
  10. if [[ -z ${_FLAG_O_MATIC_ECLASS} ]]; then
  11. _FLAG_O_MATIC_ECLASS=1
  12. inherit eutils toolchain-funcs multilib
  13. # Return all the flag variables that our high level funcs operate on.
  14. all-flag-vars() {
  15. echo {C,CPP,CXX,CCAS,F,FC,LD}FLAGS
  16. }
  17. # {C,CPP,CXX,CCAS,F,FC,LD}FLAGS that we allow in strip-flags
  18. # Note: shell globs and character lists are allowed
  19. setup-allowed-flags() {
  20. ALLOWED_FLAGS=(
  21. -pipe -O '-O[12sg]' -mcpu -march -mtune
  22. '-fstack-protector*' '-fsanitize*' '-fstack-check*' -fno-stack-check
  23. -fbounds-check -fbounds-checking -fno-strict-overflow
  24. -fno-PIE -fno-pie -nopie -no-pie -fno-unit-at-a-time
  25. -g '-g[0-9]' -ggdb '-ggdb[0-9]' '-gdwarf-*' gstabs -gstabs+
  26. -fno-ident -fpermissive -frecord-gcc-switches
  27. '-fdiagnostics*' '-fplugin*'
  28. '-W*' -w
  29. # CPPFLAGS and LDFLAGS
  30. '-[DUILR]*' '-Wl,*'
  31. )
  32. # allow a bunch of flags that negate features / control ABI
  33. ALLOWED_FLAGS+=(
  34. '-fno-stack-protector*' '-fabi-version=*'
  35. -fno-strict-aliasing -fno-bounds-check -fno-bounds-checking -fstrict-overflow
  36. -fno-omit-frame-pointer '-fno-builtin*'
  37. )
  38. ALLOWED_FLAGS+=(
  39. -mregparm -mno-app-regs -mapp-regs -mno-mmx -mno-sse
  40. -mno-sse2 -mno-sse3 -mno-ssse3 -mno-sse4 -mno-sse4.1 -mno-sse4.2
  41. -mno-avx -mno-aes -mno-pclmul -mno-sse4a -mno-3dnow -mno-popcnt
  42. -mno-abm -mips1 -mips2 -mips3 -mips4 -mips32 -mips64 -mips16 -mplt
  43. -msoft-float -mno-soft-float -mhard-float -mno-hard-float -mfpu
  44. -mieee -mieee-with-inexact -mschedule -mfloat-gprs -mspe -mno-spe
  45. -mtls-direct-seg-refs -mno-tls-direct-seg-refs -mflat -mno-flat
  46. -mno-faster-structs -mfaster-structs -m32 -m64 -mx32 -mabi
  47. -mlittle-endian -mbig-endian -EL -EB -fPIC -mlive-g0 -mcmodel
  48. -mstack-bias -mno-stack-bias -msecure-plt '-m*-toc' -mfloat-abi
  49. -mfix-r10000 -mno-fix-r10000 -mthumb -marm
  50. # gcc 4.5
  51. -mno-fma4 -mno-movbe -mno-xop -mno-lwp
  52. # gcc 4.6
  53. -mno-fsgsbase -mno-rdrnd -mno-f16c -mno-bmi -mno-tbm
  54. # gcc 4.7
  55. -mno-avx2 -mno-bmi2 -mno-fma -mno-lzcnt
  56. # gcc 4.8
  57. -mno-fxsr -mno-hle -mno-rtm -mno-xsave -mno-xsaveopt
  58. # gcc 4.9
  59. -mno-avx512cd -mno-avx512er -mno-avx512f -mno-avx512pf -mno-sha
  60. )
  61. }
  62. # inverted filters for hardened compiler. This is trying to unpick
  63. # the hardened compiler defaults.
  64. _filter-hardened() {
  65. local f
  66. for f in "$@" ; do
  67. case "${f}" in
  68. # Ideally we should only concern ourselves with PIE flags,
  69. # not -fPIC or -fpic, but too many places filter -fPIC without
  70. # thinking about -fPIE.
  71. -fPIC|-fpic|-fPIE|-fpie|-Wl,pie|-pie)
  72. gcc-specs-pie || continue
  73. if ! is-flagq -nopie && ! is-flagq -no-pie ; then
  74. # Support older Gentoo form first (-nopie) before falling
  75. # back to the official gcc-6+ form (-no-pie).
  76. if test-flags -nopie >/dev/null ; then
  77. append-flags -nopie
  78. else
  79. append-flags -no-pie
  80. fi
  81. fi
  82. ;;
  83. -fstack-protector)
  84. gcc-specs-ssp || continue
  85. is-flagq -fno-stack-protector || append-flags $(test-flags -fno-stack-protector);;
  86. -fstack-protector-all)
  87. gcc-specs-ssp-to-all || continue
  88. is-flagq -fno-stack-protector-all || append-flags $(test-flags -fno-stack-protector-all);;
  89. -fno-strict-overflow)
  90. gcc-specs-nostrict || continue
  91. is-flagq -fstrict-overflow || append-flags $(test-flags -fstrict-overflow);;
  92. esac
  93. done
  94. }
  95. # Remove occurrences of strings from variable given in $1
  96. # Strings removed are matched as globs, so for example
  97. # '-O*' would remove -O1, -O2 etc.
  98. _filter-var() {
  99. local f x var=$1 new=()
  100. shift
  101. for f in ${!var} ; do
  102. for x in "$@" ; do
  103. # Note this should work with globs like -O*
  104. [[ ${f} == ${x} ]] && continue 2
  105. done
  106. new+=( "${f}" )
  107. done
  108. export ${var}="${new[*]}"
  109. }
  110. # @FUNCTION: filter-flags
  111. # @USAGE: <flags>
  112. # @DESCRIPTION:
  113. # Remove particular <flags> from {C,CPP,CXX,CCAS,F,FC,LD}FLAGS. Accepts shell globs.
  114. filter-flags() {
  115. _filter-hardened "$@"
  116. local v
  117. for v in $(all-flag-vars) ; do
  118. _filter-var ${v} "$@"
  119. done
  120. return 0
  121. }
  122. # @FUNCTION: filter-lfs-flags
  123. # @DESCRIPTION:
  124. # Remove flags that enable Large File Support.
  125. filter-lfs-flags() {
  126. [[ $# -ne 0 ]] && die "filter-lfs-flags takes no arguments"
  127. # http://www.gnu.org/s/libc/manual/html_node/Feature-Test-Macros.html
  128. # _LARGEFILE_SOURCE: enable support for new LFS funcs (ftello/etc...)
  129. # _LARGEFILE64_SOURCE: enable support for 64bit variants (off64_t/fseeko64/etc...)
  130. # _FILE_OFFSET_BITS: default to 64bit variants (off_t is defined as off64_t)
  131. filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
  132. }
  133. # @FUNCTION: filter-ldflags
  134. # @USAGE: <flags>
  135. # @DESCRIPTION:
  136. # Remove particular <flags> from LDFLAGS. Accepts shell globs.
  137. filter-ldflags() {
  138. _filter-var LDFLAGS "$@"
  139. return 0
  140. }
  141. # @FUNCTION: append-cppflags
  142. # @USAGE: <flags>
  143. # @DESCRIPTION:
  144. # Add extra <flags> to the current CPPFLAGS.
  145. append-cppflags() {
  146. [[ $# -eq 0 ]] && return 0
  147. export CPPFLAGS+=" $*"
  148. return 0
  149. }
  150. # @FUNCTION: append-cflags
  151. # @USAGE: <flags>
  152. # @DESCRIPTION:
  153. # Add extra <flags> to the current CFLAGS. If a flag might not be supported
  154. # with different compilers (or versions), then use test-flags-CC like so:
  155. # @CODE
  156. # append-cflags $(test-flags-CC -funky-flag)
  157. # @CODE
  158. append-cflags() {
  159. [[ $# -eq 0 ]] && return 0
  160. # Do not do automatic flag testing ourselves. #417047
  161. export CFLAGS+=" $*"
  162. return 0
  163. }
  164. # @FUNCTION: append-cxxflags
  165. # @USAGE: <flags>
  166. # @DESCRIPTION:
  167. # Add extra <flags> to the current CXXFLAGS. If a flag might not be supported
  168. # with different compilers (or versions), then use test-flags-CXX like so:
  169. # @CODE
  170. # append-cxxflags $(test-flags-CXX -funky-flag)
  171. # @CODE
  172. append-cxxflags() {
  173. [[ $# -eq 0 ]] && return 0
  174. # Do not do automatic flag testing ourselves. #417047
  175. export CXXFLAGS+=" $*"
  176. return 0
  177. }
  178. # @FUNCTION: append-fflags
  179. # @USAGE: <flags>
  180. # @DESCRIPTION:
  181. # Add extra <flags> to the current {F,FC}FLAGS. If a flag might not be supported
  182. # with different compilers (or versions), then use test-flags-F77 like so:
  183. # @CODE
  184. # append-fflags $(test-flags-F77 -funky-flag)
  185. # @CODE
  186. append-fflags() {
  187. [[ $# -eq 0 ]] && return 0
  188. # Do not do automatic flag testing ourselves. #417047
  189. export FFLAGS+=" $*"
  190. export FCFLAGS+=" $*"
  191. return 0
  192. }
  193. # @FUNCTION: append-lfs-flags
  194. # @DESCRIPTION:
  195. # Add flags that enable Large File Support.
  196. append-lfs-flags() {
  197. [[ $# -ne 0 ]] && die "append-lfs-flags takes no arguments"
  198. # see comments in filter-lfs-flags func for meaning of these
  199. append-cppflags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
  200. }
  201. # @FUNCTION: append-ldflags
  202. # @USAGE: <flags>
  203. # @DESCRIPTION:
  204. # Add extra <flags> to the current LDFLAGS.
  205. append-ldflags() {
  206. [[ $# -eq 0 ]] && return 0
  207. local flag
  208. for flag in "$@"; do
  209. [[ ${flag} == -l* ]] && \
  210. eqawarn "Appending a library link instruction (${flag}); libraries to link to should not be passed through LDFLAGS"
  211. done
  212. export LDFLAGS="${LDFLAGS} $*"
  213. return 0
  214. }
  215. # @FUNCTION: append-flags
  216. # @USAGE: <flags>
  217. # @DESCRIPTION:
  218. # Add extra <flags> to your current {C,CXX,F,FC}FLAGS.
  219. append-flags() {
  220. [[ $# -eq 0 ]] && return 0
  221. case " $* " in
  222. *' '-[DIU]*) eqawarn 'please use append-cppflags for preprocessor flags' ;;
  223. *' '-L*|\
  224. *' '-Wl,*) eqawarn 'please use append-ldflags for linker flags' ;;
  225. esac
  226. append-cflags "$@"
  227. append-cxxflags "$@"
  228. append-fflags "$@"
  229. return 0
  230. }
  231. # @FUNCTION: replace-flags
  232. # @USAGE: <old> <new>
  233. # @DESCRIPTION:
  234. # Replace the <old> flag with <new>. Accepts shell globs for <old>.
  235. replace-flags() {
  236. [[ $# != 2 ]] && die "Usage: replace-flags <old flag> <new flag>"
  237. local f var new
  238. for var in $(all-flag-vars) ; do
  239. # Looping over the flags instead of using a global
  240. # substitution ensures that we're working with flag atoms.
  241. # Otherwise globs like -O* have the potential to wipe out the
  242. # list of flags.
  243. new=()
  244. for f in ${!var} ; do
  245. # Note this should work with globs like -O*
  246. [[ ${f} == ${1} ]] && f=${2}
  247. new+=( "${f}" )
  248. done
  249. export ${var}="${new[*]}"
  250. done
  251. return 0
  252. }
  253. # @FUNCTION: replace-cpu-flags
  254. # @USAGE: <old> <new>
  255. # @DESCRIPTION:
  256. # Replace cpu flags (like -march/-mcpu/-mtune) that select the <old> cpu
  257. # with flags that select the <new> cpu. Accepts shell globs for <old>.
  258. replace-cpu-flags() {
  259. local newcpu="$#" ; newcpu="${!newcpu}"
  260. while [ $# -gt 1 ] ; do
  261. # quote to make sure that no globbing is done (particularly on
  262. # ${oldcpu}) prior to calling replace-flags
  263. replace-flags "-march=${1}" "-march=${newcpu}"
  264. replace-flags "-mcpu=${1}" "-mcpu=${newcpu}"
  265. replace-flags "-mtune=${1}" "-mtune=${newcpu}"
  266. shift
  267. done
  268. return 0
  269. }
  270. _is_flagq() {
  271. local x var="$1[*]"
  272. for x in ${!var} ; do
  273. [[ ${x} == $2 ]] && return 0
  274. done
  275. return 1
  276. }
  277. # @FUNCTION: is-flagq
  278. # @USAGE: <flag>
  279. # @DESCRIPTION:
  280. # Returns shell true if <flag> is in {C,CXX,F,FC}FLAGS, else returns shell false. Accepts shell globs.
  281. is-flagq() {
  282. [[ -n $2 ]] && die "Usage: is-flag <flag>"
  283. local var
  284. for var in $(all-flag-vars) ; do
  285. _is_flagq ${var} "$1" && return 0
  286. done
  287. return 1
  288. }
  289. # @FUNCTION: is-flag
  290. # @USAGE: <flag>
  291. # @DESCRIPTION:
  292. # Echo's "true" if flag is set in {C,CXX,F,FC}FLAGS. Accepts shell globs.
  293. is-flag() {
  294. is-flagq "$@" && echo true
  295. }
  296. # @FUNCTION: is-ldflagq
  297. # @USAGE: <flag>
  298. # @DESCRIPTION:
  299. # Returns shell true if <flag> is in LDFLAGS, else returns shell false. Accepts shell globs.
  300. is-ldflagq() {
  301. [[ -n $2 ]] && die "Usage: is-ldflag <flag>"
  302. _is_flagq LDFLAGS $1
  303. }
  304. # @FUNCTION: is-ldflag
  305. # @USAGE: <flag>
  306. # @DESCRIPTION:
  307. # Echo's "true" if flag is set in LDFLAGS. Accepts shell globs.
  308. is-ldflag() {
  309. is-ldflagq "$@" && echo true
  310. }
  311. # @FUNCTION: filter-mfpmath
  312. # @USAGE: <math types>
  313. # @DESCRIPTION:
  314. # Remove specified math types from the fpmath flag. For example, if the user
  315. # has -mfpmath=sse,386, running `filter-mfpmath sse` will leave the user with
  316. # -mfpmath=386.
  317. filter-mfpmath() {
  318. local orig_mfpmath new_math prune_math
  319. # save the original -mfpmath flag
  320. orig_mfpmath=$(get-flag -mfpmath)
  321. # get the value of the current -mfpmath flag
  322. new_math=$(get-flag mfpmath)
  323. # convert "both" to something we can filter
  324. new_math=${new_math/both/387,sse}
  325. new_math=" ${new_math//[,+]/ } "
  326. # figure out which math values are to be removed
  327. prune_math=""
  328. for prune_math in "$@" ; do
  329. new_math=${new_math/ ${prune_math} / }
  330. done
  331. new_math=$(echo ${new_math})
  332. new_math=${new_math// /,}
  333. if [[ -z ${new_math} ]] ; then
  334. # if we're removing all user specified math values are
  335. # slated for removal, then we just filter the flag
  336. filter-flags ${orig_mfpmath}
  337. else
  338. # if we only want to filter some of the user specified
  339. # math values, then we replace the current flag
  340. replace-flags ${orig_mfpmath} -mfpmath=${new_math}
  341. fi
  342. return 0
  343. }
  344. # @FUNCTION: strip-flags
  345. # @DESCRIPTION:
  346. # Strip *FLAGS of everything except known good/safe flags. This runs over all
  347. # flags returned by all_flag_vars().
  348. strip-flags() {
  349. local x y var
  350. local ALLOWED_FLAGS
  351. setup-allowed-flags
  352. set -f # disable pathname expansion
  353. for var in $(all-flag-vars) ; do
  354. local new=()
  355. for x in ${!var} ; do
  356. local flag=${x%%=*}
  357. for y in "${ALLOWED_FLAGS[@]}" ; do
  358. if [[ -z ${flag%%${y}} ]] ; then
  359. new+=( "${x}" )
  360. break
  361. fi
  362. done
  363. done
  364. # In case we filtered out all optimization flags fallback to -O2
  365. if _is_flagq ${var} "-O*" && ! _is_flagq new "-O*" ; then
  366. new+=( -O2 )
  367. fi
  368. if [[ ${!var} != "${new[*]}" ]] ; then
  369. einfo "strip-flags: ${var}: changed '${!var}' to '${new[*]}'"
  370. fi
  371. export ${var}="${new[*]}"
  372. done
  373. set +f # re-enable pathname expansion
  374. return 0
  375. }
  376. test-flag-PROG() {
  377. local comp=$1
  378. local lang=$2
  379. local flag=$3
  380. [[ -z ${comp} || -z ${flag} ]] && return 1
  381. local cmdline=(
  382. $(tc-get${comp})
  383. # Clang will warn about unknown gcc flags but exit 0.
  384. # Need -Werror to force it to exit non-zero.
  385. -Werror
  386. # Use -c so we can test the assembler as well.
  387. -c -o /dev/null
  388. )
  389. if "${cmdline[@]}" -x${lang} - </dev/null >/dev/null 2>&1 ; then
  390. "${cmdline[@]}" "${flag}" -x${lang} - </dev/null >/dev/null 2>&1
  391. else
  392. "${cmdline[@]}" "${flag}" -c -o /dev/null /dev/null >/dev/null 2>&1
  393. fi
  394. }
  395. # @FUNCTION: test-flag-CC
  396. # @USAGE: <flag>
  397. # @DESCRIPTION:
  398. # Returns shell true if <flag> is supported by the C compiler, else returns shell false.
  399. test-flag-CC() { test-flag-PROG "CC" c "$1"; }
  400. # @FUNCTION: test-flag-CXX
  401. # @USAGE: <flag>
  402. # @DESCRIPTION:
  403. # Returns shell true if <flag> is supported by the C++ compiler, else returns shell false.
  404. test-flag-CXX() { test-flag-PROG "CXX" c++ "$1"; }
  405. # @FUNCTION: test-flag-F77
  406. # @USAGE: <flag>
  407. # @DESCRIPTION:
  408. # Returns shell true if <flag> is supported by the Fortran 77 compiler, else returns shell false.
  409. test-flag-F77() { test-flag-PROG "F77" f77 "$1"; }
  410. # @FUNCTION: test-flag-FC
  411. # @USAGE: <flag>
  412. # @DESCRIPTION:
  413. # Returns shell true if <flag> is supported by the Fortran 90 compiler, else returns shell false.
  414. test-flag-FC() { test-flag-PROG "FC" f95 "$1"; }
  415. test-flags-PROG() {
  416. local comp=$1
  417. local flags=()
  418. local x
  419. shift
  420. [[ -z ${comp} ]] && return 1
  421. for x ; do
  422. test-flag-${comp} "${x}" && flags+=( "${x}" )
  423. done
  424. echo "${flags[*]}"
  425. # Just bail if we dont have any flags
  426. [[ ${#flags[@]} -gt 0 ]]
  427. }
  428. # @FUNCTION: test-flags-CC
  429. # @USAGE: <flags>
  430. # @DESCRIPTION:
  431. # Returns shell true if <flags> are supported by the C compiler, else returns shell false.
  432. test-flags-CC() { test-flags-PROG "CC" "$@"; }
  433. # @FUNCTION: test-flags-CXX
  434. # @USAGE: <flags>
  435. # @DESCRIPTION:
  436. # Returns shell true if <flags> are supported by the C++ compiler, else returns shell false.
  437. test-flags-CXX() { test-flags-PROG "CXX" "$@"; }
  438. # @FUNCTION: test-flags-F77
  439. # @USAGE: <flags>
  440. # @DESCRIPTION:
  441. # Returns shell true if <flags> are supported by the Fortran 77 compiler, else returns shell false.
  442. test-flags-F77() { test-flags-PROG "F77" "$@"; }
  443. # @FUNCTION: test-flags-FC
  444. # @USAGE: <flags>
  445. # @DESCRIPTION:
  446. # Returns shell true if <flags> are supported by the Fortran 90 compiler, else returns shell false.
  447. test-flags-FC() { test-flags-PROG "FC" "$@"; }
  448. # @FUNCTION: test-flags
  449. # @USAGE: <flags>
  450. # @DESCRIPTION:
  451. # Short-hand that should hopefully work for both C and C++ compiler, but
  452. # its really only present due to the append-flags() abomination.
  453. test-flags() { test-flags-CC "$@"; }
  454. # @FUNCTION: test_version_info
  455. # @USAGE: <version>
  456. # @DESCRIPTION:
  457. # Returns shell true if the current C compiler version matches <version>, else returns shell false.
  458. # Accepts shell globs.
  459. test_version_info() {
  460. if [[ $($(tc-getCC) --version 2>&1) == *$1* ]]; then
  461. return 0
  462. else
  463. return 1
  464. fi
  465. }
  466. # @FUNCTION: strip-unsupported-flags
  467. # @DESCRIPTION:
  468. # Strip {C,CXX,F,FC}FLAGS of any flags not supported by the active toolchain.
  469. strip-unsupported-flags() {
  470. export CFLAGS=$(test-flags-CC ${CFLAGS})
  471. export CXXFLAGS=$(test-flags-CXX ${CXXFLAGS})
  472. export FFLAGS=$(test-flags-F77 ${FFLAGS})
  473. export FCFLAGS=$(test-flags-FC ${FCFLAGS})
  474. }
  475. # @FUNCTION: get-flag
  476. # @USAGE: <flag>
  477. # @DESCRIPTION:
  478. # Find and echo the value for a particular flag. Accepts shell globs.
  479. get-flag() {
  480. local f var findflag="$1"
  481. # this code looks a little flaky but seems to work for
  482. # everything we want ...
  483. # for example, if CFLAGS="-march=i686":
  484. # `get-flag -march` == "-march=i686"
  485. # `get-flag march` == "i686"
  486. for var in $(all-flag-vars) ; do
  487. for f in ${!var} ; do
  488. if [ "${f/${findflag}}" != "${f}" ] ; then
  489. printf "%s\n" "${f/-${findflag}=}"
  490. return 0
  491. fi
  492. done
  493. done
  494. return 1
  495. }
  496. has_m64() {
  497. die "${FUNCNAME}: don't use this anymore"
  498. }
  499. has_m32() {
  500. die "${FUNCNAME}: don't use this anymore"
  501. }
  502. # @FUNCTION: replace-sparc64-flags
  503. # @DESCRIPTION:
  504. # Sets mcpu to v8 and uses the original value as mtune if none specified.
  505. replace-sparc64-flags() {
  506. local SPARC64_CPUS="ultrasparc3 ultrasparc v9"
  507. if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
  508. for x in ${SPARC64_CPUS}; do
  509. CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
  510. done
  511. else
  512. for x in ${SPARC64_CPUS}; do
  513. CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
  514. done
  515. fi
  516. if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
  517. for x in ${SPARC64_CPUS}; do
  518. CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
  519. done
  520. else
  521. for x in ${SPARC64_CPUS}; do
  522. CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
  523. done
  524. fi
  525. export CFLAGS CXXFLAGS
  526. }
  527. # @FUNCTION: append-libs
  528. # @USAGE: <libs>
  529. # @DESCRIPTION:
  530. # Add extra <libs> to the current LIBS. All arguments should be prefixed with
  531. # either -l or -L. For compatibility, if arguments are not prefixed as
  532. # options, they are given a -l prefix automatically.
  533. append-libs() {
  534. [[ $# -eq 0 ]] && return 0
  535. local flag
  536. for flag in "$@"; do
  537. if [[ -z "${flag// }" ]]; then
  538. eqawarn "Appending an empty argument to LIBS is invalid! Skipping."
  539. continue
  540. fi
  541. case $flag in
  542. -[lL]*)
  543. export LIBS="${LIBS} ${flag}"
  544. ;;
  545. -*)
  546. eqawarn "Appending non-library to LIBS (${flag}); Other linker flags should be passed via LDFLAGS"
  547. export LIBS="${LIBS} ${flag}"
  548. ;;
  549. *)
  550. export LIBS="${LIBS} -l${flag}"
  551. esac
  552. done
  553. return 0
  554. }
  555. # @FUNCTION: raw-ldflags
  556. # @USAGE: [flags]
  557. # @DESCRIPTION:
  558. # Turn C style ldflags (-Wl,-foo) into straight ldflags - the results
  559. # are suitable for passing directly to 'ld'; note LDFLAGS is usually passed
  560. # to gcc where it needs the '-Wl,'.
  561. #
  562. # If no flags are specified, then default to ${LDFLAGS}.
  563. raw-ldflags() {
  564. local x input="$@"
  565. [[ -z ${input} ]] && input=${LDFLAGS}
  566. set --
  567. for x in ${input} ; do
  568. case ${x} in
  569. -Wl,*)
  570. x=${x#-Wl,}
  571. set -- "$@" ${x//,/ }
  572. ;;
  573. *) # Assume it's a compiler driver flag, so throw it away #441808
  574. ;;
  575. esac
  576. done
  577. echo "$@"
  578. }
  579. # @FUNCTION: no-as-needed
  580. # @RETURN: Flag to disable asneeded behavior for use with append-ldflags.
  581. no-as-needed() {
  582. case $($(tc-getLD) -v 2>&1 </dev/null) in
  583. *GNU*) # GNU ld
  584. echo "-Wl,--no-as-needed" ;;
  585. esac
  586. }
  587. fi