Skip to content

Commit 2f6f07d

Browse files
authored
Add detecting real compiler type from symlink like cc (#592)
1 parent eed8712 commit 2f6f07d

File tree

6 files changed

+65
-17
lines changed

6 files changed

+65
-17
lines changed

server/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ file(GLOB ALL_SOURCES
114114
"${PROJECT_SOURCE_DIR}/src/streams/tests/*"
115115
"${PROJECT_SOURCE_DIR}/src/testgens/*"
116116
"${PROJECT_SOURCE_DIR}/src/utils/*"
117+
"${PROJECT_SOURCE_DIR}/src/utils/path/*"
117118
"${PROJECT_SOURCE_DIR}/src/utils/stats/*"
118119
"${PROJECT_SOURCE_DIR}/src/stubs/*"
119120
"${PROJECT_SOURCE_DIR}/src/visitors/*"

server/src/Paths.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,12 @@ namespace Paths {
416416
return removeSuffix(sourceFilePath, STUB_SUFFIX);
417417
}
418418

419+
//endregion
420+
419421
bool isHeadersEqual(const fs::path &srcPath, const fs::path &headerPath) {
420422
return removeSuffix(srcPath, STUB_SUFFIX).stem() == headerPath.stem();
421423
}
422424

423-
//endregion
424-
425425
const std::vector<std::string> CXXFileExtensions({".cc", ".cp", ".cpp", ".c++", ".cxx"});
426426
const std::vector<std::string> HPPFileExtensions({".hh", ".hpp", ".hxx"});
427427
const std::vector<std::string> CFileSourceExtensions({".c"});

server/src/utils/CompilationUtils.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,33 @@ namespace CompilationUtils {
2525
return compilationDatabase;
2626
}
2727

28-
CompilerName getCompilerName(const fs::path &compilerPath) {
29-
auto compiler = compilerPath.filename().string();
30-
if (StringUtils::contains(compiler, CLANGXX_PATH)) {
31-
return CompilerName::CLANGXX;
32-
}
33-
if (StringUtils::contains(compiler, CLANG_PATH)) {
34-
return CompilerName::CLANG;
35-
}
36-
if (StringUtils::contains(compiler, GXX_PATH_PATTERN)) {
37-
return CompilerName::GXX;
38-
}
39-
if (StringUtils::contains(compiler, GCC_PATH_PATTERN)) {
40-
return CompilerName::GCC;
28+
CompilerName getCompilerName(fs::path compilerPath) {
29+
CompilerName compilerName = CompilerName::UNKNOWN;
30+
while (compilerName == CompilerName::UNKNOWN) {
31+
std::string compiler = compilerPath.filename().string();
32+
if (StringUtils::contains(compiler, CLANGXX_PATH)) {
33+
return CompilerName::CLANGXX;
34+
}
35+
if (StringUtils::contains(compiler, CLANG_PATH)) {
36+
return CompilerName::CLANG;
37+
}
38+
if (StringUtils::contains(compiler, GXX_PATH_PATTERN)) {
39+
return CompilerName::GXX;
40+
}
41+
if (StringUtils::contains(compiler, GCC_PATH_PATTERN)) {
42+
return CompilerName::GCC;
43+
}
44+
compilerPath = fs::findInPATH(compilerPath);
45+
if (fs::is_symlink(compilerPath)) {
46+
std::error_code ec;
47+
compilerPath = fs::read_symlink(compilerPath, ec);
48+
if (!ec) {
49+
continue;
50+
}
51+
}
52+
break;
4153
}
42-
return CompilerName::UNKNOWN;
54+
return compilerName;
4355
}
4456

4557
std::string getBuildDirectoryName(CompilerName compilerName) {

server/src/utils/CompilationUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace CompilationUtils {
3838
std::shared_ptr<CompilationDatabase>
3939
getCompilationDatabase(const fs::path &buildCommandsJsonPath);
4040

41-
CompilerName getCompilerName(fs::path const &compilerPath);
41+
CompilerName getCompilerName(fs::path compilerPath);
4242

4343
fs::path substituteRemotePathToCompileCommandsJsonPath(const utbot::ProjectContext &projectContext);
4444

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "FileSystemPath.h"
2+
#include "utils/StringUtils.h"
3+
4+
namespace fs {
5+
path findInPATH(const path &p) {
6+
if (is_absolute(p)) {
7+
return p;
8+
}
9+
std::vector<std::string> pathENV = StringUtils::split(std::getenv("PATH"), ':');
10+
for (const std::string &pathFind: pathENV) {
11+
path fullPath = path(pathFind) / p;
12+
if (exists(fullPath)) {
13+
return fullPath;
14+
}
15+
}
16+
return p;
17+
}
18+
}

server/src/utils/path/FileSystemPath.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ namespace fs {
152152
friend bool exists( const path& p );
153153
friend bool is_empty( const path& p );
154154
friend bool is_directory( const path& p );
155+
friend bool is_symlink( const path& p );
156+
friend bool is_absolute( const path& p );
157+
friend path read_symlink( const path& p, std::error_code& ec);
155158
friend path relative( const path& p, const path& base);
156159
friend path canonical( const path& p );
157160
friend path weakly_canonical( const path& p );
@@ -258,6 +261,20 @@ namespace fs {
258261
return is_directory(p.path_);
259262
}
260263

264+
inline bool is_symlink(const path &p) {
265+
return is_symlink(p.path_);
266+
}
267+
268+
inline bool is_absolute(const path &p) {
269+
return p.path_.is_absolute();
270+
}
271+
272+
inline path read_symlink(const path &p, std::error_code &ec) {
273+
return read_symlink(p.path_, ec);
274+
}
275+
276+
path findInPATH(const path &p);
277+
261278
inline path relative( const path& p, const path& base) {
262279
return path(relative(p.path_, base.path_));
263280
}

0 commit comments

Comments
 (0)