kde-plasma/breeze-gtk: x86 stable wrt bug #613144
[gentoo.git] / eclass / llvm.eclass
1 # Copyright 1999-2017 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 case "${EAPI:-0}" in
42         0|1|2|3|4|5)
43                 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
44                 ;;
45         6)
46                 ;;
47         *)
48                 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
49                 ;;
50 esac
51
52 EXPORT_FUNCTIONS pkg_setup
53
54 if [[ ! ${_LLVM_ECLASS} ]]; then
55
56 # @ECLASS-VARIABLE: LLVM_MAX_SLOT
57 # @DEFAULT_UNSET
58 # @DESCRIPTION:
59 # Highest LLVM slot supported by the package. Needs to be set before
60 # llvm_pkg_setup is called. If unset, no upper bound is assumed.
61
62 # @ECLASS-VARIABLE: _LLVM_KNOWN_SLOTS
63 # @INTERNAL
64 # @DESCRIPTION:
65 # Correct values of LLVM slots, newest first.
66 declare -g -r _LLVM_KNOWN_SLOTS=( 5 4 )
67
68 # @FUNCTION: get_llvm_prefix
69 # @USAGE: [<max_slot>]
70 # @DESCRIPTION:
71 # Prints the absolute path to an LLVM install prefix corresponding to
72 # the newest installed version of LLVM that is not newer than
73 # <max_slot>. If no <max_slot> is specified, there is no upper limit.
74 #
75 # Note that the function does not support lower-bound version, so you
76 # need to provide correct dependencies to ensure that a new enough
77 # version will be always installed. Otherwise, the function could return
78 # a version lower than required.
79 get_llvm_prefix() {
80         debug-print-function ${FUNCNAME} "${@}"
81
82         local max_slot=${1}
83         local slot
84         for slot in "${_LLVM_KNOWN_SLOTS[@]}"; do
85                 # skip higher slots
86                 if [[ -n ${max_slot} ]]; then
87                         if [[ ${max_slot} == ${slot} ]]; then
88                                 max_slot=
89                         else
90                                 continue
91                         fi
92                 fi
93
94                 local p=${EPREFIX}/usr/lib/llvm/${slot}
95                 if [[ -x ${p}/bin/llvm-config ]]; then
96                         echo "${p}"
97                         return
98                 fi
99         done
100
101         # max_slot should have been unset in the iteration
102         if [[ -n ${max_slot} ]]; then
103                 die "${FUNCNAME}: invalid max_slot=${max_slot}"
104         fi
105
106         # fallback to :0
107         # assume it's always <= 4 (the lower max_slot allowed)
108         p=${EPREFIX}/usr
109         if [[ -x ${p}/bin/llvm-config ]]; then
110                 echo "${p}"
111                 return
112         fi
113
114         die "No LLVM slot${1:+ <= ${1}} found in PATH!"
115 }
116
117 # @FUNCTION: llvm_pkg_setup
118 # @DESCRIPTION:
119 # Prepend the executable directory corresponding to the newest
120 # installed LLVM version that is not newer than ${LLVM_MAX_SLOT}
121 # to PATH. If LLVM_MAX_SLOT is unset or empty, the newest installed
122 # slot will be used.
123 #
124 # The PATH manipulation is only done for source builds. The function
125 # is a no-op when installing a binary package.
126 #
127 # If any other behavior is desired, the contents of the function
128 # should be inlined into the ebuild and modified as necessary.
129 llvm_pkg_setup() {
130         debug-print-function ${FUNCNAME} "${@}"
131
132         if [[ ${MERGE_TYPE} != binary ]]; then
133                 export PATH=$(get_llvm_prefix ${LLVM_MAX_SLOT})/bin:${PATH}
134         fi
135 }
136
137 _LLVM_ECLASS=1
138 fi