public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Michał Górny" <mgorny@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] repo/gentoo:master commit in: eclass/tests/, eclass/
Date: Sat, 10 Feb 2024 10:47:31 +0000 (UTC)	[thread overview]
Message-ID: <1707562040.511ce42e90eccd89a8d0d2ccbc239578d6af11ea.mgorny@gentoo> (raw)

commit:     511ce42e90eccd89a8d0d2ccbc239578d6af11ea
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Wed Feb  7 16:34:34 2024 +0000
Commit:     Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Sat Feb 10 10:47:20 2024 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=511ce42e

llvm-utils.eclass: Introduce an eclass for common helpers

Move some reusable functions from llvm.eclass to llvm-utils.eclass.
This is with minimal modifications so far (only argument checks were
cleaned up).

Signed-off-by: Michał Górny <mgorny <AT> gentoo.org>

 eclass/llvm-utils.eclass   | 115 +++++++++++++++++++++++++++++++++++++++++++++
 eclass/llvm.eclass         |  92 ++----------------------------------
 eclass/tests/llvm-utils.sh |  82 ++++++++++++++++++++++++++++++++
 3 files changed, 200 insertions(+), 89 deletions(-)

diff --git a/eclass/llvm-utils.eclass b/eclass/llvm-utils.eclass
new file mode 100644
index 000000000000..43988f6f88c7
--- /dev/null
+++ b/eclass/llvm-utils.eclass
@@ -0,0 +1,115 @@
+# Copyright 2024 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+# @ECLASS: llvm-utils.eclass
+# @MAINTAINER:
+# Michał Górny <mgorny@gentoo.org>
+# @AUTHOR:
+# Michał Górny <mgorny@gentoo.org>
+# @SUPPORTED_EAPIS: 7 8
+# @BLURB: Common utility functions for building against installed LLVM
+# @DESCRIPTION:
+# The utility eclass providing shared functions reused between
+# llvm.eclass and llvm-r1.eclass.  It may also be used directly
+# in ebuilds.
+
+case ${EAPI} in
+	7|8) ;;
+	*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
+esac
+
+if [[ ! ${_LLVM_UTILS_ECLASS} ]]; then
+_LLVM_UTILS_ECLASS=1
+
+# @FUNCTION: llvm_tuple_to_target
+# @USAGE: [<tuple>]
+# @DESCRIPTION:
+# Translate a tuple into a target suitable for LLVM_TARGETS.
+# Defaults to ${CHOST} if not specified.
+llvm_tuple_to_target() {
+	debug-print-function ${FUNCNAME} "${@}"
+
+	[[ ${#} -gt 1 ]] && die "Usage: ${FUNCNAME} [<tuple>]"
+
+	case ${1:-${CHOST}} in
+		aarch64*) echo "AArch64";;
+		amdgcn*) echo "AMDGPU";;
+		arc*) echo "ARC";;
+		arm*) echo "ARM";;
+		avr*) echo "AVR";;
+		bpf*) echo "BPF";;
+		csky*) echo "CSKY";;
+		loong*) echo "LoongArch";;
+		m68k*) echo "M68k";;
+		mips*) echo "Mips";;
+		msp430*) echo "MSP430";;
+		nvptx*) echo "NVPTX";;
+		powerpc*) echo "PowerPC";;
+		riscv*) echo "RISCV";;
+		sparc*) echo "Sparc";;
+		s390*) echo "SystemZ";;
+		x86_64*|i?86*) echo "X86";;
+		xtensa*) echo "Xtensa";;
+		*) die "Unknown LLVM target for tuple ${1:-${CHOST}}"
+	esac
+}
+
+# @FUNCTION: llvm_fix_clang_version
+# @USAGE: <variable-name>...
+# @DESCRIPTION:
+# Fix the clang compiler name in specified variables to include
+# the major version, to prevent PATH alterations from forcing an older
+# clang version being used.
+llvm_fix_clang_version() {
+	debug-print-function ${FUNCNAME} "${@}"
+
+	local shopt_save=$(shopt -p -o noglob)
+	set -f
+	local var
+	for var; do
+		local split=( ${!var} )
+		case ${split[0]} in
+			*clang|*clang++|*clang-cpp)
+				local version=()
+				read -r -a version < <("${split[0]}" --version)
+				local major=${version[-1]%%.*}
+				if [[ -n ${major//[0-9]} ]]; then
+					die "${var}=${!var} produced invalid --version: ${version[*]}"
+				fi
+
+				split[0]+=-${major}
+				if ! type -P "${split[0]}" &>/dev/null; then
+					die "${split[0]} does not seem to exist"
+				fi
+				declare -g "${var}=${split[*]}"
+				;;
+		esac
+	done
+	${shopt_save}
+}
+
+# @FUNCTION: llvm_fix_tool_path
+# @USAGE: <variable-name>...
+# @DESCRIPTION:
+# Fix the LLVM tools referenced in the specified variables to their
+# current location, to prevent PATH alterations from forcing older
+# versions being used.
+llvm_fix_tool_path() {
+	debug-print-function ${FUNCNAME} "${@}"
+
+	local shopt_save=$(shopt -p -o noglob)
+	set -f
+	local var
+	for var; do
+		local split=( ${!var} )
+		local path=$(type -P ${split[0]} 2>/dev/null)
+		# if it resides in one of the LLVM prefixes, it's an LLVM tool!
+		if [[ ${path} == "${BROOT}/usr/lib/llvm"* ]]; then
+			split[0]=${path}
+			declare -g "${var}=${split[*]}"
+		fi
+	done
+	${shopt_save}
+}
+
+fi

diff --git a/eclass/llvm.eclass b/eclass/llvm.eclass
index 91cc68d966fe..05ffcfd7cc6d 100644
--- a/eclass/llvm.eclass
+++ b/eclass/llvm.eclass
@@ -7,6 +7,7 @@
 # @AUTHOR:
 # Michał Górny <mgorny@gentoo.org>
 # @SUPPORTED_EAPIS: 7 8
+# @PROVIDES: llvm-utils
 # @BLURB: Utility functions to build against slotted LLVM
 # @DESCRIPTION:
 # The llvm.eclass provides utility functions that can be used to build
@@ -64,6 +65,8 @@ esac
 if [[ ! ${_LLVM_ECLASS} ]]; then
 _LLVM_ECLASS=1
 
+inherit llvm-utils
+
 # make sure that the versions installing straight into /usr/bin
 # are uninstalled
 DEPEND="!!sys-devel/llvm:0"
@@ -174,95 +177,6 @@ get_llvm_prefix() {
 	echo "${prefix}/usr/lib/llvm/$(get_llvm_slot "${@}")"
 }
 
-# @FUNCTION: llvm_tuple_to_target
-# @USAGE: [<tuple>]
-# @DESCRIPTION:
-# Translate a tuple into a target suitable for LLVM_TARGETS.
-# Defaults to ${CHOST} if not specified.
-llvm_tuple_to_target() {
-	debug-print-function ${FUNCNAME} "${@}"
-
-	case ${1:-${CHOST}} in
-		aarch64*) echo "AArch64";;
-		amdgcn*) echo "AMDGPU";;
-		arc*) echo "ARC";;
-		arm*) echo "ARM";;
-		avr*) echo "AVR";;
-		bpf*) echo "BPF";;
-		csky*) echo "CSKY";;
-		loong*) echo "LoongArch";;
-		m68k*) echo "M68k";;
-		mips*) echo "Mips";;
-		msp430*) echo "MSP430";;
-		nvptx*) echo "NVPTX";;
-		powerpc*) echo "PowerPC";;
-		riscv*) echo "RISCV";;
-		sparc*) echo "Sparc";;
-		s390*) echo "SystemZ";;
-		x86_64*|i?86*) echo "X86";;
-		xtensa*) echo "Xtensa";;
-		*) die "Unknown LLVM target for tuple ${1:-${CHOST}}"
-	esac
-}
-
-# @FUNCTION: llvm_fix_clang_version
-# @USAGE: <variable-name>...
-# @DESCRIPTION:
-# Fix the clang compiler name in specified variables to include
-# the major version, to prevent PATH alterations from forcing an older
-# clang version being used.
-llvm_fix_clang_version() {
-	debug-print-function ${FUNCNAME} "${@}"
-
-	local shopt_save=$(shopt -p -o noglob)
-	set -f
-	local var
-	for var; do
-		local split=( ${!var} )
-		case ${split[0]} in
-			*clang|*clang++|*clang-cpp)
-				local version=()
-				read -r -a version < <("${split[0]}" --version)
-				local major=${version[-1]%%.*}
-				if [[ -n ${major//[0-9]} ]]; then
-					die "${var}=${!var} produced invalid --version: ${version[*]}"
-				fi
-
-				split[0]+=-${major}
-				if ! type -P "${split[0]}" &>/dev/null; then
-					die "${split[0]} does not seem to exist"
-				fi
-				declare -g "${var}=${split[*]}"
-				;;
-		esac
-	done
-	${shopt_save}
-}
-
-# @FUNCTION: llvm_fix_tool_path
-# @USAGE: <variable-name>...
-# @DESCRIPTION:
-# Fix the LLVM tools referenced in the specified variables to their
-# current location, to prevent PATH alterations from forcing older
-# versions being used.
-llvm_fix_tool_path() {
-	debug-print-function ${FUNCNAME} "${@}"
-
-	local shopt_save=$(shopt -p -o noglob)
-	set -f
-	local var
-	for var; do
-		local split=( ${!var} )
-		local path=$(type -P ${split[0]} 2>/dev/null)
-		# if it resides in one of the LLVM prefixes, it's an LLVM tool!
-		if [[ ${path} == "${BROOT}/usr/lib/llvm"* ]]; then
-			split[0]=${path}
-			declare -g "${var}=${split[*]}"
-		fi
-	done
-	${shopt_save}
-}
-
 # @FUNCTION: llvm_pkg_setup
 # @DESCRIPTION:
 # Prepend the appropriate executable directory for the newest

diff --git a/eclass/tests/llvm-utils.sh b/eclass/tests/llvm-utils.sh
new file mode 100755
index 000000000000..44ad1b4adc84
--- /dev/null
+++ b/eclass/tests/llvm-utils.sh
@@ -0,0 +1,82 @@
+#!/bin/bash
+# Copyright 2024 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+source tests-common.sh || exit
+
+EAPI=8
+
+inherit llvm-utils
+
+test_fix_clang_version() {
+	local var=${1}
+	local tool=${2}
+	local version=${3}
+	local expected=${4}
+
+	eval "${tool}() {
+		cat <<-EOF
+			clang version ${version}
+			Target: x86_64-pc-linux-gnu
+			Thread model: posix
+			InstalledDir: /usr/lib/llvm/17/bin
+			Configuration file: /etc/clang/x86_64-pc-linux-gnu-clang.cfg
+		EOF
+	}"
+
+	declare -g ${var}=${tool}
+	tbegin "llvm_fix_clang_version ${var}=${tool} for ${version}"
+	llvm_fix_clang_version "${var}"
+	if [[ ${!var} != ${expected} ]]; then
+		eerror "llvm_fix_clang_version ${var}"
+		eerror "    gave: ${!var}"
+		eerror "expected: ${expected}"
+	fi
+	tend ${?}
+}
+
+test_fix_tool_path() {
+	local var=${1}
+	local tool=${2}
+	local expected_subst=${3}
+	local expected=${tool}
+
+	tbegin "llvm_fix_tool_path ${1}=${2} (from llvm? ${expected_subst})"
+
+	local matches=( "${BROOT}"/usr/lib/llvm/*/bin/"${tool}" )
+	if [[ ${expected_subst} == 1 ]]; then
+		if [[ ! -x ${matches[0]} ]]; then
+			ewarn "- skipping, test requires ${tool}"
+			return
+		fi
+
+		expected=${matches[0]}
+		local -x PATH=${matches[0]%/*}
+	else
+		local -x PATH=
+	fi
+
+	declare -g ${var}=${tool}
+	llvm_fix_tool_path "${var}"
+	if [[ ${!var} != ${expected} ]]; then
+		eerror "llvm_fix_tool_path ${var}"
+		eerror "    gave: ${!var}"
+		eerror "expected: ${expected}"
+	fi
+	tend ${?}
+}
+
+test_fix_clang_version CC clang 19.0.0git78b4e7c5 clang-19
+test_fix_clang_version CC clang 17.0.6 clang-17
+test_fix_clang_version CXX clang++ 17.0.6 clang++-17
+test_fix_clang_version CC x86_64-pc-linux-gnu-clang 17.0.6 \
+	x86_64-pc-linux-gnu-clang-17
+test_fix_clang_version CC clang-17 n/a clang-17
+test_fix_clang_version CC gcc n/a gcc
+
+test_fix_tool_path AR llvm-ar 1
+test_fix_tool_path RANLIB llvm-ranlib 1
+test_fix_tool_path AR ar 1
+test_fix_tool_path AR ar 0
+
+texit


             reply	other threads:[~2024-02-10 10:47 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-10 10:47 Michał Górny [this message]
  -- strict thread matches above, loose matches on Subject: below --
2025-03-03 19:27 [gentoo-commits] repo/gentoo:master commit in: eclass/tests/, eclass/ Sam James
2024-10-08 15:29 Ulrich Müller
2024-02-10 10:47 Michał Górny
2023-09-14  5:30 Michał Górny
2023-07-02 15:21 Michał Górny
2023-06-18 14:57 Michał Górny
2023-06-18 14:57 Michał Górny
2023-06-07  9:03 Ulrich Müller
2022-11-15 16:34 Michał Górny
2022-10-10 20:52 Michał Górny
2022-09-28 20:55 Michał Górny
2022-05-01  7:30 Michał Górny
2022-02-09  9:39 Michał Górny
2022-01-09  8:09 Michał Górny
2021-06-23 21:44 Michał Górny
2021-01-15 17:05 Michał Górny
2021-01-05 23:01 Sergei Trofimovich
2020-05-25  6:12 Michał Górny
2020-03-28 19:54 Sergei Trofimovich
2020-03-26  7:51 Sergei Trofimovich
2020-01-11 23:53 Sergei Trofimovich
2019-12-30 12:59 Michał Górny
2019-12-30 12:59 Michał Górny
2019-12-24 11:01 Sergei Trofimovich
2018-04-18 18:13 Mike Gilbert
2018-01-04 21:56 Michał Górny
2017-09-26 18:46 Ulrich Müller
2017-09-19 11:08 Michał Górny
2017-08-25 13:53 Michał Górny
2017-08-11 14:35 Michał Górny
2017-08-08 19:42 Michał Górny
2017-03-18  7:33 Michał Górny
2017-02-09 18:16 Mike Frysinger
2016-12-18 13:46 Michał Górny
2016-06-27  5:58 Michał Górny
2016-06-26 15:36 Michał Górny
2016-01-08  5:14 Michał Górny
2016-01-08  5:14 Michał Górny
2015-12-09 20:42 Michał Górny
2015-11-24 17:03 Mike Frysinger
2015-11-11 10:27 Michał Górny

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1707562040.511ce42e90eccd89a8d0d2ccbc239578d6af11ea.mgorny@gentoo \
    --to=mgorny@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox