]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
C++-ify, and clean up usage of, add_exe_ext_if_no_to_fullpath
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 29 Jul 2020 14:39:42 +0000 (16:39 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 29 Jul 2020 14:43:59 +0000 (16:43 +0200)
src/Win32Util.cpp
src/Win32Util.hpp
src/ccache.cpp
src/execute.cpp
src/execute.hpp

index 4af53abaa8ddbac103fef18e2f4ec4bbdaa5c5d3..ce22684a67fb50412da618f60504339253b5b79c 100644 (file)
 
 #include "Win32Util.hpp"
 
+#include "Util.hpp"
+
 namespace Win32Util {
 
+std::string
+add_exe_suffix(const std::string& path)
+{
+  auto ext = Util::to_lowercase(Util::get_extension(path));
+  if (ext == ".exe" || ext == ".bat" || ext == ".sh") {
+    return path;
+  } else {
+    return path + ".exe";
+  }
+}
+
 std::string
 error_message(DWORD error_code)
 {
index 7a5cfa75d87a1ee210a9e711a1b15a22d73301a1..a56387d1f02ef07e153d22ada4b6b0cbfbc14549 100644 (file)
 
 namespace Win32Util {
 
+// Add ".exe" suffix to `program` if it doesn't already end with ".exe", ".bat"
+// or ".sh".
+std::string add_exe_suffix(const std::string& program);
+
 // Recreate a Windows command line string based on `argv`. If `prefix` is
 // non-empty, add it as the first argument.
 std::string argv_to_string(const char* const* argv, const std::string& prefix);
index d535ade01bd94f7a9b239cab01c0ab521ff9f783..94c979785216a82e70f4ad2ca7c7c2e64c8b324d 100644 (file)
 #  include "third_party/getopt_long.h"
 #endif
 
+#ifdef _WIN32
+#  include "Win32Util.hpp"
+#endif
+
 #include <algorithm>
 #include <limits>
 
@@ -1201,22 +1205,18 @@ hash_common_info(const Context& ctx,
   hash.hash(ctx.config.cpp_extension().c_str());
 
 #ifdef _WIN32
-  const char* ext = strrchr(args[0].c_str(), '.');
-  char full_path_win_ext[MAX_PATH + 1] = {0};
-  add_exe_ext_if_no_to_fullpath(
-    full_path_win_ext, MAX_PATH, ext, args[0].c_str());
-  const char* full_path = full_path_win_ext;
+  const std::string compiler_path = Win32Util::add_exe_suffix(args[0]);
 #else
-  const char* full_path = args[0].c_str();
+  const std::string compiler_path = args[0];
 #endif
 
-  auto st = Stat::stat(full_path, Stat::OnError::log);
+  auto st = Stat::stat(compiler_path, Stat::OnError::log);
   if (!st) {
     failed(STATS_COMPILER);
   }
 
   // Hash information about the compiler.
-  hash_compiler(ctx, hash, st, args[0].c_str(), true);
+  hash_compiler(ctx, hash, st, compiler_path.c_str(), true);
 
   // Also hash the compiler name as some compilers use hard links and behave
   // differently depending on the real name.
index f97f60e5e4da2c5d1d7a7632d298686028c4244b..eacd20e5605193a7f3e193b1b4a3ed69fadd97fd 100644 (file)
@@ -65,21 +65,6 @@ win32getshell(const std::string& path)
   return sh;
 }
 
-void
-add_exe_ext_if_no_to_fullpath(char* full_path_win_ext,
-                              size_t max_size,
-                              const char* ext,
-                              const char* path)
-{
-  if (!ext
-      || (!str_eq(".exe", ext) && !str_eq(".sh", ext) && !str_eq(".bat", ext)
-          && !str_eq(".EXE", ext) && !str_eq(".BAT", ext))) {
-    snprintf(full_path_win_ext, max_size, "%s.exe", path);
-  } else {
-    snprintf(full_path_win_ext, max_size, "%s", path);
-  }
-}
-
 int
 win32execute(const char* path,
              const char* const* argv,
@@ -121,37 +106,26 @@ win32execute(const char* path,
   }
 
   std::string args = Win32Util::argv_to_string(argv, sh);
-  const char* ext = strrchr(path, '.');
-  char full_path_win_ext[MAX_PATH] = {0};
-  add_exe_ext_if_no_to_fullpath(full_path_win_ext, MAX_PATH, ext, path);
-  BOOL ret = FALSE;
+  std::string full_path = Win32Util::add_exe_suffix(path);
+  std::string tmp_file_path;
   if (args.length() > 8192) {
     TemporaryFile tmp_file(path);
     Util::write_fd(*tmp_file.fd, args.data(), args.length());
-    std::string atfile = fmt::format("\"@{}\"", tmp_file.path);
-    ret = CreateProcess(nullptr,
-                        const_cast<char*>(atfile.c_str()),
-                        nullptr,
-                        nullptr,
-                        1,
-                        0,
-                        nullptr,
-                        nullptr,
-                        &si,
-                        &pi);
-    Util::unlink_tmp(tmp_file.path);
+    args = fmt::format("\"@{}\"", tmp_file.path);
+    tmp_file_path = tmp_file.path;
   }
-  if (!ret) {
-    ret = CreateProcess(full_path_win_ext,
-                        const_cast<char*>(args.c_str()),
-                        nullptr,
-                        nullptr,
-                        1,
-                        0,
-                        nullptr,
-                        nullptr,
-                        &si,
-                        &pi);
+  BOOL ret = CreateProcess(full_path.c_str(),
+                           const_cast<char*>(args.c_str()),
+                           nullptr,
+                           nullptr,
+                           1,
+                           0,
+                           nullptr,
+                           nullptr,
+                           &si,
+                           &pi);
+  if (!tmp_file_path.empty()) {
+    Util::unlink_tmp(tmp_file_path);
   }
   if (fd_stdout != -1) {
     close(fd_stdout);
@@ -160,7 +134,7 @@ win32execute(const char* path,
   if (ret == 0) {
     DWORD error = GetLastError();
     cc_log("failed to execute %s: %s (%lu)",
-           full_path_win_ext,
+           full_path.c_str(),
            Win32Util::error_message(error).c_str(),
            error);
     return -1;
index bbcef5123c629aedc08f168dacc20b09ad53e0eb..9814a73c69a874179137c6a017426236b199a490 100644 (file)
@@ -45,8 +45,4 @@ int win32execute(const char* path,
                  int doreturn,
                  int fd_stdout,
                  int fd_stderr);
-void add_exe_ext_if_no_to_fullpath(char* full_path_win_ext,
-                                   size_t max_size,
-                                   const char* ext,
-                                   const char* path);
 #endif