• [gentoo-dev] [PATCH 3/8] llvm-utils.eclass: Fix llvm_prepend_path to av

    From =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?@21:1/5 to All on Wed Feb 7 21:40:01 2024
    Fix llvm_prepend_path() not to append the new path multiple times,
    if the original PATH variable contained multiple LLVM directories.

    Thanks to @miller-alex 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 | 2 +-
    eclass/tests/llvm-utils.sh | 6 ++++++
    2 files changed, 7 insertions(+), 1 deletion(-)

    diff --git a/eclass/llvm-utils.eclass b/eclass/llvm-utils.eclass
    index f308667e3dc2..86933484fa94 100644
    --- a/eclass/llvm-utils.eclass
    +++ b/eclass/llvm-utils.eclass
    @@ -132,7 +132,7 @@ llvm_prepend_path() {
    # 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
    + if [[ ${x} != ${llvm_path} && ! ${added} ]]; then
    new_path+=( "${llvm_path}" )
    elif [[ ${added} && ${x} == ${llvm_path} ]]; then
    # deduplicate
    diff --git a/eclass/tests/llvm-utils.sh b/eclass/tests/llvm-utils.sh
    index 5a46b25b7ad6..c786ef04ddcd 100755
    --- a/eclass/tests/llvm-utils.sh
    +++ b/eclass/tests/llvm-utils.sh
    @@ -102,5 +102,11 @@ 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_pat
  • From =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?@21:1/5 to All on Thu Feb 8 20:10:02 2024
    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