-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSourceAnalyzer.cc
More file actions
85 lines (77 loc) · 2.94 KB
/
SourceAnalyzer.cc
File metadata and controls
85 lines (77 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "SourceAnalyzer.h"
#include "Utils.h"
#include <algorithm>
#include <filesystem>
#include <vector>
static auto ShouldCompile(
const std::string& output,
const std::vector<std::string>& dependencies) -> bool {
if (!std::filesystem::exists(output)) {
return true;
}
return std::any_of(
dependencies.begin(),
dependencies.end(),
[&](const auto& dependency) { return IsNewer(dependency, output); });
}
static auto GetDepfiles(
const std::string& compiler,
const std::string& flags,
const std::string& source) -> std::vector<std::string> {
const auto& command = JoinStrings({compiler, "-MM", source, flags});
const auto& output = RunCommand(command);
auto dependencies = RegexSplit(output, R"((\s)+(\\)*(\s)*)");
if (dependencies.size() <= 1) {
return {};
}
dependencies.erase(dependencies.begin());
return dependencies;
}
static auto BuildOutputPath(const std::string& workdir, const std::string& source) -> std::string {
auto filename = std::filesystem::path(source).filename();
return (std::filesystem::path(workdir) / filename).string() + ".o";
}
auto SourceAnalyzer::Process(const std::string& source) const -> SourceFile {
const auto& path = std::filesystem::path(source);
if (!path.has_extension()) {
return {};
}
const auto& extension = ToLower(path.extension().string());
const auto& iter = handlers_.find(extension);
if (iter == handlers_.end()) {
return {};
}
return (this->*(iter->second))(source);
}
auto SourceAnalyzer::ProcessC(const std::string& source) const -> SourceFile {
const auto& compiler = args_.at("cc");
const auto& flags = args_.at("cflags");
const auto& depfiles = GetDepfiles(compiler, flags, source);
auto output = BuildOutputPath(args_.at("workdir"), source);
std::string command;
if (ShouldCompile(output, depfiles)) {
command = JoinStrings({compiler, flags, "-o", output, "-c", source});
}
return {source, std::move(output), depfiles, std::move(command), Linker::ForC(compiler)};
}
auto SourceAnalyzer::ProcessCpp(const std::string& source) const -> SourceFile {
const auto& compiler = args_.at("cxx");
const auto& flags = args_.at("cxxflags");
const auto& depfiles = GetDepfiles(compiler, flags, source);
auto output = BuildOutputPath(args_.at("workdir"), source);
std::string command;
if (ShouldCompile(output, depfiles)) {
command = JoinStrings({compiler, flags, "-o", output, "-c", source});
}
return {source, std::move(output), depfiles, std::move(command), Linker::ForCpp(compiler)};
}
auto SourceAnalyzer::ProcessAsm(const std::string& source) const -> SourceFile {
const auto& compiler = args_.at("as");
const auto& flags = args_.at("asflags");
auto output = BuildOutputPath(args_.at("workdir"), source);
std::string command;
if (ShouldCompile(output, {source})) {
command = JoinStrings({compiler, flags, "-o", output, source});
}
return {source, std::move(output), {source}, std::move(command), Linker::ForAsm(compiler)};
}