*/*: Bump copyright on files touched this year
[gentoo.git] / eclass / llvm.eclass
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: llvm.eclass
5 # @MAINTAINER:
6 # Michał Górny <mgorny@gentoo.org>
7 # @AUTHOR:
8 # Michał Górny <mgorny@gentoo.org>
9 # @SUPPORTED_EAPIS: 6 7
10 # @BLURB: Utility functions to build against slotted LLVM
11 # @DESCRIPTION:
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
14 # versions).
15 #
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.
19 #
20 # Example use for a package supporting LLVM 5 to 7:
21 # @CODE
22 # inherit cmake-utils llvm
23 #
24 # RDEPEND="
25 #       <sys-devel/llvm-8:=
26 #       || (
27 #               sys-devel/llvm:7
28 #               sys-devel/llvm:6
29 #               sys-devel/llvm:5
30 #       )
31 # "
32 # DEPEND=${RDEPEND}
33 #
34 # LLVM_MAX_SLOT=7
35 #
36 # # only if you need to define one explicitly
37 # pkg_setup() {
38 #       llvm_pkg_setup
39 #       do-something-else
40 # }
41 # @CODE
42 #
43 # Example for a package needing LLVM+clang w/ a specific target:
44 # @CODE
45 # inherit cmake-utils llvm
46 #
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.
49 # RDEPEND="
50 #       >=sys-devel/clang-6:=[llvm_targets_AMDGPU(+)]
51 # "
52 # DEPEND=${RDEPEND}
53 #
54 # llvm_check_deps() {
55 #       has_version -d "sys-devel/clang:${LLVM_SLOT}[llvm_targets_AMDGPU(+)]"
56 # }
57 # @CODE
58
59 case "${EAPI:-0}" in
60         0|1|2|3|4|5)
61                 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
62                 ;;
63         6|7)
64                 ;;
65         *)
66                 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
67                 ;;
68 esac
69
70 EXPORT_FUNCTIONS pkg_setup
71
72 if [[ ! ${_LLVM_ECLASS} ]]; then
73
74 # @ECLASS-VARIABLE: LLVM_MAX_SLOT
75 # @DEFAULT_UNSET
76 # @DESCRIPTION:
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.
79
80 # @ECLASS-VARIABLE: _LLVM_KNOWN_SLOTS
81 # @INTERNAL
82 # @DESCRIPTION:
83 # Correct values of LLVM slots, newest first.
84 declare -g -r _LLVM_KNOWN_SLOTS=( 11 10 9 8 7 6 5 4 )
85
86 # @FUNCTION: get_llvm_prefix
87 # @USAGE: [-b|-d] [<max_slot>]
88 # @DESCRIPTION:
89 # Find the newest LLVM install that is acceptable for the package,
90 # and print an absolute path to it.
91 #
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.
95 #
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.
99 #
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.
105 #
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}
112 # is installed.
113 get_llvm_prefix() {
114         debug-print-function ${FUNCNAME} "${@}"
115
116         local hv_switch=-d
117         while [[ ${1} == -* ]]; do
118                 case ${1} in
119                         -b|-d) hv_switch=${1};;
120                         *) break;;
121                 esac
122                 shift
123         done
124
125         local prefix=
126         if [[ ${EAPI} != 6 ]]; then
127                 case ${hv_switch} in
128                         -b)
129                                 prefix=${BROOT}
130                                 ;;
131                         -d)
132                                 prefix=${ESYSROOT}
133                                 ;;
134                 esac
135         else
136                 case ${hv_switch} in
137                         -b)
138                                 die "${FUNCNAME} -b is not supported in EAPI ${EAPI}"
139                                 ;;
140                         -d)
141                                 prefix=${EPREFIX}
142                                 hv_switch=
143                                 ;;
144                 esac
145         fi
146
147         local max_slot=${1}
148         local slot
149         for slot in "${_LLVM_KNOWN_SLOTS[@]}"; do
150                 # skip higher slots
151                 if [[ -n ${max_slot} ]]; then
152                         if [[ ${max_slot} == ${slot} ]]; then
153                                 max_slot=
154                         else
155                                 continue
156                         fi
157                 fi
158
159                 if declare -f llvm_check_deps >/dev/null; then
160                         local LLVM_SLOT=${slot}
161                         llvm_check_deps || continue
162                 else
163                         # check if LLVM package is installed
164                         has_version ${hv_switch} "sys-devel/llvm:${slot}" || continue
165                 fi
166
167                 echo "${prefix}/usr/lib/llvm/${slot}"
168                 return
169         done
170
171         # max_slot should have been unset in the iteration
172         if [[ -n ${max_slot} ]]; then
173                 die "${FUNCNAME}: invalid max_slot=${max_slot}"
174         fi
175
176         # fallback to :0
177         # assume it's always <= 4 (the lower max_slot allowed)
178         if has_version ${hv_switch} "sys-devel/llvm:0"; then
179                 echo "${prefix}/usr"
180                 return
181         fi
182
183         die "No LLVM slot${1:+ <= ${1}} found installed!"
184 }
185
186 # @FUNCTION: llvm_pkg_setup
187 # @DESCRIPTION:
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.
191 #
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.
194 #
195 # The PATH manipulation is only done for source builds. The function
196 # is a no-op when installing a binary package.
197 #
198 # If any other behavior is desired, the contents of the function
199 # should be inlined into the ebuild and modified as necessary.
200 llvm_pkg_setup() {
201         debug-print-function ${FUNCNAME} "${@}"
202
203         if [[ ${MERGE_TYPE} != binary ]]; then
204                 local llvm_prefix=$(get_llvm_prefix "${LLVM_MAX_SLOT}")
205
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}
210                 fi
211         fi
212 }
213
214 _LLVM_ECLASS=1
215 fi