Possible Bug: Clang-FindBinUtils.cmake searches for clang-related binaries in an unexpected order

Hello,

I suspect that there is a bug in how Clang-FindBinUtils.cmake searches for clang-related executables (e.g. clang-scan-deps). The order of the search appears incorrect.

The search iterates over two categories:

  • first over all possible names (e.g. clang-scan-deps-20.1, clang-scan-deps-20, clang-scan-deps)
  • and then over all possible locations (directories)

The current implementation iterates over the names first, and then over the directories. I believe the order should be swapped.

Pseudo-code:

// Current behavior (suspicious/buggy)

for (each possible name) {
    for (each possible directory) {
        check_if_file_exists(join_path(directory, name));
    }
}

// Expected behavior

for (each possible directory) {
    for (each possible name) {
        check_if_file_exists(join_path(directory, name));
    }
}

Concrete example:

Let’s have the following clang binaries on a Linux system:

/usr/bin:
    clang++-20
    clang-scan-deps-20
    ...

/opt/my_own_clang_build/bin:
    clang++
    clang-scan-deps
    ...

Let’s assume CMAKE_CXX_COMPILER variable is set to /opt/my_own_clang_build/bin/clang++.

In such a case, Clang-FindBinUtils.cmake will search for name clang-scan-deps-20 earlier than for name clang-scan-deps. For that reason, the result of the search will be /usr/bin/clang-scan-deps-20, and not the expected /opt/my_own_clang_build/bin/clang-scan-deps.

Please let me know whether you agree that this is a bug, and whether the proposed fix looks all right to you.

I’d like to add that I observed this suspected bug on CMake 3.13.8 but the current master seems to be affected, too.

Thanks. I’ve opened CMake MR 12510 to fix this by adding the NAMES_PER_DIR option to the find_program calls for Clang companion tools.

Hi Brad, thank you quick reaction and preparing the fix. :+1:

(I missed the fact that find_program has the NAMES_PER_DIR option - which makes the fix easy.)