-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependencyFile.cpp
More file actions
315 lines (248 loc) · 6.34 KB
/
Copy pathDependencyFile.cpp
File metadata and controls
315 lines (248 loc) · 6.34 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
*
* @file DependencyFile.cpp
* @author Gaspard Kirira
*
* Copyright 2026, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
* GCC/Clang dependency file parser
*
*/
#include <vix/engine/DependencyFile.hpp>
#include <algorithm>
#include <fstream>
#include <sstream>
namespace vix::engine
{
namespace
{
static std::string read_text_file_or_empty(const fs::path &path)
{
std::ifstream in(path);
if (!in)
return {};
std::ostringstream out;
out << in.rdbuf();
return out.str();
}
static bool is_space(char c)
{
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
}
static std::string join_continued_lines(const std::string &text)
{
std::string out;
out.reserve(text.size());
for (std::size_t i = 0; i < text.size(); ++i)
{
const char c = text[i];
if (c == '\\')
{
if (i + 1 < text.size() && text[i + 1] == '\n')
{
out.push_back(' ');
++i;
continue;
}
if (i + 2 < text.size() &&
text[i + 1] == '\r' &&
text[i + 2] == '\n')
{
out.push_back(' ');
i += 2;
continue;
}
}
out.push_back(c);
}
return out;
}
static std::vector<std::string> tokenize_make_rule(const std::string &line)
{
std::vector<std::string> tokens;
std::string current;
bool escaped = false;
for (char c : line)
{
if (escaped)
{
current.push_back(c);
escaped = false;
continue;
}
if (c == '\\')
{
escaped = true;
continue;
}
if (is_space(c))
{
if (!current.empty())
{
tokens.push_back(current);
current.clear();
}
continue;
}
current.push_back(c);
}
if (escaped)
current.push_back('\\');
if (!current.empty())
tokens.push_back(current);
return tokens;
}
static std::size_t find_unescaped_colon(const std::string &line)
{
bool escaped = false;
for (std::size_t i = 0; i < line.size(); ++i)
{
const char c = line[i];
if (escaped)
{
escaped = false;
continue;
}
if (c == '\\')
{
escaped = true;
continue;
}
if (c == ':')
return i;
}
return std::string::npos;
}
static std::string trim_copy(const std::string &value)
{
std::size_t begin = 0;
while (begin < value.size() && is_space(value[begin]))
++begin;
std::size_t end = value.size();
while (end > begin && is_space(value[end - 1]))
--end;
return value.substr(begin, end - begin);
}
static bool looks_like_phony_rule(
const std::string &left,
const std::string &right)
{
return !left.empty() && trim_copy(right).empty();
}
static fs::path unescape_make_path(const std::string &token)
{
std::string out;
out.reserve(token.size());
bool escaped = false;
for (char c : token)
{
if (escaped)
{
out.push_back(c);
escaped = false;
continue;
}
if (c == '\\')
{
escaped = true;
continue;
}
out.push_back(c);
}
if (escaped)
out.push_back('\\');
return fs::path(out);
}
} // namespace
bool DependencyFile::valid() const
{
return !target.empty() && !dependencies.empty();
}
bool DependencyFile::has_dependency(const fs::path &dependency) const
{
const fs::path normalized = normalize_dependency_path(dependency);
return std::find(
dependencies.begin(),
dependencies.end(),
normalized) != dependencies.end();
}
void DependencyFile::add_dependency(const fs::path &dependency)
{
if (dependency.empty())
return;
const fs::path normalized = normalize_dependency_path(dependency);
if (normalized.empty())
return;
if (has_dependency(normalized))
return;
dependencies.push_back(normalized);
}
fs::path normalize_dependency_path(const fs::path &path)
{
if (path.empty())
return {};
return path.lexically_normal();
}
std::optional<DependencyFile> parse_dependency_file_text(
const std::string &text,
const fs::path &dependencyFilePath)
{
const std::string flattened = join_continued_lines(text);
DependencyFile result;
result.path = dependencyFilePath;
std::istringstream in(flattened);
std::string line;
while (std::getline(in, line))
{
line = trim_copy(line);
if (line.empty())
continue;
const std::size_t colon = find_unescaped_colon(line);
if (colon == std::string::npos)
continue;
const std::string left = trim_copy(line.substr(0, colon));
const std::string right = line.substr(colon + 1);
if (left.empty())
continue;
if (looks_like_phony_rule(left, right))
continue;
if (result.target.empty())
{
const auto targets = tokenize_make_rule(left);
if (!targets.empty())
result.target = normalize_dependency_path(unescape_make_path(targets.front()));
}
const auto deps = tokenize_make_rule(right);
for (const auto &dep : deps)
{
const fs::path depPath = unescape_make_path(dep);
if (depPath.empty())
continue;
result.add_dependency(depPath);
}
}
if (!result.valid())
return std::nullopt;
return result;
}
std::optional<DependencyFile> read_dependency_file(const fs::path &path)
{
if (path.empty())
return std::nullopt;
const std::string text = read_text_file_or_empty(path);
if (text.empty())
return std::nullopt;
return parse_dependency_file_text(text, path);
}
fs::path dependency_file_for_object(const fs::path &objectPath)
{
fs::path out = objectPath;
out.replace_extension(".d");
return out;
}
} // namespace vix::engine