From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-1599122-garchives=archives.gentoo.org@lists.gentoo.org>
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 2CAF315815E
	for <garchives@archives.gentoo.org>; Sat, 10 Feb 2024 10:47:36 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id 585D2E2B21;
	Sat, 10 Feb 2024 10:47:35 +0000 (UTC)
Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits))
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id 37F52E2B21
	for <gentoo-commits@lists.gentoo.org>; Sat, 10 Feb 2024 10:47:35 +0000 (UTC)
Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits))
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id 404503431DB
	for <gentoo-commits@lists.gentoo.org>; Sat, 10 Feb 2024 10:47:34 +0000 (UTC)
Received: from localhost.localdomain (localhost [IPv6:::1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id E3B6114CA
	for <gentoo-commits@lists.gentoo.org>; Sat, 10 Feb 2024 10:47:31 +0000 (UTC)
From: "Michał Górny" <mgorny@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Michał Górny" <mgorny@gentoo.org>
Message-ID: <1707562041.5fcce58386dfb317020d7c88478480592b98b4e4.mgorny@gentoo>
Subject: [gentoo-commits] repo/gentoo:master commit in: eclass/, eclass/tests/
X-VCS-Repository: repo/gentoo
X-VCS-Files: eclass/llvm-utils.eclass eclass/tests/llvm-utils.sh
X-VCS-Directories: eclass/ eclass/tests/
X-VCS-Committer: mgorny
X-VCS-Committer-Name: Michał Górny
X-VCS-Revision: 5fcce58386dfb317020d7c88478480592b98b4e4
X-VCS-Branch: master
Date: Sat, 10 Feb 2024 10:47:31 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply
X-Archives-Salt: 33ce8a3d-423b-41f8-aa91-5a155e13e3bc
X-Archives-Hash: fd8cc37ac1da9f8ea186388afdba18f7

commit:     5fcce58386dfb317020d7c88478480592b98b4e4
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Wed Feb  7 16:39:18 2024 +0000
Commit:     Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Sat Feb 10 10:47:21 2024 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=5fcce583

llvm-utils.eclass: Fix llvm_prepend_path to avoid duplicates

Fix llvm_prepend_path() not to append the new path multiple times,
if the original PATH variable contained multiple LLVM directories.

Thanks to Alexander Miller who spotted it in:
https://github.com/gentoo/gentoo/pull/35196#discussion_r1480330001

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

 eclass/llvm-utils.eclass   | 10 ++++++----
 eclass/tests/llvm-utils.sh | 12 ++++++++++++
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/eclass/llvm-utils.eclass b/eclass/llvm-utils.eclass
index f308667e3dc2..532e609679b8 100644
--- a/eclass/llvm-utils.eclass
+++ b/eclass/llvm-utils.eclass
@@ -129,16 +129,18 @@ llvm_prepend_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
+			# prepend new path in front of the first LLVM version found
+			if [[ ! ${added} ]]; then
 				new_path+=( "${llvm_path}" )
-			elif [[ ${added} && ${x} == ${llvm_path} ]]; then
+				added=1
+			fi
+			# remove duplicate copies of the same path
+			if [[ ${x} == ${llvm_path} ]]; then
 				# deduplicate
 				continue
 			fi
-			added=1
 		fi
 		new_path+=( "${x}" )
 	done

diff --git a/eclass/tests/llvm-utils.sh b/eclass/tests/llvm-utils.sh
index 5a46b25b7ad6..6fe3da3eda13 100755
--- a/eclass/tests/llvm-utils.sh
+++ b/eclass/tests/llvm-utils.sh
@@ -98,9 +98,21 @@ 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 17 /usr/lib/llvm/17/bin:/usr/bin:/usr/lib/llvm/17/bin \
+	/usr/lib/llvm/17/bin:/usr/bin
+test_prepend_path 17 /usr/lib/llvm/17/bin:/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/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
+test_prepend_path 18 /usr/lib/llvm/17/bin:/usr/lib/llvm/16/bin:/usr/bin \
+	/usr/lib/llvm/18/bin:/usr/lib/llvm/17/bin:/usr/lib/llvm/16/bin:/usr/bin
+test_prepend_path 18 /usr/bin:/usr/lib/llvm/17/bin:/usr/lib/llvm/16/bin \
+	/usr/bin:/usr/lib/llvm/18/bin:/usr/lib/llvm/17/bin:/usr/lib/llvm/16/bin
+test_prepend_path 18 /usr/lib/llvm/17/bin:/usr/bin:/usr/lib/llvm/16/bin \
+	/usr/lib/llvm/18/bin:/usr/lib/llvm/17/bin:/usr/bin:/usr/lib/llvm/16/bin
 
 texit