From: Joel Rosdahl Date: Sat, 6 Dec 2025 13:51:40 +0000 (+0100) Subject: enhance: Improve find_executable X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8558d0f8b75836c541186a97896a84b5eca42689;p=thirdparty%2Fccache.git enhance: Improve find_executable --- diff --git a/src/ccache/ccache.cpp b/src/ccache/ccache.cpp index 2906cfd6..99ccfe49 100644 --- a/src/ccache/ccache.cpp +++ b/src/ccache/ccache.cpp @@ -189,7 +189,8 @@ add_prefix(const Context& ctx, } if (!prefixes.empty() && !util::is_full_path(prefixes[0])) { - std::string path = find_executable(ctx, prefixes[0], ctx.orig_args[0]); + std::string path = + find_non_ccache_executable(ctx, prefixes[0], ctx.orig_args[0]); if (path.empty()) { throw core::Fatal(FMT("{}: {}", prefixes[0], strerror(errno))); } @@ -1693,7 +1694,8 @@ hash_nvcc_host_compiler(const Context& ctx, TRY(hash_compiler(ctx, hash, de, path, false)); } } else { - std::string path = find_executable(ctx, compiler, ctx.orig_args[0]); + std::string path = + find_non_ccache_executable(ctx, compiler, ctx.orig_args[0]); if (!path.empty()) { DirEntry de(path, DirEntry::LogOnError::yes); TRY(hash_compiler(ctx, hash, de, ccbin, false)); @@ -2782,7 +2784,7 @@ initialize(Context& ctx, const char* const* argv, bool masquerading_as_compiler) ctx.storage.initialize(); - find_compiler(ctx, &find_executable, masquerading_as_compiler); + find_compiler(ctx, &find_non_ccache_executable, masquerading_as_compiler); // Guess compiler after logging the config value in order to be able to // display "compiler_type = auto" before overwriting the value with the diff --git a/src/ccache/execute.cpp b/src/ccache/execute.cpp index 51a50e5e..7f71ad52 100644 --- a/src/ccache/execute.cpp +++ b/src/ccache/execute.cpp @@ -364,9 +364,9 @@ execute_noreturn(const char* const* argv, const fs::path& /*temp_dir*/) #endif std::string -find_executable(const Context& ctx, - const std::string& name, - const std::string& exclude_path) +find_non_ccache_executable(const Context& ctx, + const std::string& name, + const std::string& exclude_path) { if (fs::path(name).is_absolute()) { return name; @@ -381,13 +381,15 @@ find_executable(const Context& ctx, return {}; } - return find_executable_in_path(name, path_list, exclude_path).string(); + auto check = [](const fs::path& path) { return !is_ccache_executable(path); }; + return find_executable_in_path(name, path_list, exclude_path, check).string(); } fs::path -find_executable_in_path(const std::string& name, +find_executable_in_path(std::string_view name, const std::vector& path_list, - const std::optional& exclude_path) + const std::optional& exclude_path, + std::function extra_check) { if (path_list.empty()) { return {}; @@ -425,7 +427,7 @@ find_executable_in_path(const std::string& name, if (candidate_exists) { auto real_candidate = fs::canonical(candidate); if (real_candidate && *real_candidate != real_exclude_path - && !is_ccache_executable(*real_candidate)) { + && (!extra_check || extra_check(*real_candidate))) { return candidate; } } diff --git a/src/ccache/execute.hpp b/src/ccache/execute.hpp index 4be34427..35a8b75c 100644 --- a/src/ccache/execute.hpp +++ b/src/ccache/execute.hpp @@ -21,8 +21,10 @@ #include #include +#include #include #include +#include #include class Context; @@ -35,13 +37,14 @@ int execute(Context& ctx, void execute_noreturn(const char* const* argv, const std::filesystem::path& temp_dir); -// Find an executable named `name` in `$PATH`. Exclude any executables that are -// links to `exclude_path`. -std::string find_executable(const Context& ctx, - const std::string& name, - const std::string& exclude_path); +// Find an executable named `name` in `$PATH` that is not a ccache executable. +// Exclude any executables that are links to `exclude_path`. +std::string find_non_ccache_executable(const Context& ctx, + const std::string& name, + const std::string& exclude_path); std::filesystem::path find_executable_in_path( - const std::string& name, + std::string_view name, const std::vector& path_list, - const std::optional& exclude_path = std::nullopt); + const std::optional& exclude_path = std::nullopt, + std::function extra_check = {});