]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Detect cc/c++ hard link to clang/clang++ before gcc/g++
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 7 Jun 2025 19:16:30 +0000 (21:16 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 8 Jun 2025 07:46:57 +0000 (09:46 +0200)
Apparently clang/clang++ can be gcc/g++ in addition to cc/c++, at least
on some macOS versions, so detect clang hard link before gcc since the
other way around (gcc installed as an alias of clang) seems less likely.

Fixes #1597.

src/ccache/ccache.cpp
unittest/test_ccache.cpp

index 8c0bce3b5ef1b1f2963398cc81209cb8272a1087..d727ba81f44ffd55c70fd2b19439b856a3d0a23a 100644 (file)
@@ -295,7 +295,7 @@ probe_generic_compiler(const fs::path& path)
   // link to a compiler with a more specific name.
   std::string name = util::pstr(path.filename()).str();
   if (name == "cc" || name == "c++") {
-    static const char* candidate_names[] = {"gcc", "g++", "clang", "clang++"};
+    static const char* candidate_names[] = {"clang", "clang++", "gcc", "g++"};
     for (const char* candidate_name : candidate_names) {
       fs::path candidate = path.parent_path() / candidate_name;
       if (fs::equivalent(candidate, path)) {
index 69770cb22904d47b2ef93fd02b2cda2d1723b91c..fcf7326e4b2ee7ed82bedf6d7d0ecb3d8c7f4cd3 100644 (file)
@@ -223,15 +223,40 @@ TEST_CASE("guess_compiler")
     CHECK(guess_compiler(clang_cl) == CompilerType::clang_cl);
   }
 
-  SUBCASE("Probe hardlink for actual compiler")
+  SUBCASE("Probe hardlink for actual compiler, gcc")
   {
     const auto cwd = *fs::current_path();
-    util::write_file(cwd / "gcc", "");
     const auto cc = cwd / "cc";
-    CHECK(fs::create_hard_link("gcc", cc));
+    const auto gcc = cwd / "gcc";
+    util::write_file(cwd / "cc", "");
+    CHECK(fs::create_hard_link(cc, gcc));
 
     CHECK(guess_compiler(cc) == CompilerType::gcc);
   }
+
+  SUBCASE("Probe hardlink for actual compiler, clang")
+  {
+    const auto cwd = *fs::current_path();
+    const auto cc = cwd / "cc";
+    const auto clang = cwd / "clang";
+    util::write_file(cwd / "cc", "");
+    CHECK(fs::create_hard_link(cc, clang));
+
+    CHECK(guess_compiler(cc) == CompilerType::clang);
+  }
+
+  SUBCASE("Probe hardlink for actual compiler, gcc+clang")
+  {
+    const auto cwd = *fs::current_path();
+    const auto cc = cwd / "cc";
+    const auto gcc = cwd / "gcc";
+    const auto clang = cwd / "clang";
+    util::write_file(cwd / "cc", "");
+    CHECK(fs::create_hard_link(cc, gcc));
+    CHECK(fs::create_hard_link(cc, clang));
+
+    CHECK(guess_compiler(cc) == CompilerType::clang);
+  }
 #endif
 }