]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Move Win32Util::add_exe_suffix to util
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 2 Oct 2023 16:32:56 +0000 (18:32 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 2 Oct 2023 19:33:49 +0000 (21:33 +0200)
src/Win32Util.cpp
src/Win32Util.hpp
src/ccache.cpp
src/execute.cpp
src/util/path.cpp
src/util/path.hpp
unittest/test_util_path.cpp

index f1d4c25b8065d0e1c718b45581abc91979ce4a32..340430b50c2568a7daaf97055d3dd239e61f63ba 100644 (file)
 
 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 73112988b1cc7b241250b0159fe12e836a92a83a..0130e90b5e8dd8dff2c0adf0ac42e637a0e82a33 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. If `escape_backslashes` is true,
 // emit an additional backslash for each backslash that is not preceding '"' and
index 3e1009b4f720721d6b4947d548ee15a4995380c9..d6e6a27cdc1c4a697fd20863b2b57452442524f1 100644 (file)
@@ -1400,7 +1400,7 @@ hash_common_info(const Context& ctx,
   hash.hash(ctx.config.cpp_extension());
 
 #ifdef _WIN32
-  const std::string compiler_path = Win32Util::add_exe_suffix(args[0]);
+  const std::string compiler_path = util::add_exe_suffix(args[0]);
 #else
   const std::string compiler_path = args[0];
 #endif
index 3eb22193d625e0c406af130b818ba46ea7dbed76..2b23b54b26b83cf8ce579f0a5e970ad8e48d0ddf 100644 (file)
@@ -217,7 +217,7 @@ win32execute(const char* path,
   }
 
   std::string args = Win32Util::argv_to_string(argv, sh);
-  std::string full_path = Win32Util::add_exe_suffix(path);
+  std::string full_path = util::add_exe_suffix(path);
   fs::path tmp_file_path;
 
   util::Finalizer tmp_file_remover([&tmp_file_path] {
index ba2ad669c55796d3e34dec071fb238c48a063efe..29822ea3ed16e6f5d893c5313a2537b08002f784 100644 (file)
@@ -48,6 +48,17 @@ actual_cwd()
   return cwd_str;
 }
 
+std::string
+add_exe_suffix(const std::string& program)
+{
+  auto ext = util::to_lowercase(Util::get_extension(program));
+  if (ext == ".exe" || ext == ".bat" || ext == ".sh") {
+    return program;
+  } else {
+    return program + ".exe";
+  }
+}
+
 std::string
 apparent_cwd(const std::string& actual_cwd)
 {
index 02b790e475d9bd9ab9975bfd12392a0189fcf075..6d43135e3d86d5bab2126591767de5f526b58540 100644 (file)
@@ -32,6 +32,10 @@ namespace util {
 // normalized path without symlink parts). Returns the empty string on error.
 std::string actual_cwd();
 
+// 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);
+
 // Return current working directory (CWD) by reading the environment variable
 // PWD (thus keeping any symlink parts in the path and potentially ".." or "//"
 // parts). If PWD does not resolve to the same inode as `actual_cwd` then
index 12ee2aa25f552beb30d26a2d6a25245cae159303..24d78558c4d0485cec725ba48305378045de93b4 100644 (file)
 
 #include <ostream> // https://github.com/doctest/doctest/issues/618
 
+TEST_CASE("util::add_exe_suffix")
+{
+  CHECK(util::add_exe_suffix("foo") == "foo.exe");
+  CHECK(util::add_exe_suffix("foo.bat") == "foo.bat");
+  CHECK(util::add_exe_suffix("foo.exe") == "foo.exe");
+  CHECK(util::add_exe_suffix("foo.sh") == "foo.sh");
+}
+
 TEST_CASE("util::is_absolute_path")
 {
 #ifdef _WIN32