10#ifndef MAYAFLUX_REFLECT_RESOURCE_DIR
11#define MAYAFLUX_REFLECT_RESOURCE_DIR ""
19 std::filesystem::path root;
20 std::filesystem::path registry;
21 std::filesystem::path output;
22 std::filesystem::path build_path;
23 std::filesystem::path translation_unit;
24 std::string resource_dir;
25 std::vector<std::string> extra_args;
27 bool functions {
false };
28 bool insert_includes {
true };
29 std::vector<std::filesystem::path> paths;
32int fail(
const std::string& message)
34 std::cerr << message <<
'\n';
38int generate_functions(
const Config& config)
40 std::vector<std::filesystem::path> roots = config.paths;
42 roots.push_back(config.root /
"src/MayaFlux");
45 for (
auto& root : roots) {
46 const auto canonical = std::filesystem::canonical(root, code);
48 return fail(
"cannot read " + root.string());
53 const std::vector<std::filesystem::path> headers =
discover_headers(roots,
"MF_DESCRIBE_FUNCTION");
54 if (headers.empty()) {
55 return fail(
"no MF_DESCRIBE_FUNCTION requests found under the given paths");
60 for (
const auto& header : headers) {
63 if (!std::filesystem::exists(sidecar)) {
64 input.
stubs.push_back(sidecar.filename().string());
78 if (!
scan(input, [&collector](clang::ast_matchers::MatchFinder& finder) { collector.
attach(finder); }, error)) {
81 if (!collector.
errors().empty()) {
82 for (
const auto& message : collector.
errors()) {
83 std::cerr << message <<
'\n';
88 return fail(
"no MF_DESCRIBE_FUNCTION requests were recognised in the selected headers (marker variables matched: "
89 + std::to_string(collector.
matched()) +
")");
93 std::vector<std::filesystem::path> directories;
94 for (
const auto& root : roots) {
95 if (std::filesystem::is_directory(root, code)) {
96 directories.push_back(root);
99 const auto existing = directories.empty()
100 ? std::vector<std::filesystem::path> {}
103 for (
const auto& problem : problems) {
104 std::cerr << problem <<
'\n';
106 if (problems.empty()) {
107 std::cout <<
"All " << collector.
requests().size() <<
" function sidecars are current\n";
110 std::cout << problems.size() <<
" problem(s) found\n";
115 std::size_t functions = 0;
116 for (
const auto& header : headers) {
117 const auto found = collector.
requests().find(header);
118 if (found == collector.
requests().end()) {
119 std::cerr << header.string() <<
": contains MF_DESCRIBE_FUNCTION text but produced no request\n";
126 functions += found->second.functions.size();
128 if (config.insert_includes) {
129 bool inserted =
false;
135 <<
"\" into " << header.string() <<
'\n';
139 std::cerr << *problem <<
'\n';
143 std::cout <<
"Generated " << functions <<
" function descriptions in " << collector.
requests().size() <<
" sidecars\n";
144 return clean ? 0 : 1;
147int generate(
const Config& config)
149 std::error_code code;
150 const std::filesystem::path source_root = std::filesystem::canonical(config.root /
"src", code);
152 return fail(
"cannot read " + (config.root /
"src").string());
164 input.
roots = { source_root };
168 const auto canonical = std::filesystem::canonical(entry.header, code);
170 return fail(
"cannot read header " + entry.header.string());
172 input.
headers.push_back(canonical);
174 std::ranges::sort(input.
headers);
178 if (!
scan(input, [&collector](clang::ast_matchers::MatchFinder& finder) { collector.
attach(finder); }, error)) {
181 if (!collector.
errors().empty()) {
182 for (
const auto& message : collector.
errors()) {
183 std::cerr << message <<
'\n';
187 if (
const auto missing = collector.
missing(); !missing.empty()) {
188 for (
const auto& name : missing) {
189 std::cerr << name <<
": expected one complete class definition, found none\n";
195 const std::vector<std::pair<std::string, const std::string*>> outputs {
196 {
"CreatorIncludes.hpp", &files.
includes },
197 {
"CreatorClasses.hpp", &files.
classes },
203 std::size_t stale = 0;
204 for (
const auto& [name, text] : outputs) {
205 if (
read_text(config.output / name) != std::optional<std::string>(*text)) {
206 std::cerr << (config.output / name).
string() <<
": differs from what the registry generates\n";
211 std::cout <<
"All " << outputs.size() <<
" generated files are current\n";
214 std::cout << stale <<
" file(s) out of date\n";
218 std::filesystem::create_directories(config.output, code);
220 return fail(
"cannot create " + config.output.string());
222 for (
const auto& [name, text] : outputs) {
227 std::cout <<
"Generated methods for " << collector.
classes().size() <<
" classes\n";
233int main(
int argc,
const char** argv)
235 llvm::cl::OptionCategory category(
"mayaflux-signatures options");
236 llvm::cl::opt<std::string> root(
"root",
237 llvm::cl::desc(
"Repository root holding src/"),
238 llvm::cl::value_desc(
"dir"), llvm::cl::init(
"."), llvm::cl::cat(category));
239 llvm::cl::opt<std::string> registry(
"registry",
240 llvm::cl::desc(
"Registry header (default: <root>/src/MayaFlux/API/Proxy/Registry.hpp)"),
241 llvm::cl::value_desc(
"file"), llvm::cl::cat(category));
242 llvm::cl::opt<std::string> output(
"output",
243 llvm::cl::desc(
"Directory the generated files are written to (default: <root>/src/MayaFlux/API/Proxy/Generated)"),
244 llvm::cl::value_desc(
"dir"), llvm::cl::cat(category));
245 llvm::cl::opt<std::string> build_path(
"p",
246 llvm::cl::desc(
"Directory holding compile_commands.json"),
247 llvm::cl::value_desc(
"dir"), llvm::cl::init(
"build"), llvm::cl::cat(category));
248 llvm::cl::opt<std::string> translation_unit(
"translation-unit",
249 llvm::cl::desc(
"Source file whose compile command is borrowed (default: <root>/src/MayaFlux/API/Proxy/Creator.cpp)"),
250 llvm::cl::value_desc(
"file"), llvm::cl::cat(category));
251 llvm::cl::opt<std::string> resource_dir(
"resource-dir",
252 llvm::cl::desc(
"Clang resource directory holding the builtin headers"),
254 llvm::cl::cat(category));
255 llvm::cl::list<std::string> extra_args(
"extra-arg",
256 llvm::cl::desc(
"Extra Clang argument added to the scan"),
257 llvm::cl::value_desc(
"argument"), llvm::cl::cat(category));
258 llvm::cl::opt<bool> check(
"check",
259 llvm::cl::desc(
"Write nothing; exit 1 when a generated file is out of date"),
260 llvm::cl::cat(category));
261 llvm::cl::opt<bool> functions(
"functions",
262 llvm::cl::desc(
"Write a Name.signatures.hpp sidecar beside each header that holds MF_DESCRIBE_FUNCTION "
263 "requests, instead of the Creator methods. A header with requests must not use its own sidecar"),
264 llvm::cl::cat(category));
265 llvm::cl::opt<bool> no_insert_include(
"no-insert-include",
266 llvm::cl::desc(
"With --functions, report a missing sidecar include instead of inserting it"),
267 llvm::cl::cat(category));
268 llvm::cl::opt<bool> build(
"build",
269 llvm::cl::desc(
"Build step: write the Creator files and mark them modified, or with --functions the "
270 "sidecar of the one header given, inserting its include when missing. Needs "
271 "--translation-unit (defaulted) with a compile database, or --compile-arg"),
272 llvm::cl::cat(category));
273 llvm::cl::list<std::string> compile_args(
"compile-arg",
274 llvm::cl::desc(
"With --build: one argument of the whole compile command, used instead of the database"),
275 llvm::cl::value_desc(
"argument"), llvm::cl::cat(category));
276 llvm::cl::opt<std::string> depfile(
"depfile",
277 llvm::cl::desc(
"With --build: write a depfile of the registry and the headers the scan read"),
278 llvm::cl::value_desc(
"file"), llvm::cl::cat(category));
279 llvm::cl::opt<std::string> depfile_target(
"depfile-target",
280 llvm::cl::desc(
"With --build: the target named in the depfile (default: CreatorIncludes.hpp in the output)"),
281 llvm::cl::value_desc(
"path"), llvm::cl::cat(category));
282 llvm::cl::list<std::string> paths(llvm::cl::Positional,
283 llvm::cl::desc(
"<path>... (with --functions: headers or directories searched, default src/MayaFlux)"),
284 llvm::cl::cat(category));
286 llvm::cl::HideUnrelatedOptions(category);
287 llvm::cl::ParseCommandLineOptions(argc, argv,
288 "Writes the Creator proxy's methods from the public constructors of the registered classes, or with "
289 "--functions the parameter names of the functions named by MF_DESCRIBE_FUNCTION.\n");
292 config.root = root.getValue();
293 const std::filesystem::path proxy = config.root /
"src/MayaFlux/API/Proxy";
294 config.registry = registry.empty() ? proxy /
"Registry.hpp" : std::filesystem::path(registry.getValue());
295 config.output = output.empty() ? proxy /
"Generated" : std::filesystem::path(output.getValue());
296 config.build_path = build_path.getValue();
297 config.translation_unit = translation_unit.empty() ? proxy /
"Creator.cpp" : std::filesystem::path(translation_unit.getValue());
298 config.resource_dir = resource_dir.getValue();
299 config.extra_args.assign(extra_args.begin(), extra_args.end());
300 config.check = check;
301 config.functions = functions;
302 config.insert_includes = !no_insert_include;
303 config.paths.assign(paths.begin(), paths.end());
305 if (build && config.functions) {
306 if (config.check || config.paths.size() != 1) {
307 return fail(
"--functions --build takes exactly one header, and no --check");
310 options.
header = config.paths.front();
315 options.
compile_args.assign(compile_args.begin(), compile_args.end());
316 options.
depfile = depfile.getValue();
323 if (config.check || !config.paths.empty()) {
324 return fail(
"--build writes the Creator files; it takes no --check or paths");
327 options.
root = config.root;
329 options.
output = config.output;
334 options.
compile_args.assign(compile_args.begin(), compile_args.end());
335 options.
depfile = depfile.getValue();
340 return config.functions ? generate_functions(config) : generate(config);
#define MAYAFLUX_REFLECT_RESOURCE_DIR
int main(int argc, const char **argv)
Cycle Behavior: The for_cycles(N) configuration controls how many times the capture operation execute...
One class of the Creator registry: the method name, its qualified type and its header.