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

◆ run_function_build()

int MayaFlux::Reflect::Tool::run_function_build ( const FunctionBuildOptions & options)
nodiscard

Writes the function 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 21 of file FunctionBuild.cpp.

22{
23 std::error_code code;
24 const std::filesystem::path header = std::filesystem::canonical(options.header, code);
25 if (code) {
26 return fail("cannot read header " + options.header.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 const std::filesystem::path sidecar = sidecar_path(header, function_sidecar_suffix);
38
39 BuildScanInput input;
40 input.scan.build_path = options.build_path;
41 input.scan.translation_unit = options.translation_unit;
42 input.scan.headers = { header };
43 input.scan.resource_dir = resource_dir;
44 input.scan.extra_args = options.extra_args;
45 input.compile_args = options.compile_args;
46 if (!std::filesystem::exists(sidecar)) {
47 input.scan.stubs.push_back(sidecar.filename().string());
48 }
49
50 FunctionCollector collector;
51 std::vector<std::filesystem::path> dependencies;
52 std::string error;
53 if (!scan_for_build(input, [&collector](clang::ast_matchers::MatchFinder& finder) { collector.attach(finder); },
54 dependencies, error)) {
55 return fail(error);
56 }
57
58 bool clean = true;
59 for (const auto& message : collector.errors()) {
60 if (message.starts_with(header.string())) {
61 std::cerr << message << '\n';
62 clean = false;
63 }
64 }
65 if (!clean) {
66 return 1;
67 }
68
69 const auto found = collector.requests().find(header);
70 if (found == collector.requests().end()) {
71 return fail(header.string() + ": contains MF_DESCRIBE_FUNCTION text but produced no request");
72 }
73
74 const unsigned last = last_function_line(found->second);
75 if (options.insert_includes) {
76 bool inserted = false;
77 if (!insert_include(header, last, function_sidecar_suffix, inserted, error)) {
78 return fail(error);
79 }
80 if (inserted) {
81 std::cout << "Inserted #include \"" << sidecar.filename().string() << "\" into " << header.string() << '\n';
82 }
83 }
84 if (const auto problem = check_include(header, last, function_sidecar_suffix)) {
85 return fail(*problem);
86 }
87
88 if (!write_generated(sidecar, render_function_sidecar(found->second), error)) {
89 return fail(error);
90 }
91
92 if (!options.depfile.empty()) {
93 const std::string target = options.depfile_target.empty() ? sidecar.generic_string() : options.depfile_target;
94 if (!write_depfile(options.depfile, target, without_sidecars(std::move(dependencies)), error)) {
95 return fail(error);
96 }
97 }
98
99 std::cout << "Described " << found->second.functions.size() << " function(s) in " << header.filename().string() << '\n';
100 return 0;
101}
Turns each MF_DESCRIBE_FUNCTION variable in the scanned translation unit into a FunctionRequest.
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
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
constexpr std::string_view function_sidecar_suffix
The suffix of a function sidecar: Name.hpp becomes Name.signatures.hpp beside it.
std::string render_function_sidecar(const HeaderFunctions &functions)
Text of the sidecar for one header, one Signature specialization per function, sorted by name.
unsigned last_function_line(const HeaderFunctions &functions)
The line after which the sidecar include may appear: the last marker of the header.
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::FunctionCollector::attach(), MayaFlux::Reflect::Tool::FunctionBuildOptions::build_path, MayaFlux::Reflect::Tool::ScanInput::build_path, check_include(), MayaFlux::Reflect::Tool::BuildScanInput::compile_args, MayaFlux::Reflect::Tool::FunctionBuildOptions::compile_args, MayaFlux::Reflect::Tool::FunctionBuildOptions::depfile, MayaFlux::Reflect::Tool::FunctionBuildOptions::depfile_target, MayaFlux::Reflect::Tool::FunctionCollector::errors(), MayaFlux::Reflect::Tool::FunctionBuildOptions::extra_args, MayaFlux::Reflect::Tool::ScanInput::extra_args, function_sidecar_suffix, MayaFlux::Reflect::Tool::FunctionBuildOptions::header, MayaFlux::Reflect::Tool::ScanInput::headers, insert_include(), MayaFlux::Reflect::Tool::FunctionBuildOptions::insert_includes, last_function_line(), render_function_sidecar(), MayaFlux::Reflect::Tool::FunctionCollector::requests(), resolve_resource_dir(), MayaFlux::Reflect::Tool::FunctionBuildOptions::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::FunctionBuildOptions::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: