]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Improve find_executable
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 6 Dec 2025 13:51:40 +0000 (14:51 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 7 Dec 2025 10:15:28 +0000 (11:15 +0100)
src/ccache/ccache.cpp
src/ccache/execute.cpp
src/ccache/execute.hpp

index 2906cfd63af4de13c84a05ba9cffc8fbe744d9d7..99ccfe49a7c4f040f2b8fa25bb90dfa560c5b2e0 100644 (file)
@@ -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
index 51a50e5ee12316bc63169e47538684a4c1a26e6e..7f71ad525b9857b740041e8a0d693b7493630014 100644 (file)
@@ -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<fs::path>& path_list,
-                        const std::optional<fs::path>& exclude_path)
+                        const std::optional<fs::path>& exclude_path,
+                        std::function<bool(const fs::path&)> 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;
         }
       }
index 4be34427aa639e07e26175631fef91592d9996d7..35a8b75ca8f517072caeb7824da833bd80e70677 100644 (file)
 #include <ccache/util/fd.hpp>
 
 #include <filesystem>
+#include <functional>
 #include <optional>
 #include <string>
+#include <string_view>
 #include <vector>
 
 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<std::filesystem::path>& path_list,
-  const std::optional<std::filesystem::path>& exclude_path = std::nullopt);
+  const std::optional<std::filesystem::path>& exclude_path = std::nullopt,
+  std::function<bool(const std::filesystem::path&)> extra_check = {});