From 73f6c52a4a788c5f158e18f3bfc09c54ebfefcf8 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Mon, 17 Jul 2023 18:46:27 +0200 Subject: [PATCH] refactor: Move Util::is_ccache_executable to ccache --- src/Util.cpp | 10 ---------- src/Util.hpp | 3 --- src/ccache.cpp | 16 +++++++++++++--- src/ccache.hpp | 1 + src/execute.cpp | 3 ++- unittest/test_Util.cpp | 15 --------------- unittest/test_ccache.cpp | 15 +++++++++++++++ 7 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/Util.cpp b/src/Util.cpp index 8118dc656..021114d85 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -353,16 +353,6 @@ is_absolute_path_with_prefix(std::string_view path) return std::nullopt; } -bool -is_ccache_executable(const std::string_view path) -{ - std::string name(Util::base_name(path)); -#ifdef _WIN32 - name = util::to_lowercase(name); -#endif - return util::starts_with(name, "ccache"); -} - std::string make_relative_path(const std::string& base_dir, const std::string& actual_cwd, diff --git a/src/Util.hpp b/src/Util.hpp index 755ab2b5e..f47d1d4c8 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -78,9 +78,6 @@ std::string get_relative_path(std::string_view dir, std::string_view path); // point. std::optional is_absolute_path_with_prefix(std::string_view path); -// Detmine if `path` refers to a ccache executable. -bool is_ccache_executable(std::string_view path); - // Return whether `ch` is a directory separator, i.e. '/' on POSIX systems and // '/' or '\\' on Windows systems. inline bool diff --git a/src/ccache.cpp b/src/ccache.cpp index 4a9882547..3112d795e 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -2166,7 +2166,7 @@ find_compiler(Context& ctx, throw core::Fatal(FMT("Could not find compiler \"{}\" in PATH", compiler)); } - if (Util::is_ccache_executable(resolved_compiler)) { + if (is_ccache_executable(resolved_compiler)) { throw core::Fatal("Recursive invocation of ccache"); } @@ -2311,7 +2311,7 @@ split_argv(int argc, const char* const* argv) { ArgvParts argv_parts; int i = 0; - while (i < argc && Util::is_ccache_executable(argv[i])) { + while (i < argc && is_ccache_executable(argv[i])) { argv_parts.masquerading_as_compiler = false; ++i; } @@ -2681,11 +2681,21 @@ do_cache_compilation(Context& ctx) return ctx.config.recache() ? Statistic::recache : Statistic::cache_miss; } +bool +is_ccache_executable(const std::string_view path) +{ + std::string name(Util::base_name(path)); +#ifdef _WIN32 + name = util::to_lowercase(name); +#endif + return util::starts_with(name, "ccache"); +} + int ccache_main(int argc, const char* const* argv) { try { - if (Util::is_ccache_executable(argv[0])) { + if (is_ccache_executable(argv[0])) { if (argc < 2) { PRINT_RAW(stderr, core::get_usage_text(Util::base_name(argv[0]))); exit(EXIT_FAILURE); diff --git a/src/ccache.hpp b/src/ccache.hpp index a3d0be77e..3d8072e6f 100644 --- a/src/ccache.hpp +++ b/src/ccache.hpp @@ -52,3 +52,4 @@ void find_compiler(Context& ctx, const FindExecutableFunction& find_executable_function, bool masquerading_as_compiler); CompilerType guess_compiler(std::string_view path); +bool is_ccache_executable(std::string_view path); diff --git a/src/execute.cpp b/src/execute.cpp index 27cb24ae1..44215c66e 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -29,6 +29,7 @@ #include "Util.hpp" #include "Win32Util.hpp" +#include #include #include #include @@ -399,7 +400,7 @@ find_executable_in_path(const std::string& name, if (candidate_exists) { const auto real_candidate = util::real_path(candidate); if ((real_exclude_path.empty() || real_candidate != real_exclude_path) - && !Util::is_ccache_executable(real_candidate)) { + && !is_ccache_executable(real_candidate)) { return candidate; } } diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index 2fe814c4d..d3879afef 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -207,21 +207,6 @@ TEST_CASE("Util::is_absolute_path_with_prefix") #endif } -TEST_CASE("Util::is_ccache_executable") -{ - CHECK(Util::is_ccache_executable("ccache")); - CHECK(Util::is_ccache_executable("ccache-1.2.3")); - CHECK(!Util::is_ccache_executable("fooccache")); - CHECK(!Util::is_ccache_executable("gcc")); -#ifdef _WIN32 - CHECK(Util::is_ccache_executable("CCACHE")); - CHECK(Util::is_ccache_executable("CCACHE.exe")); - CHECK(Util::is_ccache_executable("CCACHE-1.2.3")); - CHECK(Util::is_ccache_executable("CCACHE.EXE")); - CHECK(Util::is_ccache_executable("CCACHE-1.2.3.EXE")); -#endif -} - TEST_CASE("Util::is_dir_separator") { CHECK(!Util::is_dir_separator('x')); diff --git a/unittest/test_ccache.cpp b/unittest/test_ccache.cpp index bb4bf8860..cab8cf3d6 100644 --- a/unittest/test_ccache.cpp +++ b/unittest/test_ccache.cpp @@ -204,4 +204,19 @@ TEST_CASE("guess_compiler") #endif } +TEST_CASE("is_ccache_executable") +{ + CHECK(is_ccache_executable("ccache")); + CHECK(is_ccache_executable("ccache-1.2.3")); + CHECK(!is_ccache_executable("fooccache")); + CHECK(!is_ccache_executable("gcc")); +#ifdef _WIN32 + CHECK(is_ccache_executable("CCACHE")); + CHECK(is_ccache_executable("CCACHE.exe")); + CHECK(is_ccache_executable("CCACHE-1.2.3")); + CHECK(is_ccache_executable("CCACHE.EXE")); + CHECK(is_ccache_executable("CCACHE-1.2.3.EXE")); +#endif +} + TEST_SUITE_END(); -- 2.47.2