MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ run_reflect_build()

int MayaFlux::Reflect::Tool::run_reflect_build ( const ReflectBuildOptions & options)
nodiscard

Writes the sidecar of one header, for a build step.

Inserts the sidecar include when the header lacks it, writes the sidecar, and writes a depfile of the files the scan read when options.depfile is set. A missing sidecar is stubbed in the scan's temporary directory, and no other file in the source tree is touched.

Returns
0 on success, 1 with the reason on stderr otherwise.

Definition at line 20 of file ReflectBuild.cpp.

21{
22 std::error_code code;
23 const std::filesystem::path header = std::filesystem::canonical(options.header, code);
24 if (code) {
25 return fail("cannot read header " + options.header.string());
26 }
27 if (options.compile_args.empty() && options.translation_unit.empty()) {
28 return fail("--build needs --translation-unit with a compile database, or --compile-arg");
29 }
30
31 const std::string resource_dir = resolve_resource_dir(options.resource_dir);
32 if (resource_dir.empty()) {
33 return fail("no Clang resource directory: pass --resource-dir, or put clang on PATH");
34 }
35
36 const std::filesystem::path sidecar = sidecar_path(header);
37
38 BuildScanInput input;
39 input.scan.build_path = options.build_path;
40 input.scan.translation_unit = options.translation_unit;
41 input.scan.headers = { header };
42 input.scan.resource_dir = resource_dir;
43 input.scan.extra_args = options.extra_args;
44 input.compile_args = options.compile_args;
45 if (!std::filesystem::exists(sidecar)) {
46 input.scan.stubs.push_back(sidecar.filename().string());
47 }
48
49 RequestCollector collector;
50 std::vector<std::filesystem::path> dependencies;
51 std::string error;
52 if (!scan_for_build(input, [&collector](clang::ast_matchers::MatchFinder& finder) { collector.attach(finder); },
53 dependencies, error)) {
54 return fail(error);
55 }
56
57 bool clean = true;
58 for (const auto& message : collector.errors()) {
59 if (message.starts_with(header.string())) {
60 std::cerr << message << '\n';
61 clean = false;
62 }
63 }
64 if (!clean) {
65 return 1;
66 }
67
68 const auto found = collector.requests().find(header);
69 if (found == collector.requests().end()) {
70 return fail(header.string() + ": contains MF_DESCRIBE text but produced no request");
71 }
72
73 if (options.insert_includes) {
74 bool inserted = false;
75 if (!insert_include(found->second, inserted, error)) {
76 return fail(error);
77 }
78 if (inserted) {
79 std::cout << "Inserted #include \"" << sidecar.filename().string() << "\" into " << header.string() << '\n';
80 }
81 }
82 if (const auto problem = check_include(found->second)) {
83 return fail(*problem);
84 }
85
86 if (!write_generated(sidecar, render_sidecar(found->second), error)) {
87 return fail(error);
88 }
89
90 if (!options.depfile.empty()) {
91 const std::string target = options.depfile_target.empty() ? sidecar.generic_string() : options.depfile_target;
92 if (!write_depfile(options.depfile, target, without_sidecars(std::move(dependencies)), error)) {
93 return fail(error);
94 }
95 }
96
97 std::cout << "Described " << found->second.records.size() << " type(s) in " << header.filename().string() << '\n';
98 return 0;
99}
Turns each MF_DESCRIBE alias in the scanned translation unit into a RecordRequest.
Definition Collector.hpp:14
void error(Component component, Context context, std::source_location location, std::string_view message)
Log an error message and optionally throw an exception.
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
std::string render_sidecar(const HeaderRequests &requests)
Text of the sidecar for one header, with its records sorted by name.
Definition Sidecar.cpp:95
bool insert_include(const HeaderRequests &requests, bool &inserted, std::string &error)
Inserts the sidecar include on the line after the last marker when the header has none.
Definition Sidecar.cpp:41
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
std::filesystem::path sidecar_path(const std::filesystem::path &header)
Path of the sidecar for header: Name.hpp becomes Name.describe.hpp beside it.
Definition Sidecar.cpp:85
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::optional< std::string > check_include(const HeaderRequests &requests)
Checks that the header includes its sidecar exactly once, after its last marker.
Definition Sidecar.cpp:161

References MayaFlux::Reflect::Tool::RequestCollector::attach(), MayaFlux::Reflect::Tool::ReflectBuildOptions::build_path, MayaFlux::Reflect::Tool::ScanInput::build_path, check_include(), MayaFlux::Reflect::Tool::BuildScanInput::compile_args, MayaFlux::Reflect::Tool::ReflectBuildOptions::compile_args, MayaFlux::Reflect::Tool::ReflectBuildOptions::depfile, MayaFlux::Reflect::Tool::ReflectBuildOptions::depfile_target, MayaFlux::Reflect::Tool::RequestCollector::errors(), MayaFlux::Reflect::Tool::ReflectBuildOptions::extra_args, MayaFlux::Reflect::Tool::ScanInput::extra_args, MayaFlux::Reflect::Tool::ReflectBuildOptions::header, MayaFlux::Reflect::Tool::ScanInput::headers, insert_include(), MayaFlux::Reflect::Tool::ReflectBuildOptions::insert_includes, render_sidecar(), MayaFlux::Reflect::Tool::RequestCollector::requests(), resolve_resource_dir(), MayaFlux::Reflect::Tool::ReflectBuildOptions::resource_dir, MayaFlux::Reflect::Tool::ScanInput::resource_dir, MayaFlux::Reflect::Tool::BuildScanInput::scan, scan_for_build(), sidecar_path(), MayaFlux::Reflect::Tool::ScanInput::stubs, MayaFlux::Reflect::Tool::ReflectBuildOptions::translation_unit, MayaFlux::Reflect::Tool::ScanInput::translation_unit, without_sidecars(), write_depfile(), and write_generated().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function: