MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ReflectBuild.cpp
Go to the documentation of this file.
1#include "ReflectBuild.hpp"
2
3#include "Build.hpp"
4#include "Collector.hpp"
5#include "Scan.hpp"
6#include "Sidecar.hpp"
7
9
10namespace {
11
12 int fail(const std::string& message)
13 {
14 std::cerr << message << '\n';
15 return 1;
16 }
17
18}
19
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;
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}
100
101}
const std::vector< std::string > & errors() const noexcept
Definition Collector.hpp:22
void attach(clang::ast_matchers::MatchFinder &finder)
Registers the matcher for the marker aliases on finder.
const RequestMap & requests() const noexcept
Definition Collector.hpp:21
Turns each MF_DESCRIBE alias in the scanned translation unit into a RecordRequest.
Definition Collector.hpp:14
int run_reflect_build(const ReflectBuildOptions &options)
Writes the sidecar of one header, for a build step.
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
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_reflect --build needs to describe one header.
std::filesystem::path build_path
Definition Scan.hpp:12
std::vector< std::string > extra_args
Definition Scan.hpp:17
std::vector< std::string > stubs
Definition Scan.hpp:18
std::vector< std::filesystem::path > headers
Definition Scan.hpp:15
std::filesystem::path translation_unit
Definition Scan.hpp:13