|
5 | 5 |
|
6 | 6 | #include <vix/cli/app/VixcFrontend.hpp> |
7 | 7 |
|
| 8 | +#include <algorithm> |
8 | 9 | #include <fstream> |
| 10 | +#include <iterator> |
9 | 11 | #include <sstream> |
| 12 | +#include <string_view> |
| 13 | +#include <utility> |
| 14 | +#include <vector> |
10 | 15 |
|
11 | 16 | #if defined(VIX_CLI_ENABLE_VIXC_FRONTEND) |
12 | 17 | #include <vixc/vixc.hpp> |
13 | 18 | #endif |
14 | 19 |
|
15 | 20 | namespace vix::cli::app |
16 | 21 | { |
17 | | - VixcFrontendResult process_with_vixc(const std::filesystem::path &source) |
| 22 | + namespace |
18 | 23 | { |
19 | | - VixcFrontendResult output; |
20 | | - |
21 | | -#if !defined(VIX_CLI_ENABLE_VIXC_FRONTEND) |
22 | | - output.diagnostics = "Vix was built without experimental VixC frontend support."; |
23 | | - return output; |
24 | | -#else |
25 | | - std::ifstream input(source, std::ios::binary); |
26 | | - if (!input) |
| 24 | + VixcFrontendResult write_vixc_output( |
| 25 | + const std::filesystem::path &generated, |
| 26 | + std::string_view text) |
27 | 27 | { |
28 | | - output.diagnostics = "Unable to read VixC source: " + source.string(); |
29 | | - return output; |
30 | | - } |
31 | | - |
32 | | - const std::string text{ |
33 | | - std::istreambuf_iterator<char>{input}, |
34 | | - std::istreambuf_iterator<char>{}}; |
| 28 | + VixcFrontendResult output; |
35 | 29 |
|
36 | | - vixc::FrontendOptions options; |
37 | | - options.action = vixc::FrontendAction::Emit; |
38 | | - options.retain_source_map = true; |
| 30 | + std::error_code error; |
| 31 | + std::filesystem::create_directories(generated.parent_path(), error); |
| 32 | + if (error) |
| 33 | + { |
| 34 | + output.diagnostics = |
| 35 | + "Unable to create VixC generated-source directory: " + error.message(); |
| 36 | + return output; |
| 37 | + } |
39 | 38 |
|
40 | | - const vixc::FrontendResult result = |
41 | | - vixc::Frontend{}.process(source.string(), text, options); |
| 39 | + bool unchanged = false; |
| 40 | + { |
| 41 | + std::ifstream existing(generated, std::ios::binary); |
| 42 | + if (existing) |
| 43 | + { |
| 44 | + const std::string existing_text{ |
| 45 | + std::istreambuf_iterator<char>{existing}, |
| 46 | + std::istreambuf_iterator<char>{}}; |
| 47 | + unchanged = existing_text == text; |
| 48 | + } |
| 49 | + } |
42 | 50 |
|
43 | | - std::ostringstream diagnostics; |
44 | | - for (const vixc::Diagnostic &diagnostic : result.diagnostics()) |
45 | | - { |
46 | | - diagnostics << "vixc "; |
47 | | - if (diagnostic.has_code()) |
48 | | - diagnostics << '[' << diagnostic.code() << "] "; |
49 | | - diagnostics << diagnostic.message(); |
50 | | - if (diagnostic.has_range()) |
| 51 | + if (!unchanged) |
51 | 52 | { |
52 | | - diagnostics << " (offsets " |
53 | | - << diagnostic.range().begin_offset() << '-' |
54 | | - << diagnostic.range().end_offset() << ')'; |
| 53 | + std::ofstream generated_output( |
| 54 | + generated, |
| 55 | + std::ios::binary | std::ios::trunc); |
| 56 | + if (!generated_output) |
| 57 | + { |
| 58 | + output.diagnostics = |
| 59 | + "Unable to write VixC generated source: " + generated.string(); |
| 60 | + return output; |
| 61 | + } |
| 62 | + generated_output << text; |
55 | 63 | } |
56 | | - diagnostics << '\n'; |
57 | | - } |
58 | 64 |
|
59 | | - if (!result.success() || !result.has_generated_output()) |
60 | | - { |
61 | | - output.diagnostics = diagnostics.str(); |
62 | | - if (output.diagnostics.empty()) |
63 | | - output.diagnostics = "VixC did not produce generated C++ output."; |
| 65 | + output.success = true; |
| 66 | + output.generated_source = generated; |
64 | 67 | return output; |
65 | 68 | } |
66 | 69 |
|
67 | | - const std::filesystem::path directory = |
68 | | - source.parent_path() / ".vix" / "vixc"; |
69 | | - const std::filesystem::path generated = |
70 | | - directory / (source.filename().string() + ".vixc.cpp"); |
71 | | - std::error_code error; |
72 | | - std::filesystem::create_directories(directory, error); |
73 | | - if (error) |
| 70 | + VixcFrontendResult process_with_vixc_to_path( |
| 71 | + const std::filesystem::path &source, |
| 72 | + const std::filesystem::path &generated) |
74 | 73 | { |
75 | | - output.diagnostics = "Unable to create VixC generated-source directory: " + error.message(); |
| 74 | + VixcFrontendResult output; |
| 75 | + |
| 76 | +#if !defined(VIX_CLI_ENABLE_VIXC_FRONTEND) |
| 77 | + output.diagnostics = "Vix was built without experimental VixC frontend support."; |
76 | 78 | return output; |
77 | | - } |
| 79 | +#else |
| 80 | + std::ifstream input(source, std::ios::binary); |
| 81 | + if (!input) |
| 82 | + { |
| 83 | + output.diagnostics = "Unable to read VixC source: " + source.string(); |
| 84 | + return output; |
| 85 | + } |
78 | 86 |
|
79 | | - bool unchanged = false; |
80 | | - { |
81 | | - std::ifstream existing(generated, std::ios::binary); |
82 | | - if (existing) |
| 87 | + const std::string text{ |
| 88 | + std::istreambuf_iterator<char>{input}, |
| 89 | + std::istreambuf_iterator<char>{}}; |
| 90 | + |
| 91 | + vixc::FrontendOptions options; |
| 92 | + options.action = vixc::FrontendAction::Emit; |
| 93 | + options.retain_source_map = true; |
| 94 | + |
| 95 | + const vixc::FrontendResult result = |
| 96 | + vixc::Frontend{}.process(source.string(), text, options); |
| 97 | + |
| 98 | + std::ostringstream diagnostics; |
| 99 | + for (const vixc::Diagnostic &diagnostic : result.diagnostics()) |
83 | 100 | { |
84 | | - const std::string existing_text{ |
85 | | - std::istreambuf_iterator<char>{existing}, |
86 | | - std::istreambuf_iterator<char>{}}; |
87 | | - unchanged = existing_text == result.generated_output(); |
| 101 | + diagnostics << "vixc "; |
| 102 | + if (diagnostic.has_code()) |
| 103 | + diagnostics << '[' << diagnostic.code() << "] "; |
| 104 | + diagnostics << diagnostic.message(); |
| 105 | + if (diagnostic.has_range()) |
| 106 | + { |
| 107 | + diagnostics << " (offsets " |
| 108 | + << diagnostic.range().begin_offset() << '-' |
| 109 | + << diagnostic.range().end_offset() << ')'; |
| 110 | + } |
| 111 | + diagnostics << '\n'; |
88 | 112 | } |
| 113 | + |
| 114 | + if (!result.success() || !result.has_generated_output()) |
| 115 | + { |
| 116 | + output.diagnostics = diagnostics.str(); |
| 117 | + if (output.diagnostics.empty()) |
| 118 | + output.diagnostics = "VixC did not produce generated C++ output."; |
| 119 | + return output; |
| 120 | + } |
| 121 | + |
| 122 | + output = write_vixc_output(generated, result.generated_output()); |
| 123 | + if (!output.success) |
| 124 | + { |
| 125 | + output.diagnostics = diagnostics.str() + output.diagnostics; |
| 126 | + return output; |
| 127 | + } |
| 128 | + |
| 129 | + output.diagnostics = diagnostics.str(); |
| 130 | + return output; |
| 131 | +#endif |
89 | 132 | } |
| 133 | + } |
90 | 134 |
|
91 | | - if (!unchanged) |
| 135 | + VixcFrontendResult process_with_vixc(const std::filesystem::path &source) |
| 136 | + { |
| 137 | + const std::filesystem::path generated = |
| 138 | + source.parent_path() / ".vix" / "vixc" / |
| 139 | + (source.filename().string() + ".vixc.cpp"); |
| 140 | + return process_with_vixc_to_path(source, generated); |
| 141 | + } |
| 142 | + |
| 143 | + VixcFrontendResult prepare_vixc_sources( |
| 144 | + AppManifest &manifest, |
| 145 | + const std::filesystem::path &project_directory) |
| 146 | + { |
| 147 | + VixcFrontendResult prepared; |
| 148 | + std::vector<std::string> compile_sources; |
| 149 | + std::vector<std::string> include_directories = manifest.includeDirs; |
| 150 | + |
| 151 | + for (const std::string &source : manifest.sources) |
92 | 152 | { |
93 | | - std::ofstream generated_output(generated, std::ios::binary | std::ios::trunc); |
94 | | - if (!generated_output) |
| 153 | + const std::filesystem::path relative = |
| 154 | + std::filesystem::path{source}.lexically_normal(); |
| 155 | + if (relative.is_absolute() || |
| 156 | + relative.empty() || |
| 157 | + *relative.begin() == "..") |
95 | 158 | { |
96 | | - output.diagnostics = "Unable to write VixC generated source: " + generated.string(); |
97 | | - return output; |
| 159 | + prepared.diagnostics = |
| 160 | + "VixC application sources must be relative to the project directory: " + |
| 161 | + source; |
| 162 | + return prepared; |
98 | 163 | } |
99 | | - generated_output << result.generated_output(); |
| 164 | + |
| 165 | + const std::filesystem::path original = |
| 166 | + (project_directory / relative).lexically_normal(); |
| 167 | + const std::filesystem::path generated = |
| 168 | + project_directory / ".vix" / "generated" / "vixc" / |
| 169 | + (relative.generic_string() + ".vixc.cpp"); |
| 170 | + |
| 171 | + VixcFrontendResult result = process_with_vixc_to_path(original, generated); |
| 172 | + if (!result.success) |
| 173 | + return result; |
| 174 | + |
| 175 | + compile_sources.push_back( |
| 176 | + generated.lexically_relative(project_directory).generic_string()); |
| 177 | + const std::filesystem::path parent = original.parent_path(); |
| 178 | + const bool parentAlreadyIncluded = std::any_of( |
| 179 | + include_directories.begin(), |
| 180 | + include_directories.end(), |
| 181 | + [&](const std::string &include_directory) |
| 182 | + { |
| 183 | + return (project_directory / include_directory).lexically_normal() == parent; |
| 184 | + }); |
| 185 | + if (!parentAlreadyIncluded) |
| 186 | + include_directories.push_back(parent.generic_string()); |
100 | 187 | } |
101 | 188 |
|
102 | | - output.success = true; |
103 | | - output.generated_source = generated; |
104 | | - output.diagnostics = diagnostics.str(); |
105 | | - return output; |
106 | | -#endif |
| 189 | + manifest.sources = std::move(compile_sources); |
| 190 | + manifest.includeDirs = std::move(include_directories); |
| 191 | + prepared.success = true; |
| 192 | + return prepared; |
107 | 193 | } |
108 | 194 | } |
0 commit comments