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] llvm.eclass: An eclass to handle dependencies on slotted LLVM
Date: Sun, 29 Jan 2017 19:53:25
Message-Id: 20170129195227.17914-1-mgorny@gentoo.org
---
 eclass/llvm.eclass | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 98 insertions(+)
 create mode 100644 eclass/llvm.eclass

diff --git a/eclass/llvm.eclass b/eclass/llvm.eclass
new file mode 100644
index 000000000000..a0ce616d5b92
--- /dev/null
+++ b/eclass/llvm.eclass
@@ -0,0 +1,98 @@
+# Copyright 1999-2017 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+# $Id$
+
+# @ECLASS: llvm.eclass
+# @MAINTAINER:
+# Michał Górny <mgorny@g.o>
+# @AUTHOR:
+# Michał Górny <mgorny@g.o>
+# @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
+#	)
+# "
+#
+# src_configure() {
+#	local mycmakeargs=(
+#		-DLLVM_CONFIG="$(get_llvm_config 5)"
+#	)
+#	cmake-utils_src_configure
+# }
+# @CODE
+
+if [[ ! ${_LLVM_ECLASS} ]]; then
+
+# @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_config
+# @USAGE: [<max_slot>]
+# @DESCRIPTION:
+# Prints path to llvm-config executable for the newest matching version
+# of LLVM. If <max_slot> is provided, then no version newer than
+# the specified slot will be used. If it is not, the newest installed
+# version will be used.
+#
+# Note that the function does not support lower-bound version, so you
+# need to set proper dependencies. Otherwise, the function can return
+# a lower LLVM version than required.
+get_llvm_config() {
+	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}/bin/llvm-config
+		if [[ -x ${p} ]]; then
+			echo "${p}"
+			return
+		fi
+	done
+
+	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/bin/llvm-config
+	if [[ -x ${p} ]]; then
+		echo "${p}"
+		return
+	fi
+
+	die "No LLVM slot${max_slot:+ <= ${max_slot}} found in PATH!"
+}
+
+_LLVM_ECLASS=1
+fi
-- 
2.11.0

Replies