meson.eclass: Don't mix host *FLAGS with build *FLAGS
[gentoo.git] / eclass / llvm.eclass
1 # Copyright 1999-2018 Gentoo Foundation
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 # @BLURB: Utility functions to build against slotted LLVM
10 # @DESCRIPTION:
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
13 # versions).
14 #
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.
18 #
19 # Example use for a package supporting LLVM 3.8 to 5:
20 # @CODE
21 # inherit cmake-utils llvm
22 #
23 # RDEPEND="
24 #       <sys-devel/llvm-6_rc:=
25 #       || (
26 #               sys-devel/llvm:5
27 #               sys-devel/llvm:4
28 #               >=sys-devel/llvm-3.8:0
29 #       )
30 # "
31 #
32 # LLVM_MAX_SLOT=5
33 #
34 # # only if you need to define one explicitly
35 # pkg_setup() {
36 #       llvm_pkg_setup
37 #       do-something-else
38 # }
39 # @CODE
40 #
41 # Example for a package needing LLVM+clang w/ a specific target:
42 # @CODE
43 # inherit cmake-utils llvm
44 #
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.
47 # RDEPEND="
48 #       >=sys-devel/clang-4:=[llvm_targets_AMDGPU(+)]
49 # "
50 #
51 # llvm_check_deps() {
52 #       has_version "sys-devel/clang:${LLVM_SLOT}[llvm_targets_AMDGPU(+)]"
53 # }
54 # @CODE
55
56 case "${EAPI:-0}" in
57         0|1|2|3|4|5)
58                 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
59                 ;;
60         6)
61                 ;;
62         *)
63                 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
64                 ;;
65 esac
66
67 EXPORT_FUNCTIONS pkg_setup
68
69 if [[ ! ${_LLVM_ECLASS} ]]; then
70
71 # @ECLASS-VARIABLE: LLVM_MAX_SLOT
72 # @DEFAULT_UNSET
73 # @DESCRIPTION:
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.
76
77 # @ECLASS-VARIABLE: _LLVM_KNOWN_SLOTS
78 # @INTERNAL
79 # @DESCRIPTION:
80 # Correct values of LLVM slots, newest first.
81 declare -g -r _LLVM_KNOWN_SLOTS=( 7 6 5 4 )
82
83 # @FUNCTION: get_llvm_prefix
84 # @USAGE: [<max_slot>]
85 # @DESCRIPTION:
86 # Find the newest LLVM install that is acceptable for the package,
87 # and print an absolute path to it.
88 #
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.
94 #
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}
101 # is installed.
102 get_llvm_prefix() {
103         debug-print-function ${FUNCNAME} "${@}"
104
105         local max_slot=${1}
106         local slot
107         for slot in "${_LLVM_KNOWN_SLOTS[@]}"; do
108                 # skip higher slots
109                 if [[ -n ${max_slot} ]]; then
110                         if [[ ${max_slot} == ${slot} ]]; then
111                                 max_slot=
112                         else
113                                 continue
114                         fi
115                 fi
116
117                 if declare -f llvm_check_deps >/dev/null; then
118                         local LLVM_SLOT=${slot}
119                         llvm_check_deps || continue
120                 else
121                         # check if LLVM package is installed
122                         has_version "sys-devel/llvm:${slot}" || continue
123                 fi
124
125                 echo "${EPREFIX}/usr/lib/llvm/${slot}"
126                 return
127         done
128
129         # max_slot should have been unset in the iteration
130         if [[ -n ${max_slot} ]]; then
131                 die "${FUNCNAME}: invalid max_slot=${max_slot}"
132         fi
133
134         # fallback to :0
135         # assume it's always <= 4 (the lower max_slot allowed)
136         if has_version "sys-devel/llvm:0"; then
137                 echo "${EPREFIX}/usr"
138                 return
139         fi
140
141         die "No LLVM slot${1:+ <= ${1}} found installed!"
142 }
143
144 # @FUNCTION: llvm_pkg_setup
145 # @DESCRIPTION:
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.
149 #
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.
152 #
153 # The PATH manipulation is only done for source builds. The function
154 # is a no-op when installing a binary package.
155 #
156 # If any other behavior is desired, the contents of the function
157 # should be inlined into the ebuild and modified as necessary.
158 llvm_pkg_setup() {
159         debug-print-function ${FUNCNAME} "${@}"
160
161         if [[ ${MERGE_TYPE} != binary ]]; then
162                 local llvm_prefix=$(get_llvm_prefix "${LLVM_MAX_SLOT}")
163
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}
168                 fi
169         fi
170 }
171
172 _LLVM_ECLASS=1
173 fi