1 # Copyright 1999-2020 Gentoo Authors
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 # @SUPPORTED_EAPIS: 6 7
10 # @BLURB: Utility functions to build against slotted LLVM
12 # The llvm.eclass provides utility functions that can be used to build
13 # against specific version of slotted LLVM (with fallback to :0 for old
16 # This eclass does not generate dependency strings. You need to write
17 # a proper dependency string yourself to guarantee that appropriate
18 # version of LLVM is installed.
20 # Example use for a package supporting LLVM 5 to 7:
22 # inherit cmake-utils llvm
36 # # only if you need to define one explicitly
43 # Example for a package needing LLVM+clang w/ a specific target:
45 # inherit cmake-utils llvm
47 # # note: do not use := on both clang and llvm, it can match different
48 # # slots then. clang pulls llvm in, so we can skip the latter.
50 # >=sys-devel/clang-6:=[llvm_targets_AMDGPU(+)]
55 # has_version -d "sys-devel/clang:${LLVM_SLOT}[llvm_targets_AMDGPU(+)]"
61 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
66 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
70 EXPORT_FUNCTIONS pkg_setup
72 if [[ ! ${_LLVM_ECLASS} ]]; then
74 # @ECLASS-VARIABLE: LLVM_MAX_SLOT
77 # Highest LLVM slot supported by the package. Needs to be set before
78 # llvm_pkg_setup is called. If unset, no upper bound is assumed.
80 # @ECLASS-VARIABLE: _LLVM_KNOWN_SLOTS
83 # Correct values of LLVM slots, newest first.
84 declare -g -r _LLVM_KNOWN_SLOTS=( 11 10 9 8 7 6 5 4 )
86 # @FUNCTION: get_llvm_prefix
87 # @USAGE: [-b|-d] [<max_slot>]
89 # Find the newest LLVM install that is acceptable for the package,
90 # and print an absolute path to it.
92 # If -b is specified, the checks are performed relative to BROOT,
93 # and BROOT-path is returned. This is appropriate when your package
94 # calls llvm-config executable. -b is supported since EAPI 7.
96 # If -d is specified, the checks are performed relative to ESYSROOT,
97 # and ESYSROOT-path is returned. This is appropriate when your package
98 # uses CMake find_package(LLVM). -d is the default.
100 # If <max_slot> is specified, then only LLVM versions that are not newer
101 # than <max_slot> will be considered. Otherwise, all LLVM versions would
102 # be considered acceptable. The function does not support specifying
103 # minimal supported version -- the developer must ensure that a version
104 # new enough is installed via providing appropriate dependencies.
106 # If llvm_check_deps() function is defined within the ebuild, it will
107 # be called to verify whether a particular slot is accepable. Within
108 # the function scope, LLVM_SLOT will be defined to the SLOT value
109 # (0, 4, 5...). The function should return a true status if the slot
110 # is acceptable, false otherwise. If llvm_check_deps() is not defined,
111 # the function defaults to checking whether sys-devel/llvm:${LLVM_SLOT}
114 debug-print-function ${FUNCNAME} "${@}"
117 while [[ ${1} == -* ]]; do
119 -b|-d) hv_switch=${1};;
126 if [[ ${EAPI} != 6 ]]; then
138 die "${FUNCNAME} -b is not supported in EAPI ${EAPI}"
149 for slot in "${_LLVM_KNOWN_SLOTS[@]}"; do
151 if [[ -n ${max_slot} ]]; then
152 if [[ ${max_slot} == ${slot} ]]; then
159 if declare -f llvm_check_deps >/dev/null; then
160 local LLVM_SLOT=${slot}
161 llvm_check_deps || continue
163 # check if LLVM package is installed
164 has_version ${hv_switch} "sys-devel/llvm:${slot}" || continue
167 echo "${prefix}/usr/lib/llvm/${slot}"
171 # max_slot should have been unset in the iteration
172 if [[ -n ${max_slot} ]]; then
173 die "${FUNCNAME}: invalid max_slot=${max_slot}"
177 # assume it's always <= 4 (the lower max_slot allowed)
178 if has_version ${hv_switch} "sys-devel/llvm:0"; then
183 die "No LLVM slot${1:+ <= ${1}} found installed!"
186 # @FUNCTION: llvm_pkg_setup
188 # Prepend the appropriate executable directory for the newest
189 # acceptable LLVM slot to the PATH. For path determination logic,
190 # please see the get_llvm_prefix documentation.
192 # The highest acceptable LLVM slot can be set in LLVM_MAX_SLOT variable.
193 # If it is unset or empty, any slot is acceptable.
195 # The PATH manipulation is only done for source builds. The function
196 # is a no-op when installing a binary package.
198 # If any other behavior is desired, the contents of the function
199 # should be inlined into the ebuild and modified as necessary.
201 debug-print-function ${FUNCNAME} "${@}"
203 if [[ ${MERGE_TYPE} != binary ]]; then
204 local llvm_prefix=$(get_llvm_prefix "${LLVM_MAX_SLOT}")
206 # do not prepend /usr/bin, it's not necessary and breaks other
207 # prepends, https://bugs.gentoo.org/622866
208 if [[ ${llvm_prefix} != ${EPREFIX}/usr ]]; then
209 export PATH=${llvm_prefix}/bin:${PATH}