Skip to content

Commit e1dc728

Browse files
committed
feat: add experimental VixC frontend integration
2 parents f0354b9 + e26260a commit e1dc728

8 files changed

Lines changed: 231 additions & 1 deletion

File tree

‎CMakeLists.txt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
3232

3333
option(VIX_ENABLE_LTO "Enable LTO in Release builds" OFF)
3434
option(VIX_CLI_ENABLE_DESKTOP "Enable vix desktop support through vix::ui" OFF)
35+
option(VIX_ENABLE_VIXC_FRONTEND "Enable the experimental VixC frontend for vix build and run" OFF)
36+
set(VIXC_SOURCE_DIR "" CACHE PATH "Path to a VixC source checkout for the experimental frontend")
3537

3638
# ----------------------------------------------------
3739
# CLI test isolation
@@ -78,6 +80,22 @@ target_include_directories(vix_cli PRIVATE
7880
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
7981
)
8082

83+
if (VIX_ENABLE_VIXC_FRONTEND)
84+
if (NOT TARGET vixc::vixc)
85+
if (VIXC_SOURCE_DIR)
86+
if (NOT EXISTS "${VIXC_SOURCE_DIR}/CMakeLists.txt")
87+
message(FATAL_ERROR "VIXC_SOURCE_DIR does not contain a VixC CMake project: ${VIXC_SOURCE_DIR}")
88+
endif()
89+
add_subdirectory("${VIXC_SOURCE_DIR}" "${CMAKE_BINARY_DIR}/_vixc")
90+
else()
91+
find_package(vixc CONFIG REQUIRED)
92+
endif()
93+
endif()
94+
95+
target_link_libraries(vix_cli PRIVATE vixc::vixc)
96+
target_compile_definitions(vix_cli PRIVATE VIX_CLI_ENABLE_VIXC_FRONTEND=1)
97+
endif()
98+
8199
function(vix_add_flag_if_supported tgt flag)
82100
string(REGEX REPLACE "[^A-Za-z0-9]" "_" flag_var "${flag}")
83101
set(test_var "HAVE_${flag_var}")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @file VixcFrontend.hpp
3+
* @brief Experimental adapter from Vix application sources to the public VixC frontend.
4+
*/
5+
6+
#ifndef VIX_CLI_APP_VIXC_FRONTEND_HPP
7+
#define VIX_CLI_APP_VIXC_FRONTEND_HPP
8+
9+
#include <filesystem>
10+
#include <string>
11+
12+
namespace vix::cli::app
13+
{
14+
struct VixcFrontendResult final
15+
{
16+
bool success{false};
17+
std::filesystem::path generated_source;
18+
std::string diagnostics;
19+
};
20+
21+
[[nodiscard]]
22+
VixcFrontendResult process_with_vixc(
23+
const std::filesystem::path &source);
24+
}
25+
26+
#endif // VIX_CLI_APP_VIXC_FRONTEND_HPP

‎include/vix/cli/commands/run/RunDetail.hpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ namespace vix::commands::RunCommand::detail
104104

105105
bool singleCpp = false;
106106
fs::path cppFile;
107+
bool useVixcFrontend = false;
107108

108109
bool manifestMode = false;
109110
fs::path manifestFile;

‎include/vix/cli/process/Process.hpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ namespace vix::cli::process
191191
bool singleCpp = false;
192192
fs::path cppFile;
193193

194+
/** Selects the opt-in experimental VixC frontend for source units. */
195+
bool useVixcFrontend = false;
196+
194197
bool warnings{false};
195198
std::size_t warningsPage{1};
196199
std::size_t warningsLimit{10};

‎src/app/VixcFrontend.cpp‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @file VixcFrontend.cpp
3+
* @brief Experimental VixC source preparation adapter.
4+
*/
5+
6+
#include <vix/cli/app/VixcFrontend.hpp>
7+
8+
#include <fstream>
9+
#include <sstream>
10+
11+
#if defined(VIX_CLI_ENABLE_VIXC_FRONTEND)
12+
#include <vixc/vixc.hpp>
13+
#endif
14+
15+
namespace vix::cli::app
16+
{
17+
VixcFrontendResult process_with_vixc(const std::filesystem::path &source)
18+
{
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)
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>{}};
35+
36+
vixc::FrontendOptions options;
37+
options.action = vixc::FrontendAction::Emit;
38+
options.retain_source_map = true;
39+
40+
const vixc::FrontendResult result =
41+
vixc::Frontend{}.process(source.string(), text, options);
42+
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+
{
52+
diagnostics << " (offsets "
53+
<< diagnostic.range().begin_offset() << '-'
54+
<< diagnostic.range().end_offset() << ')';
55+
}
56+
diagnostics << '\n';
57+
}
58+
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.";
64+
return output;
65+
}
66+
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)
74+
{
75+
output.diagnostics = "Unable to create VixC generated-source directory: " + error.message();
76+
return output;
77+
}
78+
79+
bool unchanged = false;
80+
{
81+
std::ifstream existing(generated, std::ios::binary);
82+
if (existing)
83+
{
84+
const std::string existing_text{
85+
std::istreambuf_iterator<char>{existing},
86+
std::istreambuf_iterator<char>{}};
87+
unchanged = existing_text == result.generated_output();
88+
}
89+
}
90+
91+
if (!unchanged)
92+
{
93+
std::ofstream generated_output(generated, std::ios::binary | std::ios::trunc);
94+
if (!generated_output)
95+
{
96+
output.diagnostics = "Unable to write VixC generated source: " + generated.string();
97+
return output;
98+
}
99+
generated_output << result.generated_output();
100+
}
101+
102+
output.success = true;
103+
output.generated_source = generated;
104+
output.diagnostics = diagnostics.str();
105+
return output;
106+
#endif
107+
}
108+
}

‎src/commands/BuildCommand.cpp‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <vix/cli/app/AppManifest.hpp>
2929
#include <vix/cli/app/AppCMakeGenerator.hpp>
3030
#include <vix/cli/app/AppProjectResolver.hpp>
31+
#include <vix/cli/app/VixcFrontend.hpp>
3132

3233
#include <algorithm>
3334
#include <atomic>
@@ -1631,6 +1632,21 @@ namespace vix::commands::BuildCommand
16311632
{
16321633
o.verbose = true;
16331634
}
1635+
else if (a == "--frontend")
1636+
{
1637+
auto v = util::take_value(args, i);
1638+
if (!v || *v != "vixc")
1639+
{
1640+
error("--frontend currently supports only 'vixc'.");
1641+
exitCode = 2;
1642+
return o;
1643+
}
1644+
o.useVixcFrontend = true;
1645+
}
1646+
else if (a == "--frontend=vixc")
1647+
{
1648+
o.useVixcFrontend = true;
1649+
}
16341650
else if (a == "--debug")
16351651
{
16361652
o.debug = true;
@@ -6539,9 +6555,29 @@ namespace vix::commands::BuildCommand
65396555
return 1;
65406556
}
65416557

6558+
const fs::path originalSource = fs::absolute(opt_.cppFile).lexically_normal();
6559+
fs::path sourceForCompilation = originalSource;
6560+
6561+
if (opt_.useVixcFrontend)
6562+
{
6563+
const app::VixcFrontendResult vixcResult =
6564+
app::process_with_vixc(originalSource);
6565+
if (!vixcResult.success)
6566+
{
6567+
error("VixC frontend failed for " + originalSource.string());
6568+
if (!vixcResult.diagnostics.empty())
6569+
std::cerr << vixcResult.diagnostics;
6570+
return 1;
6571+
}
6572+
6573+
sourceForCompilation = vixcResult.generated_source;
6574+
if (!opt_.quiet)
6575+
info("VixC generated: " + sourceForCompilation.string());
6576+
}
6577+
65426578
run_detail::Options runOpt{};
65436579
runOpt.singleCpp = true;
6544-
runOpt.cppFile = fs::absolute(opt_.cppFile);
6580+
runOpt.cppFile = sourceForCompilation;
65456581

65466582
runOpt.preset = opt_.preset;
65476583
runOpt.dir = opt_.dir;
@@ -6582,6 +6618,8 @@ namespace vix::commands::BuildCommand
65826618
runOpt.runArgs.clear();
65836619
runOpt.runEnv.clear();
65846620
runOpt.scriptFlags = opt_.cmakeArgs;
6621+
if (opt_.useVixcFrontend)
6622+
runOpt.scriptFlags.push_back("-I" + originalSource.parent_path().string());
65856623

65866624
fs::path exePath;
65876625
const int code = run_detail::build_script_executable(runOpt, exePath);
@@ -7629,6 +7667,7 @@ namespace vix::commands::BuildCommand
76297667
out << "Build options:\n";
76307668
out << " --preset <name> Build preset: dev, dev-ninja, release\n";
76317669
out << " --build-target <name> Build a specific CMake target\n";
7670+
out << " --frontend vixc Experimentally process a single source through VixC\n";
76327671
out << " -j, --jobs <n> Number of parallel build jobs\n";
76337672
out << " --clean Remove local build directories and configure again\n";
76347673
out << " --watch Watch project files and rebuild incrementally\n";

‎src/commands/RunCommand.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <vix/cli/commands/RunCommand.hpp>
1515
#include <vix/cli/commands/InstallCommand.hpp>
1616
#include <vix/cli/commands/run/RunDetail.hpp>
17+
#include <vix/cli/app/VixcFrontend.hpp>
1718
#include <vix/cli/commands/replay/ReplayCapture.hpp>
1819
#include <vix/cli/commands/replay/ReplayRecorder.hpp>
1920
#include <vix/cli/errors/RawLogDetectors.hpp>
@@ -1638,6 +1639,23 @@ namespace vix::commands::RunCommand
16381639

16391640
if (opt.singleCpp)
16401641
{
1642+
if (opt.useVixcFrontend)
1643+
{
1644+
const fs::path originalSource = fs::absolute(opt.cppFile).lexically_normal();
1645+
const vix::cli::app::VixcFrontendResult vixcResult =
1646+
vix::cli::app::process_with_vixc(originalSource);
1647+
if (!vixcResult.success)
1648+
{
1649+
error("VixC frontend failed for " + originalSource.string());
1650+
if (!vixcResult.diagnostics.empty())
1651+
std::cerr << vixcResult.diagnostics;
1652+
return 1;
1653+
}
1654+
1655+
opt.cppFile = vixcResult.generated_source;
1656+
opt.scriptFlags.push_back("-I" + originalSource.parent_path().string());
1657+
}
1658+
16411659
if (!opt.tempDeps.empty())
16421660
return run_with_temporary_deps(opt);
16431661
return run_script_mode(opt);

‎src/commands/run/RunFlow.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,23 @@ namespace vix::commands::RunCommand::detail
467467
{
468468
opt.verbose = true;
469469
}
470+
else if (a == "--frontend")
471+
{
472+
const std::string value = take_value(args, i, "--frontend", opt);
473+
if (opt.parseFailed)
474+
return opt;
475+
if (value != "vixc")
476+
{
477+
opt.parseFailed = true;
478+
opt.parseExitCode = 2;
479+
return opt;
480+
}
481+
opt.useVixcFrontend = true;
482+
}
483+
else if (a == "--frontend=vixc")
484+
{
485+
opt.useVixcFrontend = true;
486+
}
470487
else if (a == "--local-cache")
471488
{
472489
opt.localCache = true;

0 commit comments

Comments
 (0)