-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationSignature.cpp
More file actions
157 lines (130 loc) · 4.11 KB
/
Copy pathConfigurationSignature.cpp
File metadata and controls
157 lines (130 loc) · 4.11 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* @file ConfigurationSignature.cpp
*
* Configuration signature generation and persistence.
*/
#include <vix/engine/ConfigurationSignature.hpp>
#include <cctype>
#include <filesystem>
#include <fstream>
#include <optional>
#include <sstream>
#include <string>
#include <system_error>
namespace vix::engine
{
namespace
{
std::string trim(std::string value)
{
auto notSpace = [](unsigned char ch)
{
return !std::isspace(ch);
};
while (!value.empty() && !notSpace(static_cast<unsigned char>(value.front())))
value.erase(value.begin());
while (!value.empty() && !notSpace(static_cast<unsigned char>(value.back())))
value.pop_back();
return value;
}
std::string signature_join(
const std::vector<std::pair<std::string, std::string>> &variables)
{
std::ostringstream out;
for (const auto &variable : variables)
out << variable.first << "=" << variable.second << "\n";
return out.str();
}
} // namespace
std::string make_configuration_signature(
const ExecutionPlan &plan,
const ConfigurationSignatureOptions &options)
{
std::ostringstream oss;
oss << "preset=" << plan.preset.name << "\n";
oss << "generator=" << plan.preset.generator << "\n";
oss << "buildType=" << plan.preset.buildType << "\n";
oss << "static=" << (options.linkStatic ? "1" : "0") << "\n";
oss << "targetTriple=" << options.targetTriple << "\n";
oss << "sysroot=" << options.sysroot << "\n";
oss << "useCache=" << (options.useCache ? "1" : "0") << "\n";
oss << "warningCheck=" << (options.warningCheck ? "1" : "0") << "\n";
oss << "linker=" << static_cast<int>(options.linker) << "\n";
oss << "launcher=" << static_cast<int>(options.launcher) << "\n";
oss << "verbose=" << (options.verbose ? "1" : "0") << "\n";
oss << "cmakeVerbose=" << (options.cmakeVerbose ? "1" : "0") << "\n";
if (plan.launcher)
oss << "launcherTool=" << *plan.launcher << "\n";
if (plan.fastLinkerFlag)
oss << "linkerFlag=" << *plan.fastLinkerFlag << "\n";
oss << "projectFingerprint=" << plan.projectFingerprint << "\n";
oss << "vars:\n";
oss << signature_join(plan.cmakeVars);
oss << "rawCMakeArgs:\n";
for (const auto &arg : options.rawCMakeArgs)
oss << arg << "\n";
if (!options.targetTriple.empty())
{
oss << "toolchain:\n";
oss << options.toolchainContent;
if (!options.toolchainContent.empty() &&
options.toolchainContent.back() != '\n')
{
oss << "\n";
}
}
return trim(oss.str()) + "\n";
}
std::optional<std::string> read_configuration_signature(
const std::filesystem::path &signatureFile)
{
std::ifstream input(signatureFile, std::ios::binary);
if (!input)
return std::nullopt;
std::ostringstream buffer;
buffer << input.rdbuf();
if (!input.good() && !input.eof())
return std::nullopt;
return buffer.str();
}
bool write_configuration_signature(
const std::filesystem::path &signatureFile,
std::string_view signature)
{
std::error_code ec;
if (!signatureFile.parent_path().empty())
{
std::filesystem::create_directories(signatureFile.parent_path(), ec);
if (ec)
return false;
}
const std::filesystem::path tmp = signatureFile.string() + ".tmp";
{
std::ofstream output(tmp, std::ios::binary);
if (!output)
return false;
output.write(
signature.data(),
static_cast<std::streamsize>(signature.size()));
output.flush();
if (!output)
return false;
}
std::filesystem::rename(tmp, signatureFile, ec);
if (ec)
{
std::filesystem::remove(tmp, ec);
return false;
}
return true;
}
bool configuration_signature_matches(
const std::filesystem::path &signatureFile,
std::string_view expectedSignature)
{
const std::optional<std::string> existing =
read_configuration_signature(signatureFile);
return existing.has_value() &&
*existing == expectedSignature;
}
} // namespace vix::engine