MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
MayaFluxReflect.cpp
Go to the documentation of this file.
1#include "Check.hpp"
2#include "Discovery.hpp"
3#include "ReflectBuild.hpp"
4#include "Scan.hpp"
5#include "Sidecar.hpp"
6
7#ifndef MAYAFLUX_REFLECT_RESOURCE_DIR
8#define MAYAFLUX_REFLECT_RESOURCE_DIR ""
9#endif
10
11namespace {
12
13using namespace MayaFlux::Reflect::Tool;
14
15struct Config {
16 std::vector<std::filesystem::path> roots;
17 std::vector<std::filesystem::path> headers;
18 std::filesystem::path build_path;
19 std::filesystem::path translation_unit;
20 std::string resource_dir;
21 std::vector<std::string> extra_args;
22 bool insert_includes { true };
23 bool check { false };
24};
25
26int fail(const std::string& message)
27{
28 std::cerr << message << '\n';
29 return 1;
30}
31
32int generate(const Config& config)
33{
34 std::vector<std::filesystem::path> headers = discover_headers(config.roots);
35 std::error_code code;
36 for (const auto& header : config.headers) {
37 const auto canonical = std::filesystem::canonical(header, code);
38 if (code) {
39 return fail("cannot read header " + header.string());
40 }
41 headers.push_back(canonical);
42 }
43 std::ranges::sort(headers);
44 headers.erase(std::ranges::unique(headers).begin(), headers.end());
45 if (headers.empty()) {
46 return fail("no MF_DESCRIBE requests found; pass --root or header paths");
47 }
48
49 std::string error;
50 ScanInput input;
51 for (const auto& header : headers) {
52 const std::filesystem::path sidecar = sidecar_path(header);
53 if (config.check) {
54 if (!std::filesystem::exists(sidecar)) {
55 input.stubs.push_back(sidecar.filename().string());
56 }
57 } else if (!ensure_stub(sidecar, error)) {
58 return fail(error);
59 }
60 }
61
62 input.build_path = config.build_path;
63 input.translation_unit = config.translation_unit;
64 input.roots = config.roots;
65 input.headers = headers;
66 input.resource_dir = config.resource_dir;
67 input.extra_args = config.extra_args;
68
69 RequestCollector collector;
70 if (!scan(input, collector, error)) {
71 return fail(error);
72 }
73 if (!collector.errors().empty()) {
74 for (const auto& message : collector.errors()) {
75 std::cerr << message << '\n';
76 }
77 return 1;
78 }
79 if (collector.requests().empty()) {
80 return fail("no MF_DESCRIBE requests were recognised in the selected headers (marker aliases matched: "
81 + std::to_string(collector.matched()) + ", skipped: " + std::to_string(collector.skipped()) + ")");
82 }
83
84 if (config.check) {
85 const auto existing = config.roots.empty() ? std::vector<std::filesystem::path> {} : discover_sidecars(config.roots);
86 const auto problems = check_sidecars(collector.requests(), headers, existing);
87 for (const auto& problem : problems) {
88 std::cerr << problem << '\n';
89 }
90 if (problems.empty()) {
91 std::cout << "All " << collector.requests().size() << " sidecars are current\n";
92 return 0;
93 }
94 std::cout << problems.size() << " problem(s) found\n";
95 return 1;
96 }
97
98 bool clean = true;
99 std::size_t types = 0;
100 for (const auto& header : headers) {
101 const auto found = collector.requests().find(header);
102 if (found == collector.requests().end()) {
103 std::cerr << header.string() << ": contains MF_DESCRIBE text but produced no request\n";
104 clean = false;
105 continue;
106 }
107 if (!write_if_changed(sidecar_path(header), render_sidecar(found->second), error)) {
108 return fail(error);
109 }
110 types += found->second.records.size();
111 if (config.insert_includes) {
112 bool inserted = false;
113 if (!insert_include(found->second, inserted, error)) {
114 return fail(error);
115 }
116 if (inserted) {
117 std::cout << "Inserted #include \"" << sidecar_path(header).filename().string() << "\" into " << header.string() << '\n';
118 }
119 }
120 if (const auto problem = check_include(found->second)) {
121 std::cerr << *problem << '\n';
122 clean = false;
123 }
124 }
125
126 std::cout << "Generated " << types << " type descriptions in " << collector.requests().size() << " sidecars\n";
127 return clean ? 0 : 1;
128}
129
130}
131
132int main(int argc, const char** argv)
133{
134 llvm::cl::OptionCategory category("mayaflux-reflect options");
135 llvm::cl::list<std::string> roots("root",
136 llvm::cl::desc("Directory searched recursively for MF_DESCRIBE, or a single header "
137 "(default without headers: src/MayaFlux when it exists, else src)"),
138 llvm::cl::value_desc("path"), llvm::cl::cat(category));
139 llvm::cl::list<std::string> headers(llvm::cl::Positional,
140 llvm::cl::desc("<header>..."), llvm::cl::cat(category));
141 llvm::cl::opt<std::string> build_path("p",
142 llvm::cl::desc("Directory holding compile_commands.json"),
143 llvm::cl::value_desc("dir"), llvm::cl::init("build"), llvm::cl::cat(category));
144 llvm::cl::opt<std::string> translation_unit("translation-unit",
145 llvm::cl::desc("Source file whose compile command is borrowed for the scan"),
146 llvm::cl::value_desc("file"), llvm::cl::cat(category));
147 llvm::cl::opt<std::string> resource_dir("resource-dir",
148 llvm::cl::desc("Clang resource directory holding the builtin headers"),
149 llvm::cl::value_desc("dir"), llvm::cl::init(MAYAFLUX_REFLECT_RESOURCE_DIR),
150 llvm::cl::cat(category));
151 llvm::cl::list<std::string> extra_args("extra-arg",
152 llvm::cl::desc("Extra Clang argument added to the scan"),
153 llvm::cl::value_desc("argument"), llvm::cl::cat(category));
154
155 llvm::cl::opt<bool> no_insert_include("no-insert-include",
156 llvm::cl::desc("Report a missing sidecar include instead of inserting it"),
157 llvm::cl::cat(category));
158
159 llvm::cl::opt<bool> check("check",
160 llvm::cl::desc("Write nothing; exit 1 when a sidecar, its include or a marker is out of date"),
161 llvm::cl::cat(category));
162
163 llvm::cl::opt<bool> build("build",
164 llvm::cl::desc("Build step: describe the one header given, inserting its sidecar include when missing, "
165 "and write nothing else in the source tree. Needs --translation-unit or --compile-arg"),
166 llvm::cl::cat(category));
167 llvm::cl::list<std::string> compile_args("compile-arg",
168 llvm::cl::desc("With --build: one argument of the whole compile command, used instead of the database"),
169 llvm::cl::value_desc("argument"), llvm::cl::cat(category));
170 llvm::cl::opt<std::string> depfile("depfile",
171 llvm::cl::desc("With --build: write a depfile of the files the scan read"),
172 llvm::cl::value_desc("file"), llvm::cl::cat(category));
173 llvm::cl::opt<std::string> depfile_target("depfile-target",
174 llvm::cl::desc("With --build: the target named in the depfile (default: the sidecar path)"),
175 llvm::cl::value_desc("path"), llvm::cl::cat(category));
176
177 llvm::cl::HideUnrelatedOptions(category);
178 llvm::cl::ParseCommandLineOptions(argc, argv,
179 "Writes a Name.describe.hpp sidecar beside each header that holds MF_DESCRIBE requests.\n");
180
181 Config config;
182 config.roots.assign(roots.begin(), roots.end());
183 config.headers.assign(headers.begin(), headers.end());
184 config.build_path = build_path.getValue();
185 config.translation_unit = translation_unit.getValue();
186 config.resource_dir = resource_dir.getValue();
187 config.extra_args.assign(extra_args.begin(), extra_args.end());
188 config.insert_includes = !no_insert_include;
189 config.check = check;
190
191 if (build) {
192 if (config.headers.size() != 1 || !config.roots.empty() || config.check) {
193 return fail("--build takes exactly one header, and neither --root nor --check");
194 }
195 ReflectBuildOptions options;
196 options.header = config.headers.front();
197 options.build_path = config.build_path;
198 options.translation_unit = config.translation_unit;
199 options.resource_dir = config.resource_dir;
200 options.extra_args = config.extra_args;
201 options.compile_args.assign(compile_args.begin(), compile_args.end());
202 options.depfile = depfile.getValue();
203 options.depfile_target = depfile_target.getValue();
204 options.insert_includes = config.insert_includes;
205 return run_reflect_build(options);
206 }
207
208 if (config.roots.empty() && config.headers.empty()) {
209 config.roots.emplace_back(std::filesystem::is_directory("src/MayaFlux") ? "src/MayaFlux" : "src");
210 }
211
212 for (auto& root : config.roots) {
213 std::error_code code;
214 const auto canonical = std::filesystem::canonical(root, code);
215 if (code) {
216 return fail("cannot read root " + root.string());
217 }
218 root = canonical;
219 }
220 return generate(config);
221}
int main(int argc, const char **argv)
#define MAYAFLUX_REFLECT_RESOURCE_DIR
Cycle Behavior: The for_cycles(N) configuration controls how many times the capture operation execute...
std::size_t matched() const noexcept
Marker aliases the matcher reached, before any filtering.
Definition Collector.hpp:25
std::size_t skipped() const noexcept
Marker aliases ignored for sitting in a system header or an invalid location.
Definition Collector.hpp:28
const std::vector< std::string > & errors() const noexcept
Definition Collector.hpp:22
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.
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 ensure_stub(const std::filesystem::path &path, std::string &error)
Writes an empty sidecar when none exists, so the include never fails a first scan.
Definition Sidecar.cpp:129
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
std::vector< std::filesystem::path > discover_headers(const std::vector< std::filesystem::path > &roots)
Finds headers under roots that contain an MF_DESCRIBE request.
Definition Discovery.cpp:45
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
std::vector< std::string > check_sidecars(const RequestMap &requests, const std::vector< std::filesystem::path > &headers, const std::vector< std::filesystem::path > &existing)
Compares the sidecars on disk with what the generator would write, touching nothing.
Definition Check.cpp:102
std::vector< std::filesystem::path > discover_sidecars(const std::vector< std::filesystem::path > &roots)
Every Name.describe.hpp under the directories among roots, canonical and sorted.
Definition Discovery.cpp:76
bool scan(const ScanInput &input, RequestCollector &collector, std::string &error)
Parses every header of input in one translation unit and fills collector.
Definition Scan.cpp:176
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
bool write_if_changed(const std::filesystem::path &path, const std::string &content, std::string &error)
Replaces path with content only when the content differs.
Definition Sidecar.cpp:137
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 > roots
Definition Scan.hpp:14
std::vector< std::filesystem::path > headers
Definition Scan.hpp:15
std::filesystem::path translation_unit
Definition Scan.hpp:13
What one scan needs: the compile database, the headers, and flag overrides.
Definition Scan.hpp:11