llvm.eclass 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # Copyright 1999-2017 Gentoo Foundation
  2. # Distributed under the terms of the GNU General Public License v2
  3. # @ECLASS: llvm.eclass
  4. # @MAINTAINER:
  5. # Michał Górny <mgorny@gentoo.org>
  6. # @AUTHOR:
  7. # Michał Górny <mgorny@gentoo.org>
  8. # @BLURB: Utility functions to build against slotted LLVM
  9. # @DESCRIPTION:
  10. # The llvm.eclass provides utility functions that can be used to build
  11. # against specific version of slotted LLVM (with fallback to :0 for old
  12. # versions).
  13. #
  14. # This eclass does not generate dependency strings. You need to write
  15. # a proper dependency string yourself to guarantee that appropriate
  16. # version of LLVM is installed.
  17. #
  18. # Example use for a package supporting LLVM 3.8 to 5:
  19. # @CODE
  20. # inherit cmake-utils llvm
  21. #
  22. # RDEPEND="
  23. # <sys-devel/llvm-6_rc:=
  24. # || (
  25. # sys-devel/llvm:5
  26. # sys-devel/llvm:4
  27. # >=sys-devel/llvm-3.8:0
  28. # )
  29. # "
  30. #
  31. # LLVM_MAX_SLOT=5
  32. #
  33. # # only if you need to define one explicitly
  34. # pkg_setup() {
  35. # llvm_pkg_setup
  36. # do-something-else
  37. # }
  38. # @CODE
  39. case "${EAPI:-0}" in
  40. 0|1|2|3|4|5)
  41. die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
  42. ;;
  43. 6)
  44. ;;
  45. *)
  46. die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
  47. ;;
  48. esac
  49. EXPORT_FUNCTIONS pkg_setup
  50. if [[ ! ${_LLVM_ECLASS} ]]; then
  51. # @ECLASS-VARIABLE: LLVM_MAX_SLOT
  52. # @DEFAULT_UNSET
  53. # @DESCRIPTION:
  54. # Highest LLVM slot supported by the package. Needs to be set before
  55. # llvm_pkg_setup is called. If unset, no upper bound is assumed.
  56. # @ECLASS-VARIABLE: _LLVM_KNOWN_SLOTS
  57. # @INTERNAL
  58. # @DESCRIPTION:
  59. # Correct values of LLVM slots, newest first.
  60. declare -g -r _LLVM_KNOWN_SLOTS=( 5 4 )
  61. # @FUNCTION: get_llvm_prefix
  62. # @USAGE: [<max_slot>]
  63. # @DESCRIPTION:
  64. # Prints the absolute path to an LLVM install prefix corresponding to
  65. # the newest installed version of LLVM that is not newer than
  66. # <max_slot>. If no <max_slot> is specified, there is no upper limit.
  67. #
  68. # Note that the function does not support lower-bound version, so you
  69. # need to provide correct dependencies to ensure that a new enough
  70. # version will be always installed. Otherwise, the function could return
  71. # a version lower than required.
  72. get_llvm_prefix() {
  73. debug-print-function ${FUNCNAME} "${@}"
  74. local max_slot=${1}
  75. local slot
  76. for slot in "${_LLVM_KNOWN_SLOTS[@]}"; do
  77. # skip higher slots
  78. if [[ -n ${max_slot} ]]; then
  79. if [[ ${max_slot} == ${slot} ]]; then
  80. max_slot=
  81. else
  82. continue
  83. fi
  84. fi
  85. local p=${EPREFIX}/usr/lib/llvm/${slot}
  86. if [[ -x ${p}/bin/llvm-config ]]; then
  87. echo "${p}"
  88. return
  89. fi
  90. done
  91. # max_slot should have been unset in the iteration
  92. if [[ -n ${max_slot} ]]; then
  93. die "${FUNCNAME}: invalid max_slot=${max_slot}"
  94. fi
  95. # fallback to :0
  96. # assume it's always <= 4 (the lower max_slot allowed)
  97. p=${EPREFIX}/usr
  98. if [[ -x ${p}/bin/llvm-config ]]; then
  99. echo "${p}"
  100. return
  101. fi
  102. die "No LLVM slot${1:+ <= ${1}} found in PATH!"
  103. }
  104. # @FUNCTION: llvm_pkg_setup
  105. # @DESCRIPTION:
  106. # Prepend the executable directory corresponding to the newest
  107. # installed LLVM version that is not newer than ${LLVM_MAX_SLOT}
  108. # to PATH. If LLVM_MAX_SLOT is unset or empty, the newest installed
  109. # slot will be used.
  110. #
  111. # The PATH manipulation is only done for source builds. The function
  112. # is a no-op when installing a binary package.
  113. #
  114. # If any other behavior is desired, the contents of the function
  115. # should be inlined into the ebuild and modified as necessary.
  116. llvm_pkg_setup() {
  117. debug-print-function ${FUNCNAME} "${@}"
  118. if [[ ${MERGE_TYPE} != binary ]]; then
  119. export PATH=$(get_llvm_prefix ${LLVM_MAX_SLOT})/bin:${PATH}
  120. fi
  121. }
  122. _LLVM_ECLASS=1
  123. fi