]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Use util::getenv_path_list to get PATH in find_executable_in_path
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 7 Nov 2024 20:54:35 +0000 (21:54 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 9 Nov 2024 12:59:05 +0000 (13:59 +0100)
This should improve support for non-ASCII paths in Windows.

src/ccache/execute.cpp
src/ccache/execute.hpp
src/ccache/hashutil.cpp

index fd6c7bdffef9366440fdcb46e9c2be88b1a20927..ce529ac230ee1e3873ba26d71d7f08f0ada0ac11 100644 (file)
@@ -26,6 +26,7 @@
 #include <ccache/signalhandler.hpp>
 #include <ccache/util/defer.hpp>
 #include <ccache/util/direntry.hpp>
+#include <ccache/util/environment.hpp>
 #include <ccache/util/error.hpp>
 #include <ccache/util/expected.hpp>
 #include <ccache/util/fd.hpp>
@@ -87,10 +88,14 @@ execute_noreturn(const char* const* argv, const fs::path& temp_dir)
 std::string
 win32getshell(const std::string& path)
 {
-  const char* path_list = getenv("PATH");
+  auto path_list = util::getenv_path_list("PATH");
+  if (path_list.empty()) {
+    return {};
+  }
+
   std::string sh;
-  if (util::to_lowercase(util::pstr(fs::path(path).extension()).str()) == ".sh"
-      && path_list) {
+  if (util::to_lowercase(util::pstr(fs::path(path).extension()).str())
+      == ".sh") {
     sh = util::pstr(find_executable_in_path("sh.exe", path_list));
   }
   if (sh.empty() && getenv("CCACHE_DETECT_SHEBANG")) {
@@ -99,7 +104,7 @@ win32getshell(const std::string& path)
     if (fp) {
       char buf[10] = {0};
       fgets(buf, sizeof(buf) - 1, fp.get());
-      if (std::string(buf) == "#!/bin/sh" && path_list) {
+      if (std::string(buf) == "#!/bin/sh") {
         sh = util::pstr(find_executable_in_path("sh.exe", path_list));
       }
     }
@@ -367,9 +372,9 @@ find_executable(const Context& ctx,
     return name;
   }
 
-  std::string path_list = ctx.config.path();
+  auto path_list = util::split_path_list(ctx.config.path());
   if (path_list.empty()) {
-    path_list = getenv("PATH");
+    path_list = util::getenv_path_list("PATH");
   }
   if (path_list.empty()) {
     LOG_RAW("No PATH variable");
@@ -381,7 +386,7 @@ find_executable(const Context& ctx,
 
 fs::path
 find_executable_in_path(const std::string& name,
-                        const std::string& path_list,
+                        const std::vector<fs::path>& path_list,
                         const std::optional<fs::path>& exclude_path)
 {
   if (path_list.empty()) {
@@ -393,7 +398,7 @@ find_executable_in_path(const std::string& name,
 
   // Search the path list looking for the first compiler of the right name that
   // isn't us.
-  for (const auto& dir : util::split_path_list(path_list)) {
+  for (const auto& dir : path_list) {
     const std::vector<fs::path> candidates = {
       dir / name,
 #ifdef _WIN32
index a3ef9c59c83600fdba4f16b6399ef97cff107c74..92940692eeca2253fe0afd7c536df2cfb23eb502 100644 (file)
@@ -23,6 +23,7 @@
 #include <filesystem>
 #include <optional>
 #include <string>
+#include <vector>
 
 class Context;
 
@@ -42,7 +43,7 @@ std::string find_executable(const Context& ctx,
 
 std::filesystem::path find_executable_in_path(
   const std::string& name,
-  const std::string& path_list,
+  const std::vector<std::filesystem::path>& path_list,
   const std::optional<std::filesystem::path>& exclude_path = std::nullopt);
 
 #ifdef _WIN32
index 4db8133c3ae8d507c5f7ea154276de66e9052876..880bedee5e6db92a9406d9770751e26123b56c50 100644 (file)
@@ -26,6 +26,7 @@
 #include <ccache/macroskip.hpp>
 #include <ccache/util/cpu.hpp>
 #include <ccache/util/direntry.hpp>
+#include <ccache/util/environment.hpp>
 #include <ccache/util/file.hpp>
 #include <ccache/util/filesystem.hpp>
 #include <ccache/util/format.hpp>
@@ -387,7 +388,8 @@ hash_command_output(Hash& hash,
   STARTUPINFO si;
   memset(&si, 0x00, sizeof(si));
 
-  auto path = find_executable_in_path(args[0], getenv("PATH")).string();
+  auto path =
+    find_executable_in_path(args[0], util::getenv_path_list("PATH")).string();
   if (path.empty()) {
     path = args[0];
   }