MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
FunctionBuild.cpp
Go to the documentation of this file.
1#include "FunctionBuild.hpp"
2
3#include "Build.hpp"
5#include "FunctionSidecar.hpp"
6#include "Scan.hpp"
7#include "Sidecar.hpp"
8
10
11namespace {
12
13 int fail(const std::string& message)
14 {
15 std::cerr << message << '\n';
16 return 1;
17 }
18
19}
20
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;
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}
102
103}
const FunctionMap & requests() const noexcept
void attach(clang::ast_matchers::MatchFinder &finder)
Registers the matcher for the marker variables on finder.
const std::vector< std::string > & errors() const noexcept
Turns each MF_DESCRIBE_FUNCTION variable in the scanned translation unit into a FunctionRequest.
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.
int run_function_build(const FunctionBuildOptions &options)
Writes the function sidecar of one header, for a build step.
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
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_signatures --functions --build needs to describe the functions of 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