Skip to content

Commit 2d44072

Browse files
committed
fix: Detect compiler type for hard linked generic compiler name
This makes color diagnostics work on systems like Arch Linux where e.g. /usr/bin/c++ is a hard link to /usr/bin/g++. Fixes ccache#1514.
1 parent 51677af commit 2d44072

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/ccache/ccache.cpp

+28-2
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ init_hash_debug(Context& ctx,
261261
}
262262

263263
#ifndef _WIN32
264-
std::string
264+
265+
static fs::path
265266
follow_symlinks(const fs::path& path)
266267
{
267268
// Follow symlinks to the real compiler to learn its name. We're not using
@@ -279,8 +280,33 @@ follow_symlinks(const fs::path& path)
279280
p = p.parent_path() / *symlink_target;
280281
}
281282
}
283+
if (p != path) {
284+
LOG("Followed symlinks from {} to {} when guessing compiler type", path, p);
285+
}
282286
return p;
283287
}
288+
289+
static fs::path
290+
probe_generic_compiler(const fs::path& path)
291+
{
292+
// Detect whether a generically named compiler (e.g. /usr/bin/cc) is a hard
293+
// link to a compiler with a more specific name.
294+
std::string name = util::pstr(path.filename()).str();
295+
if (name == "cc" || name == "c++") {
296+
static const char* candidate_names[] = {"gcc", "g++", "clang", "clang++"};
297+
for (const char* candidate_name : candidate_names) {
298+
fs::path candidate = path.parent_path() / candidate_name;
299+
if (fs::equivalent(candidate, path)) {
300+
LOG("Detected that {} is equivalent to {} when guessing compiler type",
301+
path,
302+
candidate);
303+
return candidate;
304+
}
305+
}
306+
}
307+
return path;
308+
}
309+
284310
#endif
285311

286312
static CompilerType
@@ -314,7 +340,7 @@ guess_compiler(const fs::path& path)
314340
return type;
315341
#else
316342
if (type == CompilerType::other) {
317-
return do_guess_compiler(follow_symlinks(path));
343+
return do_guess_compiler(probe_generic_compiler(follow_symlinks(path)));
318344
} else {
319345
return type;
320346
}

0 commit comments

Comments
 (0)