1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
6 # Michał Górny <mgorny@gentoo.org>
8 # Michał Górny <mgorny@gentoo.org>
9 # @BLURB: Utility functions to build against slotted LLVM
11 # The llvm.eclass provides utility functions that can be used to build
12 # against specific version of slotted LLVM (with fallback to :0 for old
15 # This eclass does not generate dependency strings. You need to write
16 # a proper dependency string yourself to guarantee that appropriate
17 # version of LLVM is installed.
19 # Example use for a package supporting LLVM 3.8 to 5:
21 # inherit cmake-utils llvm
24 # <sys-devel/llvm-6_rc:=
28 # >=sys-devel/llvm-3.8:0
34 # # only if you need to define one explicitly
41 # Example for a package needing LLVM+clang w/ a specific target:
43 # inherit cmake-utils llvm
45 # # note: do not use := on both clang and llvm, it can match different
46 # # slots then. clang pulls llvm in, so we can skip the latter.
48 # >=sys-devel/clang-4:=[llvm_targets_AMDGPU(+)]
52 # has_version "sys-devel/clang:${LLVM_SLOT}[llvm_targets_AMDGPU(+)]"
58 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
63 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
67 EXPORT_FUNCTIONS pkg_setup
69 if [[ ! ${_LLVM_ECLASS} ]]; then
71 # @ECLASS-VARIABLE: LLVM_MAX_SLOT
74 # Highest LLVM slot supported by the package. Needs to be set before
75 # llvm_pkg_setup is called. If unset, no upper bound is assumed.
77 # @ECLASS-VARIABLE: _LLVM_KNOWN_SLOTS
80 # Correct values of LLVM slots, newest first.
81 declare -g -r _LLVM_KNOWN_SLOTS=( 7 6 5 4 )
83 # @FUNCTION: get_llvm_prefix
84 # @USAGE: [<max_slot>]
86 # Find the newest LLVM install that is acceptable for the package,
87 # and print an absolute path to it.
89 # If <max_slot> is specified, then only LLVM versions that are not newer
90 # than <max_slot> will be considered. Otherwise, all LLVM versions would
91 # be considered acceptable. The function does not support specifying
92 # minimal supported version -- the developer must ensure that a version
93 # new enough is installed via providing appropriate dependencies.
95 # If llvm_check_deps() function is defined within the ebuild, it will
96 # be called to verify whether a particular slot is accepable. Within
97 # the function scope, LLVM_SLOT will be defined to the SLOT value
98 # (0, 4, 5...). The function should return a true status if the slot
99 # is acceptable, false otherwise. If llvm_check_deps() is not defined,
100 # the function defaults to checking whether sys-devel/llvm:${LLVM_SLOT}
103 debug-print-function ${FUNCNAME} "${@}"
107 for slot in "${_LLVM_KNOWN_SLOTS[@]}"; do
109 if [[ -n ${max_slot} ]]; then
110 if [[ ${max_slot} == ${slot} ]]; then
117 if declare -f llvm_check_deps >/dev/null; then
118 local LLVM_SLOT=${slot}
119 llvm_check_deps || continue
121 # check if LLVM package is installed
122 has_version "sys-devel/llvm:${slot}" || continue
125 echo "${EPREFIX}/usr/lib/llvm/${slot}"
129 # max_slot should have been unset in the iteration
130 if [[ -n ${max_slot} ]]; then
131 die "${FUNCNAME}: invalid max_slot=${max_slot}"
135 # assume it's always <= 4 (the lower max_slot allowed)
136 if has_version "sys-devel/llvm:0"; then
137 echo "${EPREFIX}/usr"
141 die "No LLVM slot${1:+ <= ${1}} found installed!"
144 # @FUNCTION: llvm_pkg_setup
146 # Prepend the appropriate executable directory for the newest
147 # acceptable LLVM slot to the PATH. For path determination logic,
148 # please see the get_llvm_prefix documentation.
150 # The highest acceptable LLVM slot can be set in LLVM_MAX_SLOT variable.
151 # If it is unset or empty, any slot is acceptable.
153 # The PATH manipulation is only done for source builds. The function
154 # is a no-op when installing a binary package.
156 # If any other behavior is desired, the contents of the function
157 # should be inlined into the ebuild and modified as necessary.
159 debug-print-function ${FUNCNAME} "${@}"
161 if [[ ${MERGE_TYPE} != binary ]]; then
162 local llvm_prefix=$(get_llvm_prefix "${LLVM_MAX_SLOT}")
164 # do not prepend /usr/bin, it's not necessary and breaks other
165 # prepends, https://bugs.gentoo.org/622866
166 if [[ ${llvm_prefix} != ${EPREFIX}/usr ]]; then
167 export PATH=${llvm_prefix}/bin:${PATH}