From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 366FE15815E for ; Wed, 7 Feb 2024 20:36:07 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id BCF86E2A2A; Wed, 7 Feb 2024 20:35:26 +0000 (UTC) Received: from smtp.gentoo.org (mail.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 671DCE2A1F for ; Wed, 7 Feb 2024 20:35:26 +0000 (UTC) From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= To: gentoo-dev@lists.gentoo.org Cc: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Subject: [gentoo-dev] [PATCH 2/8] llvm-utils.eclass: Split out PATH prepending logic Date: Wed, 7 Feb 2024 21:11:37 +0100 Message-ID: <20240207203515.17640-3-mgorny@gentoo.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240207203515.17640-1-mgorny@gentoo.org> References: <20240207203515.17640-1-mgorny@gentoo.org> Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Archives-Salt: c97e1b5b-869b-480f-9de6-3be4d7d90123 X-Archives-Hash: 40b21db5844c4db9c215819b04ffa28c Split the logic prepending PATH from pkg_setup() into a dedicated llvm_prepend_path() function. Signed-off-by: Michał Górny --- eclass/llvm-utils.eclass | 36 ++++++++++++++++++++++++++++++++++++ eclass/llvm.eclass | 25 +------------------------ eclass/tests/llvm-utils.sh | 24 ++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 24 deletions(-) diff --git a/eclass/llvm-utils.eclass b/eclass/llvm-utils.eclass index 43988f6f88c7..f308667e3dc2 100644 --- a/eclass/llvm-utils.eclass +++ b/eclass/llvm-utils.eclass @@ -112,4 +112,40 @@ llvm_fix_tool_path() { ${shopt_save} } +# @FUNCTION: llvm_prepend_path +# @USAGE: +# @DESCRIPTION: +# Prepend the path to the specified LLVM slot to PATH variable, +# and reexport it. +llvm_prepend_path() { + debug-print-function ${FUNCNAME} "${@}" + + [[ ${#} -ne 1 ]] && die "Usage: ${FUNCNAME} " + local slot=${1} + + local llvm_path=${ESYSROOT}/usr/lib/llvm/${slot}/bin + local IFS=: + local split_path=( ${PATH} ) + local new_path=() + local x added= + + # prepend new path in front of the first LLVM version found + for x in "${split_path[@]}"; do + if [[ ${x} == */usr/lib/llvm/*/bin ]]; then + if [[ ${x} != ${llvm_path} ]]; then + new_path+=( "${llvm_path}" ) + elif [[ ${added} && ${x} == ${llvm_path} ]]; then + # deduplicate + continue + fi + added=1 + fi + new_path+=( "${x}" ) + done + # ...or to the end of PATH + [[ ${added} ]] || new_path+=( "${llvm_path}" ) + + export PATH=${new_path[*]} +} + fi diff --git a/eclass/llvm.eclass b/eclass/llvm.eclass index 05ffcfd7cc6d..e297fe992c9f 100644 --- a/eclass/llvm.eclass +++ b/eclass/llvm.eclass @@ -212,30 +212,7 @@ llvm_pkg_setup() { llvm_fix_tool_path LLVM_CONFIG fi - local prefix=${ESYSROOT} - local llvm_path=${prefix}/usr/lib/llvm/${LLVM_SLOT}/bin - local IFS=: - local split_path=( ${PATH} ) - local new_path=() - local x added= - - # prepend new path before first LLVM version found - for x in "${split_path[@]}"; do - if [[ ${x} == */usr/lib/llvm/*/bin ]]; then - if [[ ${x} != ${llvm_path} ]]; then - new_path+=( "${llvm_path}" ) - elif [[ ${added} && ${x} == ${llvm_path} ]]; then - # deduplicate - continue - fi - added=1 - fi - new_path+=( "${x}" ) - done - # ...or to the end of PATH - [[ ${added} ]] || new_path+=( "${llvm_path}" ) - - export PATH=${new_path[*]} + llvm_prepend_path "${LLVM_SLOT}" fi } diff --git a/eclass/tests/llvm-utils.sh b/eclass/tests/llvm-utils.sh index 44ad1b4adc84..5a46b25b7ad6 100755 --- a/eclass/tests/llvm-utils.sh +++ b/eclass/tests/llvm-utils.sh @@ -66,6 +66,21 @@ test_fix_tool_path() { tend ${?} } +test_prepend_path() { + local slot=${1} + local -x PATH=${2} + local expected=${3} + + tbegin "llvm_prepend_path ${slot} to PATH=${PATH}" + llvm_prepend_path "${slot}" + if [[ ${PATH} != ${expected} ]]; then + eerror "llvm_prepend_path ${var}" + eerror " gave: ${PATH}" + 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 @@ -79,4 +94,13 @@ test_fix_tool_path RANLIB llvm-ranlib 1 test_fix_tool_path AR ar 1 test_fix_tool_path AR ar 0 +ESYSROOT= +test_prepend_path 17 /usr/bin /usr/bin:/usr/lib/llvm/17/bin +test_prepend_path 17 /usr/lib/llvm/17/bin:/usr/bin /usr/lib/llvm/17/bin:/usr/bin +test_prepend_path 17 /usr/bin:/usr/lib/llvm/17/bin /usr/bin:/usr/lib/llvm/17/bin +test_prepend_path 18 /usr/lib/llvm/17/bin:/usr/bin \ + /usr/lib/llvm/18/bin:/usr/lib/llvm/17/bin:/usr/bin +test_prepend_path 18 /usr/bin:/usr/lib/llvm/17/bin \ + /usr/bin:/usr/lib/llvm/18/bin:/usr/lib/llvm/17/bin + texit -- 2.43.0