Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-dev@l.g.o
Cc: "Michał Górny" <mgorny@g.o>
Subject: [gentoo-dev] [PATCH v3] llvm.eclass: An eclass to handle dependencies on slotted LLVM
Date: Sat, 04 Feb 2017 12:52:54
Message-Id: 20170204125236.12821-1-mgorny@gentoo.org
In Reply to: [gentoo-dev] [PATCH] llvm.eclass: An eclass to handle dependencies on slotted LLVM by "Michał Górny"
1 Introduce an eclass to support dependencies building against slotted
2 LLVM. It provides a function to find the newest installed LLVM version
3 that is not newer than the max supported slot, and a trivial pkg_setup()
4 implementation that adds executable directory of this install to PATH.
5 This ensures that:
6
7 a) build systems will find the correct llvm-config and use it to use
8 the correct version of LLVM,
9
10 b) CMake's find_package() will find the correct LLVMConfig / ClangConfig
11 files.
12 ---
13 eclass/llvm.eclass | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++
14 1 file changed, 139 insertions(+)
15 create mode 100644 eclass/llvm.eclass
16
17 diff --git a/eclass/llvm.eclass b/eclass/llvm.eclass
18 new file mode 100644
19 index 000000000000..bde050b96851
20 --- /dev/null
21 +++ b/eclass/llvm.eclass
22 @@ -0,0 +1,139 @@
23 +# Copyright 1999-2017 Gentoo Foundation
24 +# Distributed under the terms of the GNU General Public License v2
25 +# $Id$
26 +
27 +# @ECLASS: llvm.eclass
28 +# @MAINTAINER:
29 +# Michał Górny <mgorny@g.o>
30 +# @AUTHOR:
31 +# Michał Górny <mgorny@g.o>
32 +# @BLURB: Utility functions to build against slotted LLVM
33 +# @DESCRIPTION:
34 +# The llvm.eclass provides utility functions that can be used to build
35 +# against specific version of slotted LLVM (with fallback to :0 for old
36 +# versions).
37 +#
38 +# This eclass does not generate dependency strings. You need to write
39 +# a proper dependency string yourself to guarantee that appropriate
40 +# version of LLVM is installed.
41 +#
42 +# Example use for a package supporting LLVM 3.8 to 5:
43 +# @CODE
44 +# inherit cmake-utils llvm
45 +#
46 +# RDEPEND="
47 +# <sys-devel/llvm-6_rc:=
48 +# || (
49 +# sys-devel/llvm:5
50 +# sys-devel/llvm:4
51 +# >=sys-devel/llvm-3.8:0
52 +# )
53 +# "
54 +#
55 +# LLVM_MAX_SLOT=5
56 +#
57 +# # only if you need to define one explicitly
58 +# pkg_setup() {
59 +# llvm_pkg_setup
60 +# do-something-else
61 +# }
62 +# @CODE
63 +
64 +case "${EAPI:-0}" in
65 + 0|1|2|3|4|5)
66 + die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
67 + ;;
68 + 6)
69 + ;;
70 + *)
71 + die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
72 + ;;
73 +esac
74 +
75 +EXPORT_FUNCTIONS pkg_setup
76 +
77 +if [[ ! ${_LLVM_ECLASS} ]]; then
78 +
79 +# @ECLASS-VARIABLE: LLVM_MAX_SLOT
80 +# @DEFAULT_UNSET
81 +# @DESCRIPTION:
82 +# Highest LLVM slot supported by the package. Needs to be set before
83 +# llvm_pkg_setup is called. If unset, no upper bound is assumed.
84 +
85 +# @ECLASS-VARIABLE: _LLVM_KNOWN_SLOTS
86 +# @INTERNAL
87 +# @DESCRIPTION:
88 +# Correct values of LLVM slots, newest first.
89 +declare -g -r _LLVM_KNOWN_SLOTS=( 5 4 )
90 +
91 +# @FUNCTION: get_llvm_prefix
92 +# @USAGE: [<max_slot>]
93 +# @DESCRIPTION:
94 +# Prints the absolute path to an LLVM install prefix corresponding to
95 +# the newest installed version of LLVM that is not newer than
96 +# <max_slot>. If no <max_slot> is specified, there is no upper limit.
97 +#
98 +# Note that the function does not support lower-bound version, so you
99 +# need to provide correct dependencies to ensure that a new enough
100 +# version will be always installed. Otherwise, the function could return
101 +# a version lower than required.
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 + local p=${EPREFIX}/usr/lib/llvm/${slot}
118 + if [[ -x ${p}/bin/llvm-config ]]; then
119 + echo "${p}"
120 + return
121 + fi
122 + done
123 +
124 + # max_slot should have been unset in the iteration
125 + if [[ -n ${max_slot} ]]; then
126 + die "${FUNCNAME}: invalid max_slot=${max_slot}"
127 + fi
128 +
129 + # fallback to :0
130 + # assume it's always <= 4 (the lower max_slot allowed)
131 + p=${EPREFIX}/usr
132 + if [[ -x ${p}/bin/llvm-config ]]; then
133 + echo "${p}"
134 + return
135 + fi
136 +
137 + die "No LLVM slot${1:+ <= ${1}} found in PATH!"
138 +}
139 +
140 +# @FUNCTION: llvm_pkg_setup
141 +# @DESCRIPTION:
142 +# Prepend the executable directory corresponding to the newest
143 +# installed LLVM version that is not newer than ${LLVM_MAX_SLOT}
144 +# to PATH. If LLVM_MAX_SLOT is unset or empty, the newest installed
145 +# slot will be used.
146 +#
147 +# The PATH manipulation is only done for source builds. The function
148 +# is a no-op when installing a binary package.
149 +#
150 +# If any other behavior is desired, the contents of the function
151 +# should be inlined into the ebuild and modified as necessary.
152 +llvm_pkg_setup() {
153 + debug-print-function ${FUNCNAME} "${@}"
154 +
155 + if [[ ${MERGE_TYPE} != binary ]]; then
156 + export PATH=$(get_llvm_prefix ${LLVM_MAX_SLOT})/bin:${PATH}
157 + fi
158 +}
159 +
160 +_LLVM_ECLASS=1
161 +fi
162 --
163 2.11.1