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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
|
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0
#include <algorithm>
#include <array>
#include <cctype>
#include <map>
#include <set>
#include <utility>
#include <vector>
#include "tools/Amalgamation.hpp"
namespace Progmasoft::Catch3::Tools
{
namespace
{
namespace fs = std::filesystem;
constexpr std::string_view kHeaderHead = R"(
// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
)";
constexpr std::string_view kHeaderTail
= R"(// ----------------------------------------------------------
// This file is an amalgamation of multiple different files.
// You probably shouldn't edit it directly.
//
// The parts that come from src/Progmasoft, the Catch3 runner, keep
// their own copyright and license lines (MPL-2.0 WITH
// AdditionRef-Progmasoft-Exception-2.0). The runner needs C++17.
// ----------------------------------------------------------
)";
constexpr std::string_view kThirdPartyTail
= R"(// ----------------------------------------------------------
// This file is an amalgamation of src/Progmasoft/Runner/Yyjson.inl,
// src/Progmasoft/PugiXml.inl and the libraries they include from
// third_party/. You probably shouldn't edit it directly.
//
// yyjson and pugixml keep their MIT licenses, stated in their text below;
// the rest is MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0. Only
// catch_amalgamated.cpp includes this file.
// ----------------------------------------------------------
)";
// The comments at the top of each engine file.
constexpr std::array<std::string_view, 5> kCopyrightLines{
"// Copyright Catch2 Authors\n",
"// Distributed under the Boost Software License, Version 1.0.\n",
"// (See accompanying file LICENSE.txt or copy at\n",
"// https://www.boost.org/LICENSE_1_0.txt)\n",
"// SPDX-License-Identifier: BSL-1.0\n",
};
constexpr std::string_view kUserConfig
= "Progmasoft/Catch3/UserConfig.hpp";
const std::map<std::string, std::string> &
ThirdPartyFiles()
{
static const auto *files = new std::map<std::string, std::string>{
{ "yyjson.c", "yyjson/yyjson.c" },
{ "yyjson.h", "yyjson/yyjson.h" },
{ "pugixml.cpp", "pugixml/pugixml.cpp" },
{ "pugixml.hpp", "pugixml/pugixml.hpp" },
{ "pugiconfig.hpp", "pugixml/pugiconfig.hpp" },
};
return *files;
}
[[nodiscard]] bool
Consists(std::string_view text, std::string_view extra)
{
return std::all_of(text.begin(), text.end(), [extra](char c) {
return std::isalpha(static_cast<unsigned char>(c)) != 0
|| extra.find(c) != std::string_view::npos;
});
}
[[nodiscard]] bool
IsPragmaOnce(std::string_view line)
{
return Trimmed(line) == "#pragma once";
}
/// A runner's include: `<Progmasoft/...>` or, inside one directory,
/// `"Name.hpp"`.
[[nodiscard]] std::optional<IncludedFile>
ParseRunnerInclude(std::string_view line)
{
auto include = ParseInclude(line);
if (!include)
{
return std::nullopt;
}
const bool found = include->Angled
? StartsWith(include->Name, "Progmasoft/")
: EndsWith(include->Name, ".hpp")
&& Consists(include->Name, "/.");
return found ? include : std::nullopt;
}
/// The runner compiles yyjson and pugixml through these files.
[[nodiscard]] bool
IsWrapperInclude(std::string_view line)
{
const auto include = ParseInclude(line);
return include && !include->Angled
&& EndsWith(include->Name, ".inl")
&& Consists(include->Name, "./");
}
/// An include of yyjson and pugixml, which space them in many ways.
[[nodiscard]] std::optional<std::string>
ParseThirdPartyInclude(std::string_view line)
{
const auto skipSpace = [&line] {
const auto first = line.find_first_not_of(" \t");
line.remove_prefix(first == std::string_view::npos ? line.size()
: first);
};
skipSpace();
if (!StartsWith(line, "#"))
{
return std::nullopt;
}
line.remove_prefix(1);
skipSpace();
if (!StartsWith(line, "include"))
{
return std::nullopt;
}
line.remove_prefix(7);
const auto before = line.size();
skipSpace();
if (line.size() == before || line.empty()
|| (line.front() != '<' && line.front() != '"'))
{
return std::nullopt;
}
line.remove_prefix(1);
const auto end = line.find_first_of(">\"");
if (end == std::string_view::npos || end == 0
|| !Consists(line.substr(0, end), "."))
{
return std::nullopt;
}
return std::string{ line.substr(0, end) };
}
class Generator final
{
public:
Generator(const fs::path &repository, Problems &found)
: root(repository)
, source(repository / "src")
, thirdParty(repository / "third_party")
, problems(found)
, upstream(UpstreamFiles(repository, found))
{}
[[nodiscard]] std::string
Header()
{
std::string out = FileHeader();
out += "#ifndef CATCH_AMALGAMATED_HPP_INCLUDED\n";
out += "#define CATCH_AMALGAMATED_HPP_INCLUDED\n";
Concatenate(out, source / "Progmasoft/Catch3/All.hpp", true);
// Mocks are Progmasoft's own, and tests use them through
// this header.
Concatenate(out, source / "Progmasoft/Catch3/Mock.hpp", true);
out += "#endif // CATCH_AMALGAMATED_HPP_INCLUDED\n";
return out;
}
[[nodiscard]] std::string
Source()
{
std::string out = FileHeader();
out += "\n#include \"catch_amalgamated.hpp\"\n";
Concatenate(
out,
source / "Progmasoft/Catch3/Internal/WindowsHProxy.hpp",
false);
// The runner's headers come first: Catch::Session reaches
// the runner.
auto headers = FilesBelow(root,
source / "Progmasoft/Runner",
{ ".hpp" },
false);
for (const char *name : { "Results.hpp",
"Runner.hpp",
"Version.hpp",
"XmlWriter.hpp" })
{
headers.push_back(std::string{ "src/Progmasoft/Catch3/" }
+ name);
}
for (const auto &header : headers)
{
const auto path = Normal(root / header);
if (runnerHeaders.insert(path).second)
{
ConcatenateRunner(out, path, true);
}
}
for (const auto &file : upstream)
{
if (EndsWith(file, ".cpp"))
{
Concatenate(out, root / file, false);
}
}
std::vector<std::string> files{
"src/Progmasoft/Mock.cpp",
"src/Progmasoft/Results.cpp",
"src/Progmasoft/XmlWriter.cpp"
};
for (auto &file : FilesBelow(root,
source / "Progmasoft/Runner",
{ ".cpp" },
false))
{
files.push_back(std::move(file));
}
for (const auto &file : files)
{
ConcatenateRunner(out, root / file, false);
}
return out;
}
[[nodiscard]] std::string
ThirdParty()
{
std::string out
= "// Catch3 v" + Catch3Version()
+ ": yyjson and pugixml for catch_amalgamated.cpp\n";
out += kThirdPartyTail;
out += "#pragma once\n";
std::set<std::string> done;
for (const char *wrapper :
{ "Runner/Yyjson.inl", "PugiXml.inl" })
{
const auto text
= ReadOrReport(source / "Progmasoft" / wrapper,
problems);
for (const auto &line : SplitLines(text))
{
if (StartsWith(line, "// Copyright")
|| StartsWith(line, "// SPDX-License-Identifier"))
{
continue;
}
const auto name = ParseThirdPartyInclude(line);
if (name && ThirdPartyFiles().count(*name) != 0)
{
ExpandThirdParty(out, *name, done);
continue;
}
out += line;
}
}
return out;
}
private:
[[nodiscard]] static std::string
Normal(const fs::path &path)
{
return path.lexically_normal().generic_string();
}
[[nodiscard]] std::string
Catch3Version()
{
const auto text
= ReadOrReport(source / "Progmasoft/Catch3/Version.hpp",
problems);
constexpr std::string_view kKey = "kVersion = \"";
const auto start = text.find(kKey);
if (start == std::string::npos)
{
problems.emplace_back("no kVersion in Version.hpp");
return {};
}
const auto first = start + kKey.size();
return text.substr(first, text.find('"', first) - first);
}
[[nodiscard]] std::string
FileHeader()
{
const auto engine = ParseEngineVersion(
ReadOrReport(source / "Progmasoft/Catch3/EngineVersion.cpp",
problems));
if (!engine)
{
problems.emplace_back("no version in EngineVersion.cpp");
}
std::string out{ kHeaderHead };
out += "// Catch3 v" + Catch3Version() + ", on the Catch2 v"
+ engine.value_or("") + " engine\n";
out += kHeaderTail;
return out;
}
/// The engine header a line includes, or nothing.
[[nodiscard]] std::optional<std::string>
EngineInclude(std::string_view line) const
{
const auto include = ParseInclude(line);
if (!include || !include->Angled
|| !StartsWith(include->Name, "Progmasoft/Catch3/"))
{
return std::nullopt;
}
if (upstream.count("src/" + include->Name) != 0
|| include->Name == kUserConfig)
{
return include->Name;
}
return std::nullopt;
}
/// Copies a file, leaving out copyright lines; with
/// `expandHeaders`, engine headers are put in place of their
/// includes, once each.
void
Concatenate(std::string &out,
const fs::path &file,
bool expandHeaders)
{
const auto text = ReadOrReport(file, problems);
for (const auto &line : SplitLines(text))
{
if (IsPragmaOnce(line)
|| std::find(kCopyrightLines.begin(),
kCopyrightLines.end(),
line)
!= kCopyrightLines.end())
{
continue;
}
const auto engine = EngineInclude(line);
if (!engine)
{
// The runner's headers are expanded at the start of
// the .cpp file.
if (expandHeaders || !ParseRunnerInclude(line))
{
out += line;
}
continue;
}
if (!expandHeaders
|| !concatenatedHeaders.insert(*engine).second)
{
continue;
}
// The generated configuration does not exist yet, and
// leaving it out means the default one.
if (*engine != kUserConfig)
{
Concatenate(out, source / *engine, true);
}
}
}
/// Copies a file of the runner, expanding the runner's headers it
/// includes once, or dropping those includes from .cpp files.
void
ConcatenateRunner(std::string &out,
const fs::path &file,
bool expandHeaders)
{
const auto directory = file.parent_path();
const auto text = ReadOrReport(file, problems);
for (const auto &line : SplitLines(text))
{
if (IsPragmaOnce(line) || EngineInclude(line))
{
continue;
}
if (IsWrapperInclude(line))
{
if (!thirdPartyIncluded)
{
out += "#include "
"\"catch_amalgamated_third_party.hpp\"\n";
thirdPartyIncluded = true;
}
continue;
}
const auto include = ParseRunnerInclude(line);
if (!include)
{
out += line;
continue;
}
const auto header = Normal(
(include->Angled ? source : directory) / include->Name);
if (expandHeaders && runnerHeaders.insert(header).second)
{
ConcatenateRunner(out, header, true);
}
}
}
void
ExpandThirdParty(std::string &out,
const std::string &name,
std::set<std::string> &done)
{
if (!done.insert(name).second)
{
return;
}
const auto text
= ReadOrReport(thirdParty / ThirdPartyFiles().at(name),
problems);
for (const auto &line : SplitLines(text))
{
auto included = ParseThirdPartyInclude(line);
// pugixml includes its source through a macro in
// header-only mode.
if (Trimmed(line) == "#\tinclude PUGIXML_SOURCE")
{
included = "pugixml.cpp";
}
if (included && ThirdPartyFiles().count(*included) != 0)
{
ExpandThirdParty(out, *included, done);
}
else
{
out += line;
}
}
}
fs::path root;
fs::path source;
fs::path thirdParty;
Problems &problems;
std::set<std::string> upstream;
std::set<std::string> concatenatedHeaders;
std::set<std::string> runnerHeaders;
bool thirdPartyIncluded = false;
};
[[nodiscard]] std::array<std::pair<const char *, const std::string *>,
3>
Parts(const Amalgamation &amalgamation)
{
return { { { "catch_amalgamated.hpp", &amalgamation.Header },
{ "catch_amalgamated.cpp", &amalgamation.Source },
{ "catch_amalgamated_third_party.hpp",
&amalgamation.ThirdParty } } };
}
} // namespace
std::optional<IncludedFile>
ParseInclude(std::string_view line)
{
const auto first = line.find_first_not_of(" \t");
if (first == std::string_view::npos)
{
return std::nullopt;
}
line.remove_prefix(first);
constexpr std::string_view kInclude = "#include ";
if (!StartsWith(line, kInclude) || line.size() <= kInclude.size())
{
return std::nullopt;
}
line.remove_prefix(kInclude.size());
const char open = line.front();
if (open != '<' && open != '"')
{
return std::nullopt;
}
line.remove_prefix(1);
const auto end = line.find(open == '<' ? '>' : '"');
if (end == std::string_view::npos || end == 0)
{
return std::nullopt;
}
return IncludedFile{ std::string{ line.substr(0, end) }, open == '<' };
}
std::optional<std::string>
ParseEngineVersion(std::string_view source)
{
constexpr std::string_view kKey = "static Version version";
const auto key = source.find(kKey);
if (key == std::string_view::npos)
{
return std::nullopt;
}
const auto open = source.find('(', key);
const auto close = source.find(')', key);
if (open == std::string_view::npos || close == std::string_view::npos)
{
return std::nullopt;
}
std::vector<std::string_view> parts;
auto rest = source.substr(open + 1, close - open - 1);
while (true)
{
const auto comma = rest.find(',');
parts.push_back(Trimmed(rest.substr(0, comma)));
if (comma == std::string_view::npos)
{
break;
}
rest.remove_prefix(comma + 1);
}
if (parts.size() != 5)
{
return std::nullopt;
}
std::string version{ parts[0] };
version.append(".").append(parts[1]).append(".").append(parts[2]);
auto branch = parts[3];
if (branch.size() > 2)
{
branch = branch.substr(1, branch.size() - 2);
version.append("-").append(branch).append(".").append(parts[4]);
}
return version;
}
Amalgamation
GenerateAmalgamation(const std::filesystem::path &root, Problems &problems)
{
Generator generator{ root, problems };
Amalgamation amalgamation;
amalgamation.Header = generator.Header();
amalgamation.Source = generator.Source();
amalgamation.ThirdParty = generator.ThirdParty();
return amalgamation;
}
void
CheckAmalgamation(const std::filesystem::path &root,
const Amalgamation &expected,
Problems &problems)
{
for (const auto &[name, text] : Parts(expected))
{
if (ReadText(root / "extras" / name) != *text)
{
problems.push_back(std::string{ "extras/" } + name
+ " is out of date: run bazelisk run "
"//tools:amalgamate");
}
}
}
void
WriteAmalgamation(const std::filesystem::path &root,
const Amalgamation &amalgamation,
Problems &problems)
{
for (const auto &[name, text] : Parts(amalgamation))
{
if (!WriteText(root / "extras" / name, *text))
{
problems.push_back(std::string{ "cannot write extras/" }
+ name);
}
}
}
} // namespace Progmasoft::Catch3::Tools
|