From: Joel Rosdahl Date: Mon, 2 Oct 2023 16:32:56 +0000 (+0200) Subject: refactor: Move Win32Util::add_exe_suffix to util X-Git-Tag: v4.9~41 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=29e90bef9b1726bf9f34356fe5a5a88e4dbd2097;p=thirdparty%2Fccache.git refactor: Move Win32Util::add_exe_suffix to util --- diff --git a/src/Win32Util.cpp b/src/Win32Util.cpp index f1d4c25b8..340430b50 100644 --- a/src/Win32Util.cpp +++ b/src/Win32Util.cpp @@ -27,17 +27,6 @@ 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) { diff --git a/src/Win32Util.hpp b/src/Win32Util.hpp index 73112988b..0130e90b5 100644 --- a/src/Win32Util.hpp +++ b/src/Win32Util.hpp @@ -26,10 +26,6 @@ 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 diff --git a/src/ccache.cpp b/src/ccache.cpp index 3e1009b4f..d6e6a27cd 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -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 diff --git a/src/execute.cpp b/src/execute.cpp index 3eb22193d..2b23b54b2 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -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] { diff --git a/src/util/path.cpp b/src/util/path.cpp index ba2ad669c..29822ea3e 100644 --- a/src/util/path.cpp +++ b/src/util/path.cpp @@ -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) { diff --git a/src/util/path.hpp b/src/util/path.hpp index 02b790e47..6d43135e3 100644 --- a/src/util/path.hpp +++ b/src/util/path.hpp @@ -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 diff --git a/unittest/test_util_path.cpp b/unittest/test_util_path.cpp index 12ee2aa25..24d78558c 100644 --- a/unittest/test_util_path.cpp +++ b/unittest/test_util_path.cpp @@ -24,6 +24,14 @@ #include // 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