-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathp1689.cppm
More file actions
398 lines (362 loc) · 14.1 KB
/
p1689.cppm
File metadata and controls
398 lines (362 loc) · 14.1 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// mcpp.modgraph.p1689 — compiler-driven module dep scan via P1689r5 (.ddi).
//
// Replaces (under MCPP_SCANNER=p1689) the regex-based scanner. Each source
// file gets fed to:
//
// g++ <std-flag> -fmodules -fdeps-format=p1689r5 \
// -fdeps-file=<tmp>/x.ddi -fdeps-target=<tmp>/x.o \
// -M -MM -MF <tmp>/x.dep -E <source> -o <tmp>/x.o
//
// The resulting .ddi JSON describes the file's `provides` / `requires`
// directly from the compiler's frontend, removing any drift between
// our regex heuristic and what GCC actually sees.
//
// We hand-parse the .ddi (no nlohmann::json dep — same justification
// as fetcher.cppm).
//
// Spec: docs/27-p1689-dyndep.md.
module;
export module mcpp.modgraph.p1689;
import std;
import mcpp.modgraph.graph;
import mcpp.platform;
import mcpp.toolchain.detect;
export namespace mcpp::modgraph::p1689 {
struct DdiProvide {
std::string logicalName;
bool isInterface = false;
};
struct DdiRule {
std::string primaryOutput;
std::vector<DdiProvide> provides;
std::vector<std::string> requires_;
};
// Parse a .ddi JSON body into the first rule's info.
// Tolerates the slightly unstable GCC formatting (variable whitespace).
std::expected<DdiRule, std::string> parse_ddi(std::string_view body);
// Run g++ on `source` and return the populated SourceUnit.
// tmpDir : per-build scratch dir for .ddi/.dep/.o byproducts.
std::expected<SourceUnit, std::string>
scan_file(const std::filesystem::path& source,
const std::string& packageName,
const mcpp::toolchain::Toolchain& tc,
const std::filesystem::path& tmpDir,
const std::vector<std::filesystem::path>& includeDirs,
std::string_view cppStandardFlag);
} // namespace mcpp::modgraph::p1689
namespace mcpp::modgraph::p1689 {
namespace {
// --- Tiny JSON helpers (subset). ----------------------------------------
void skip_ws(std::string_view s, std::size_t& i) {
while (i < s.size() && (s[i] == ' ' || s[i] == '\t' ||
s[i] == '\n' || s[i] == '\r'))
++i;
}
// Parse a JSON string starting at s[i] which must point at '"'.
// On success returns the unescaped contents and advances i past the closing '"'.
std::expected<std::string, std::string>
parse_string(std::string_view s, std::size_t& i) {
if (i >= s.size() || s[i] != '"') {
return std::unexpected("expected '\"'");
}
++i;
std::string out;
while (i < s.size()) {
char c = s[i++];
if (c == '"') return out;
if (c == '\\') {
if (i >= s.size()) break;
char nc = s[i++];
switch (nc) {
case 'n': out.push_back('\n'); break;
case 't': out.push_back('\t'); break;
case 'r': out.push_back('\r'); break;
case '"': out.push_back('"'); break;
case '\\': out.push_back('\\'); break;
case '/': out.push_back('/'); break;
default: out.push_back(nc);
}
} else {
out.push_back(c);
}
}
return std::unexpected("unterminated string");
}
// Find a key at the *current depth* of the object starting at s[start]
// (which must be at '{'). Returns the index just after the matching ':'
// for the requested key, or npos.
std::size_t find_key_in_object(std::string_view s, std::size_t start,
std::string_view key)
{
if (start >= s.size() || s[start] != '{') return std::string_view::npos;
std::size_t i = start + 1;
int depth = 0;
while (i < s.size()) {
skip_ws(s, i);
if (i >= s.size()) break;
char c = s[i];
if (depth == 0 && c == '}') return std::string_view::npos;
if (c == '"') {
// String key candidate
std::size_t kStart = i;
std::string parsed;
auto pr = parse_string(s, i);
if (!pr) return std::string_view::npos;
parsed = *pr;
skip_ws(s, i);
if (i < s.size() && s[i] == ':') {
++i;
if (depth == 0 && parsed == key) {
skip_ws(s, i);
return i;
}
// Skip the value at this depth.
skip_ws(s, i);
// Skip a value: string / number / bool / null / object / array
if (i < s.size() && s[i] == '"') {
auto _ = parse_string(s, i);
} else if (i < s.size() && (s[i] == '{' || s[i] == '[')) {
char open = s[i];
char close = (open == '{') ? '}' : ']';
int d2 = 0;
bool in_str = false;
while (i < s.size()) {
char cc = s[i];
if (in_str) {
if (cc == '\\' && i + 1 < s.size()) { i += 2; continue; }
if (cc == '"') in_str = false;
++i; continue;
}
if (cc == '"') { in_str = true; ++i; continue; }
if (cc == open) ++d2;
else if (cc == close) { --d2; if (d2 == 0) { ++i; break; } }
++i;
}
} else {
// bare token
while (i < s.size() && s[i] != ',' && s[i] != '}' && s[i] != ']'
&& s[i] != ' ' && s[i] != '\n' && s[i] != '\r' && s[i] != '\t')
++i;
}
skip_ws(s, i);
if (i < s.size() && s[i] == ',') { ++i; }
continue;
}
(void)kStart;
continue;
}
// Stray separator
if (c == ',') { ++i; continue; }
++i;
// Defensive infinite-loop guard
(void)depth;
}
return std::string_view::npos;
}
} // namespace
std::expected<DdiRule, std::string> parse_ddi(std::string_view body) {
// Locate top-level "rules":[...]
std::size_t i = 0;
skip_ws(body, i);
if (i >= body.size() || body[i] != '{')
return std::unexpected("ddi: expected top-level object");
auto rulesAt = find_key_in_object(body, i, "rules");
if (rulesAt == std::string_view::npos)
return std::unexpected("ddi: missing 'rules'");
skip_ws(body, rulesAt);
if (rulesAt >= body.size() || body[rulesAt] != '[')
return std::unexpected("ddi: 'rules' is not an array");
// Step into the first rule object inside the array.
std::size_t j = rulesAt + 1;
skip_ws(body, j);
if (j >= body.size() || body[j] != '{')
return std::unexpected("ddi: 'rules' array empty or malformed");
DdiRule rule;
// primary-output
auto poAt = find_key_in_object(body, j, "primary-output");
if (poAt != std::string_view::npos) {
skip_ws(body, poAt);
if (poAt < body.size() && body[poAt] == '"') {
auto v = parse_string(body, poAt);
if (v) rule.primaryOutput = *v;
}
}
// provides: array of objects
auto provAt = find_key_in_object(body, j, "provides");
if (provAt != std::string_view::npos) {
skip_ws(body, provAt);
if (provAt < body.size() && body[provAt] == '[') {
std::size_t k = provAt + 1;
while (k < body.size()) {
skip_ws(body, k);
if (k >= body.size() || body[k] == ']') break;
if (body[k] == '{') {
DdiProvide p;
auto lnAt = find_key_in_object(body, k, "logical-name");
if (lnAt != std::string_view::npos) {
skip_ws(body, lnAt);
if (lnAt < body.size() && body[lnAt] == '"') {
auto v = parse_string(body, lnAt);
if (v) p.logicalName = *v;
}
}
auto isAt = find_key_in_object(body, k, "is-interface");
if (isAt != std::string_view::npos) {
skip_ws(body, isAt);
if (body.substr(isAt, 4) == "true") p.isInterface = true;
if (body.substr(isAt, 5) == "false") p.isInterface = false;
}
rule.provides.push_back(std::move(p));
}
// Skip past this object then comma.
int d2 = 0; bool in_str = false;
while (k < body.size()) {
char cc = body[k];
if (in_str) {
if (cc == '\\' && k + 1 < body.size()) { k += 2; continue; }
if (cc == '"') in_str = false;
++k; continue;
}
if (cc == '"') { in_str = true; ++k; continue; }
if (cc == '{') ++d2;
else if (cc == '}') { --d2; ++k; if (d2 == 0) break; continue; }
++k;
}
skip_ws(body, k);
if (k < body.size() && body[k] == ',') { ++k; continue; }
if (k < body.size() && body[k] == ']') break;
}
}
}
// requires: array of objects with logical-name
auto reqAt = find_key_in_object(body, j, "requires");
if (reqAt != std::string_view::npos) {
skip_ws(body, reqAt);
if (reqAt < body.size() && body[reqAt] == '[') {
std::size_t k = reqAt + 1;
while (k < body.size()) {
skip_ws(body, k);
if (k >= body.size() || body[k] == ']') break;
if (body[k] == '{') {
auto lnAt = find_key_in_object(body, k, "logical-name");
if (lnAt != std::string_view::npos) {
skip_ws(body, lnAt);
if (lnAt < body.size() && body[lnAt] == '"') {
auto v = parse_string(body, lnAt);
if (v) rule.requires_.push_back(*v);
}
}
}
int d2 = 0; bool in_str = false;
while (k < body.size()) {
char cc = body[k];
if (in_str) {
if (cc == '\\' && k + 1 < body.size()) { k += 2; continue; }
if (cc == '"') in_str = false;
++k; continue;
}
if (cc == '"') { in_str = true; ++k; continue; }
if (cc == '{') ++d2;
else if (cc == '}') { --d2; ++k; if (d2 == 0) break; continue; }
++k;
}
skip_ws(body, k);
if (k < body.size() && body[k] == ',') { ++k; continue; }
if (k < body.size() && body[k] == ']') break;
}
}
}
return rule;
}
namespace {
bool run_capturing(const std::string& cmd, std::string& out) {
auto r = mcpp::platform::process::capture(cmd);
out = r.output;
return r.exit_code == 0;
}
std::string shell_escape(const std::filesystem::path& p) {
std::string s = p.string();
std::string out = "'";
for (char c : s) {
if (c == '\'') out += "'\\''";
else out.push_back(c);
}
out += "'";
return out;
}
} // namespace
std::expected<SourceUnit, std::string>
scan_file(const std::filesystem::path& source,
const std::string& packageName,
const mcpp::toolchain::Toolchain& tc,
const std::filesystem::path& tmpDir,
const std::vector<std::filesystem::path>& includeDirs,
std::string_view cppStandardFlag)
{
std::error_code ec;
std::filesystem::create_directories(tmpDir, ec);
// One unique name per source — flat layout in tmpDir is fine since
// the caller gives us a fresh dir per build.
auto stem = source.filename().string();
auto base = tmpDir / std::format("{}_{}",
std::hash<std::string>{}(source.string()) % 1000000, stem);
auto ddi = base; ddi += ".ddi";
auto dep = base; dep += ".dep";
auto obj = base; obj += ".o";
std::string sysroot_flag;
if (!tc.sysroot.empty()) {
sysroot_flag = std::format(" --sysroot={}", shell_escape(tc.sysroot));
}
std::string std_flag = cppStandardFlag.empty()
? std::string("-std=c++23")
: std::string(cppStandardFlag);
std::string include_flags;
for (auto const& dir : includeDirs) {
include_flags += " -I";
include_flags += shell_escape(dir);
}
std::string cmd = std::format(
"{} {} -fmodules{}{}"
" -fdeps-format=p1689r5"
" -fdeps-file={}"
" -fdeps-target={}"
" -M -MM -MF {}"
" -E {} -o {} 2>&1",
shell_escape(tc.binaryPath),
std_flag,
sysroot_flag,
include_flags,
shell_escape(ddi),
shell_escape(obj),
shell_escape(dep),
shell_escape(source),
shell_escape(obj));
std::string output;
bool ok = run_capturing(cmd, output);
if (!ok || !std::filesystem::exists(ddi)) {
return std::unexpected(std::format(
"p1689 scan failed for '{}':\n{}", source.string(), output));
}
std::ifstream is(ddi);
std::string body((std::istreambuf_iterator<char>(is)), {});
auto rule = parse_ddi(body);
if (!rule) {
return std::unexpected(std::format(
"p1689 .ddi parse failed for '{}': {}", source.string(), rule.error()));
}
SourceUnit u;
u.path = source;
u.packageName = packageName;
if (!rule->provides.empty()) {
u.provides = ModuleId{ rule->provides.front().logicalName };
u.isModuleInterface = rule->provides.front().isInterface
|| source.extension() == ".cppm";
} else if (source.extension() == ".cpp" || source.extension() == ".cxx") {
u.isImplementation = true;
}
for (auto& r : rule->requires_) {
u.requires_.push_back(ModuleId{ r });
}
return u;
}
} // namespace mcpp::modgraph::p1689