MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Build.cpp
Go to the documentation of this file.
1#include "Build.hpp"
2
3#include "Sidecar.hpp"
4
5#include <array>
6#include <cctype>
7#include <cstdio>
8
9#ifdef _WIN32
10#define MAYAFLUX_REFLECT_POPEN _popen
11#define MAYAFLUX_REFLECT_PCLOSE _pclose
12#define MAYAFLUX_REFLECT_QUIET " 2>nul"
13#else
14#define MAYAFLUX_REFLECT_POPEN popen
15#define MAYAFLUX_REFLECT_PCLOSE pclose
16#define MAYAFLUX_REFLECT_QUIET " 2>/dev/null"
17#endif
18
20
21namespace {
22
23 std::string clang_resource_dir()
24 {
25 std::string command = "clang -print-resource-dir";
26 command += MAYAFLUX_REFLECT_QUIET;
27 FILE* pipe = MAYAFLUX_REFLECT_POPEN(command.c_str(), "r");
28 if (pipe == nullptr) {
29 return {};
30 }
31 std::string output;
32 std::array<char, 256> buffer {};
33 while (std::fgets(buffer.data(), static_cast<int>(buffer.size()), pipe) != nullptr) {
34 output += buffer.data();
35 }
37 while (!output.empty() && std::isspace(static_cast<unsigned char>(output.back())) != 0) {
38 output.pop_back();
39 }
40 return output;
41 }
42
43 std::string escaped(std::string_view text)
44 {
45 std::string out;
46 out.reserve(text.size());
47 for (const char c : text) {
48 if (c == ' ' || c == '\t' || c == '#') {
49 out += '\\';
50 } else if (c == '$') {
51 out += '$';
52 }
53 out += c;
54 }
55 return out;
56 }
57
58}
59
60std::string resolve_resource_dir(const std::string& configured)
61{
62 if (!configured.empty() && llvm::sys::fs::is_directory(configured)) {
63 return configured;
64 }
65 std::string found = clang_resource_dir();
66 if (found.empty() || !llvm::sys::fs::is_directory(found)) {
67 return {};
68 }
69 return found;
70}
71
72bool write_depfile(const std::filesystem::path& path, const std::string& target,
73 const std::vector<std::filesystem::path>& dependencies, std::string& error)
74{
75 std::string text = escaped(std::filesystem::path(target).generic_string()) + ":";
76 for (const auto& dependency : dependencies) {
77 text += " \\\n " + escaped(dependency.generic_string());
78 }
79 text += '\n';
80
81 std::error_code code;
82 if (path.has_parent_path()) {
83 std::filesystem::create_directories(path.parent_path(), code);
84 if (code) {
85 error = "cannot create " + path.parent_path().string();
86 return false;
87 }
88 }
89 return write_if_changed(path, text, error);
90}
91
92bool write_generated(const std::filesystem::path& path, const std::string& content, std::string& error)
93{
94 if (!write_if_changed(path, content, error)) {
95 return false;
96 }
97 std::error_code code;
98 std::filesystem::last_write_time(path, std::filesystem::file_time_type::clock::now(), code);
99 if (code) {
100 error = "cannot update the timestamp of " + path.string();
101 return false;
102 }
103 return true;
104}
105
106std::vector<std::filesystem::path> without_sidecars(std::vector<std::filesystem::path> dependencies)
107{
108 std::erase_if(dependencies, [](const std::filesystem::path& path) {
109 const std::string name = path.filename().string();
110 return name.ends_with(".describe.hpp") || name.ends_with(".signatures.hpp");
111 });
112 return dependencies;
113}
114
115}
#define MAYAFLUX_REFLECT_QUIET
Definition Build.cpp:16
#define MAYAFLUX_REFLECT_PCLOSE
Definition Build.cpp:15
#define MAYAFLUX_REFLECT_POPEN
Definition Build.cpp:14
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 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
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