MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
CreatorBuild.cpp
Go to the documentation of this file.
1#include "CreatorBuild.hpp"
2
3#include "Build.hpp"
4#include "Scan.hpp"
7#include "SignatureOutput.hpp"
8
10
11namespace {
12
13 int fail(const std::string& message)
14 {
15 std::cerr << message << '\n';
16 return 1;
17 }
18
19}
20
22{
23 std::error_code code;
24 const std::filesystem::path source_root = std::filesystem::canonical(options.root / "src", code);
25 if (code) {
26 return fail("cannot read " + (options.root / "src").string());
27 }
28 if (options.compile_args.empty() && options.translation_unit.empty()) {
29 return fail("--build needs --translation-unit with a compile database, or --compile-arg");
30 }
31
32 const std::string resource_dir = resolve_resource_dir(options.resource_dir);
33 if (resource_dir.empty()) {
34 return fail("no Clang resource directory: pass --resource-dir, or put clang on PATH");
35 }
36
37 std::string error;
38 auto entries = read_signature_entries(options.registry, source_root, error);
39 if (!entries) {
40 return fail(error);
41 }
42
43 BuildScanInput input;
44 input.scan.build_path = options.build_path;
46 input.scan.roots = { source_root };
47 input.scan.resource_dir = resource_dir;
48 input.scan.extra_args = options.extra_args;
49 input.compile_args = options.compile_args;
50 for (const SignatureEntry& entry : *entries) {
51 const auto canonical = std::filesystem::canonical(entry.header, code);
52 if (code) {
53 return fail("cannot read header " + entry.header.string());
54 }
55 input.scan.headers.push_back(canonical);
56 }
57 std::ranges::sort(input.scan.headers);
58 input.scan.headers.erase(std::ranges::unique(input.scan.headers).begin(), input.scan.headers.end());
59
60 SignatureCollector collector(*entries, source_root);
61 std::vector<std::filesystem::path> dependencies;
62 if (!scan_for_build(input, [&collector](clang::ast_matchers::MatchFinder& finder) { collector.attach(finder); },
63 dependencies, error)) {
64 return fail(error);
65 }
66 if (!collector.errors().empty()) {
67 for (const auto& message : collector.errors()) {
68 std::cerr << message << '\n';
69 }
70 return 1;
71 }
72 if (const auto missing = collector.missing(); !missing.empty()) {
73 for (const auto& name : missing) {
74 std::cerr << name << ": expected one complete class definition, found none\n";
75 }
76 return 1;
77 }
78
79 const SignatureFiles files = render_signature_files(collector.classes(), source_root);
80 const std::vector<std::pair<std::string, const std::string*>> outputs {
81 { "CreatorIncludes.hpp", &files.includes },
82 { "CreatorClasses.hpp", &files.classes },
83 { "CreatorDeclarations.inc", &files.declarations },
84 { "CreatorDefinitions.inc", &files.definitions },
85 };
86
87 std::filesystem::create_directories(options.output, code);
88 if (code) {
89 return fail("cannot create " + options.output.string());
90 }
91 for (const auto& [name, text] : outputs) {
92 if (!write_generated(options.output / name, *text, error)) {
93 return fail(error);
94 }
95 }
96
97 if (!options.depfile.empty()) {
98 const std::filesystem::path output_root = std::filesystem::weakly_canonical(options.output, code);
99 std::erase_if(dependencies, [&output_root](const std::filesystem::path& path) {
100 const auto relative = path.lexically_relative(output_root);
101 return !relative.empty() && *relative.begin() != "..";
102 });
103 const auto registry = std::filesystem::weakly_canonical(options.registry, code);
104 dependencies.push_back(registry);
105 std::ranges::sort(dependencies);
106 dependencies.erase(std::ranges::unique(dependencies).begin(), dependencies.end());
107
108 const std::string target = options.depfile_target.empty()
109 ? (options.output / outputs.front().first).generic_string()
110 : options.depfile_target;
111 if (!write_depfile(options.depfile, target, without_sidecars(std::move(dependencies)), error)) {
112 return fail(error);
113 }
114 }
115
116 std::cout << "Generated methods for " << collector.classes().size() << " classes\n";
117 return 0;
118}
119
120}
std::vector< SignatureClass > classes() const
The registered classes that were found, in registry order.
std::vector< std::string > missing() const
Registered classes the scan never reached a definition of.
const std::vector< std::string > & errors() const noexcept
void attach(clang::ast_matchers::MatchFinder &finder)
Registers the matcher for class definitions on finder.
Reads the public constructors of each registered class from the scanned translation unit.
bool write_generated(const std::filesystem::path &path, const std::string &content, std::string &error)
Writes content when it differs, then marks path as modified now.
Definition Build.cpp:92
std::string resolve_resource_dir(const std::string &configured)
The Clang resource directory a build scan uses.
Definition Build.cpp:60
std::vector< std::filesystem::path > without_sidecars(std::vector< std::filesystem::path > dependencies)
dependencies without files that end in a generated sidecar suffix.
Definition Build.cpp:106
int run_creator_build(const CreatorBuildOptions &options)
Writes the four Creator files, for a build step.
std::optional< std::vector< SignatureEntry > > read_signature_entries(const std::filesystem::path &registry, const std::filesystem::path &source_root, std::string &error)
Reads the N, W and B registrations of the registry header and finds each class's header.
SignatureFiles render_signature_files(const std::vector< SignatureClass > &classes, const std::filesystem::path &source_root)
Renders the generated files for the non abstract classes of classes.
bool scan_for_build(const BuildScanInput &input, const AttachMatchers &attach, std::vector< std::filesystem::path > &dependencies, std::string &error)
scan() for a build step: also reports the files the scan read.
Definition Scan.cpp:330
bool write_depfile(const std::filesystem::path &path, const std::string &target, const std::vector< std::filesystem::path > &dependencies, std::string &error)
Writes a depfile of the form target: dependency... that CMake's DEPFILE reads.
Definition Build.cpp:72
std::vector< std::string > compile_args
Definition Scan.hpp:43
What a build scan needs: a ScanInput, plus optional explicit compile arguments.
Definition Scan.hpp:41
What mayaflux_signatures --build needs to write the Creator files.
std::filesystem::path build_path
Definition Scan.hpp:12
std::vector< std::string > extra_args
Definition Scan.hpp:17
std::vector< std::filesystem::path > roots
Definition Scan.hpp:14
std::vector< std::filesystem::path > headers
Definition Scan.hpp:15
std::filesystem::path translation_unit
Definition Scan.hpp:13
One class of the Creator registry: the method name, its qualified type and its header.
The four generated files of the Creator proxy, as text.