Skip to content

Commit ade9bfc

Browse files
committed
feat(cli): integrate VixC frontend into project builds
1 parent e26260a commit ade9bfc

12 files changed

Lines changed: 498 additions & 143 deletions

‎include/vix/cli/app/AppProjectResolver.hpp‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <filesystem>
2121
#include <string>
2222

23+
#include <vix/cli/app/AppManifest.hpp>
24+
2325
namespace vix::cli::app
2426
{
2527
namespace fs = std::filesystem;
@@ -85,6 +87,9 @@ namespace vix::cli::app
8587
*/
8688
std::string targetName;
8789

90+
/** Prepared application model used for Vix-owned build paths. */
91+
AppManifest manifest;
92+
8893
/**
8994
* @brief True when CMakeLists.txt was generated from vix.app.
9095
*/
@@ -116,9 +121,13 @@ namespace vix::cli::app
116121
* present.
117122
*
118123
* @param base Directory to start from.
124+
* @param use_vixc_frontend Whether to prepare vix.app translation units
125+
* through the optional VixC frontend before build generation.
119126
* @return Resolved project result.
120127
*/
121-
AppProjectResolveResult resolve_app_project(const fs::path &base);
128+
AppProjectResolveResult resolve_app_project(
129+
const fs::path &base,
130+
bool use_vixc_frontend = false);
122131

123132
} // namespace vix::cli::app
124133

‎include/vix/cli/app/VixcFrontend.hpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <filesystem>
1010
#include <string>
1111

12+
#include <vix/cli/app/AppManifest.hpp>
13+
1214
namespace vix::cli::app
1315
{
1416
struct VixcFrontendResult final
@@ -21,6 +23,23 @@ namespace vix::cli::app
2123
[[nodiscard]]
2224
VixcFrontendResult process_with_vixc(
2325
const std::filesystem::path &source);
26+
27+
/**
28+
* @brief Prepares application translation units for native compilation.
29+
*
30+
* Each manifest source is processed independently and replaced in this
31+
* prepared manifest copy by a deterministic path below .vix/generated/vixc.
32+
* Original source directories are retained as include directories so quoted
33+
* source-relative includes continue to resolve after generation.
34+
*
35+
* @param manifest Prepared copy of the application manifest.
36+
* @param project_directory Application project root.
37+
* @return Generated source result or frontend diagnostics on failure.
38+
*/
39+
[[nodiscard]]
40+
VixcFrontendResult prepare_vixc_sources(
41+
AppManifest &manifest,
42+
const std::filesystem::path &project_directory);
2443
}
2544

2645
#endif // VIX_CLI_APP_VIXC_FRONTEND_HPP

‎src/app/AppProjectResolver.cpp‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <vix/cli/app/AppProjectResolver.hpp>
1717

1818
#include <vix/cli/app/AppCMakeGenerator.hpp>
19+
#include <vix/cli/app/VixcFrontend.hpp>
1920
#include <vix/cli/app/AppManifest.hpp>
2021
#include <vix/cli/modules/DependencyConstraints.hpp>
2122
#include <vix/cli/modules/DependencyOwnership.hpp>
@@ -299,7 +300,8 @@ namespace vix::cli::app
299300
}
300301

301302
AppProjectResolveResult resolve_vix_app_project(
302-
const fs::path &projectDir)
303+
const fs::path &projectDir,
304+
bool use_vixc_frontend)
303305
{
304306
AppProjectResolveResult result;
305307

@@ -319,6 +321,17 @@ namespace vix::cli::app
319321

320322
AppManifest manifest = loadResult.manifest;
321323

324+
if (use_vixc_frontend)
325+
{
326+
const VixcFrontendResult prepared =
327+
prepare_vixc_sources(manifest, projectDir);
328+
if (!prepared.success)
329+
{
330+
result.error = prepared.diagnostics;
331+
return result;
332+
}
333+
}
334+
322335
std::string graphError;
323336
const auto graph =
324337
vix::cli::modules::ModuleGraph::from_app_modules(
@@ -389,6 +402,7 @@ namespace vix::cli::app
389402
result.cmakeSourceDir = generateResult.sourceDir;
390403
result.cmakeListsPath = generateResult.cmakeListsPath;
391404
result.targetName = manifest.name;
405+
result.manifest = std::move(manifest);
392406

393407
return result;
394408
}
@@ -417,7 +431,9 @@ namespace vix::cli::app
417431
!cmakeListsPath.empty();
418432
}
419433

420-
AppProjectResolveResult resolve_app_project(const fs::path &base)
434+
AppProjectResolveResult resolve_app_project(
435+
const fs::path &base,
436+
bool use_vixc_frontend)
421437
{
422438
AppProjectResolveResult result;
423439

@@ -438,7 +454,7 @@ namespace vix::cli::app
438454
return resolve_cmake_project(projectDir);
439455

440456
if (file_exists_regular(appManifestPath))
441-
return resolve_vix_app_project(projectDir);
457+
return resolve_vix_app_project(projectDir, use_vixc_frontend);
442458

443459
result.error =
444460
"Unable to determine the project directory. Missing CMakeLists.txt "

‎src/app/VixcFrontend.cpp‎

Lines changed: 153 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,104 +5,190 @@
55

66
#include <vix/cli/app/VixcFrontend.hpp>
77

8+
#include <algorithm>
89
#include <fstream>
10+
#include <iterator>
911
#include <sstream>
12+
#include <string_view>
13+
#include <utility>
14+
#include <vector>
1015

1116
#if defined(VIX_CLI_ENABLE_VIXC_FRONTEND)
1217
#include <vixc/vixc.hpp>
1318
#endif
1419

1520
namespace vix::cli::app
1621
{
17-
VixcFrontendResult process_with_vixc(const std::filesystem::path &source)
22+
namespace
1823
{
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)
2727
{
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;
3529

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+
}
3938

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+
}
4250

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)
5152
{
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;
5563
}
56-
diagnostics << '\n';
57-
}
5864

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;
6467
return output;
6568
}
6669

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)
7473
{
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.";
7678
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+
}
7886

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())
83100
{
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';
88112
}
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
89132
}
133+
}
90134

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)
92152
{
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() == "..")
95158
{
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;
98163
}
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());
100187
}
101188

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;
107193
}
108194
}

0 commit comments

Comments
 (0)