summaryrefslogtreecommitdiff
blob: 294e27b019b95f1b207800bc2e3a5ea3d49b2ef4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Copyright 1999-2017 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

# @ECLASS: llvm.eclass
# @MAINTAINER:
# Michał Górny <mgorny@gentoo.org>
# @AUTHOR:
# Michał Górny <mgorny@gentoo.org>
# @BLURB: Utility functions to build against slotted LLVM
# @DESCRIPTION:
# The llvm.eclass provides utility functions that can be used to build
# against specific version of slotted LLVM (with fallback to :0 for old
# versions).
#
# This eclass does not generate dependency strings. You need to write
# a proper dependency string yourself to guarantee that appropriate
# version of LLVM is installed.
#
# Example use for a package supporting LLVM 3.8 to 5:
# @CODE
# inherit cmake-utils llvm
#
# RDEPEND="
#	<sys-devel/llvm-6_rc:=
#	|| (
#		sys-devel/llvm:5
#		sys-devel/llvm:4
#		>=sys-devel/llvm-3.8:0
#	)
# "
#
# LLVM_MAX_SLOT=5
#
# # only if you need to define one explicitly
# pkg_setup() {
#	llvm_pkg_setup
#	do-something-else
# }
# @CODE

case "${EAPI:-0}" in
	0|1|2|3|4|5)
		die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
		;;
	6)
		;;
	*)
		die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
		;;
esac

EXPORT_FUNCTIONS pkg_setup

if [[ ! ${_LLVM_ECLASS} ]]; then

# @ECLASS-VARIABLE: LLVM_MAX_SLOT
# @DEFAULT_UNSET
# @DESCRIPTION:
# Highest LLVM slot supported by the package. Needs to be set before
# llvm_pkg_setup is called. If unset, no upper bound is assumed.

# @ECLASS-VARIABLE: _LLVM_KNOWN_SLOTS
# @INTERNAL
# @DESCRIPTION:
# Correct values of LLVM slots, newest first.
declare -g -r _LLVM_KNOWN_SLOTS=( 5 4 )

# @FUNCTION: get_llvm_prefix
# @USAGE: [<max_slot>]
# @DESCRIPTION:
# Prints the absolute path to an LLVM install prefix corresponding to
# the newest installed version of LLVM that is not newer than
# <max_slot>. If no <max_slot> is specified, there is no upper limit.
#
# Note that the function does not support lower-bound version, so you
# need to provide correct dependencies to ensure that a new enough
# version will be always installed. Otherwise, the function could return
# a version lower than required.
get_llvm_prefix() {
	debug-print-function ${FUNCNAME} "${@}"

	local max_slot=${1}
	local slot
	for slot in "${_LLVM_KNOWN_SLOTS[@]}"; do
		# skip higher slots
		if [[ -n ${max_slot} ]]; then
			if [[ ${max_slot} == ${slot} ]]; then
				max_slot=
			else
				continue
			fi
		fi

		local p=${EPREFIX}/usr/lib/llvm/${slot}
		if [[ -x ${p}/bin/llvm-config ]]; then
			echo "${p}"
			return
		fi
	done

	# max_slot should have been unset in the iteration
	if [[ -n ${max_slot} ]]; then
		die "${FUNCNAME}: invalid max_slot=${max_slot}"
	fi

	# fallback to :0
	# assume it's always <= 4 (the lower max_slot allowed)
	p=${EPREFIX}/usr
	if [[ -x ${p}/bin/llvm-config ]]; then
		echo "${p}"
		return
	fi

	die "No LLVM slot${1:+ <= ${1}} found in PATH!"
}

# @FUNCTION: llvm_pkg_setup
# @DESCRIPTION:
# Prepend the executable directory corresponding to the newest
# installed LLVM version that is not newer than ${LLVM_MAX_SLOT}
# to PATH. If LLVM_MAX_SLOT is unset or empty, the newest installed
# slot will be used.
#
# The PATH manipulation is only done for source builds. The function
# is a no-op when installing a binary package.
#
# If any other behavior is desired, the contents of the function
# should be inlined into the ebuild and modified as necessary.
llvm_pkg_setup() {
	debug-print-function ${FUNCNAME} "${@}"

	if [[ ${MERGE_TYPE} != binary ]]; then
		export PATH=$(get_llvm_prefix ${LLVM_MAX_SLOT})/bin:${PATH}
	fi
}

_LLVM_ECLASS=1
fi