Mailing List Archive

[PATCH] 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@gentoo.org>
---
eclass/llvm-utils.eclass | 10 ++++++----
eclass/tests/llvm-utils.sh | 12 ++++++++++++
2 files changed, 18 insertions(+), 4 deletions(-)

Changed in v2: simplified the logic, courtesy of Alexander Miller,
and added more test cases.

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
--
2.43.0