Initializes the Clang interpreter and prepares for code evaluation.
63{
64 LILA_INFO(Emitter::INTERPRETER,
"Initializing Clang interpreter");
65
66#ifdef MAYAFLUX_PLATFORM_WINDOWS
67 llvm::sys::DynamicLibrary::LoadLibraryPermanently("msvcp140.dll");
68 llvm::sys::DynamicLibrary::LoadLibraryPermanently("vcruntime140.dll");
69 llvm::sys::DynamicLibrary::LoadLibraryPermanently("ucrtbase.dll");
70 llvm::sys::DynamicLibrary::LoadLibraryPermanently("MayaFluxLib.dll");
71#endif
72
73 llvm::InitializeNativeTarget();
74 llvm::InitializeNativeTargetAsmPrinter();
75 llvm::InitializeNativeTargetAsmParser();
76
77 m_impl->compile_flags.clear();
78 m_impl->compile_flags.emplace_back(
"-std=c++23");
79 m_impl->compile_flags.emplace_back(
"-DMAYASIMPLE");
80
81#ifdef MAYAFLUX_PLATFORM_LINUX
82 m_impl->compile_flags.emplace_back(
"-mcmodel=large");
83 m_impl->compile_flags.emplace_back(
"-fPIC");
84 m_impl->compile_flags.emplace_back(
"-fPIE");
85#endif
86
87 std::string pch_dir;
88 if (std::filesystem::exists(MayaFlux::Config::PCH_RUNTIME_PATH)) {
89 pch_dir = MayaFlux::Config::RUNTIME_DATA_DIR;
91 std::string("Using installed PCH from: ") + std::string(MayaFlux::Config::PCH_RUNTIME_PATH));
92
93 } else if (std::filesystem::exists(MayaFlux::Config::PCH_SOURCE_PATH)) {
94 pch_dir = std::string(MayaFlux::Config::SOURCE_DIR) + "/cmake";
96 std::string("Using source PCH from: ") + std::string(MayaFlux::Config::PCH_SOURCE_PATH));
97
98 } else {
99 m_last_error =
"Cannot find pch.h in runtime or source locations";
101 return false;
102 }
103
104 m_impl->compile_flags.push_back(
"-I" + pch_dir);
105
107 if (!resource_dir.empty()) {
108 m_impl->compile_flags.push_back(
"-resource-dir=" + resource_dir);
110 std::string("Using clang resource dir: ") + resource_dir);
111 } else {
112 m_impl->compile_flags.emplace_back(
"-resource-dir=/usr/lib/clang/21");
114 "Using default clang resource dir: /usr/lib/clang/21");
115 }
116
118 for (const auto& include : system_includes) {
119 m_impl->compile_flags.push_back(
"-isystem" + include);
120 }
121
122#ifndef MAYAFLUX_PLATFORM_WINDOWS
124 if (!eigen_include.empty()) {
125 m_impl->compile_flags.push_back(
"-isystem" + eigen_include);
127 std::string("Added Eigen include path: ") + eigen_include);
128 } else {
130 "Could not find Eigen include path - some features may not work");
131 }
132#endif
133
134#ifdef MAYAFLUX_PLATFORM_MACOS
135
136
137 std::string sdk_path = MayaFlux::Platform::SystemConfig::get_macos_sdk_path();
138 if (!sdk_path.empty()) {
139 m_impl->compile_flags.push_back(
"-isysroot" + sdk_path);
140 LILA_DEBUG(Emitter::INTERPRETER,
"Using macOS SDK: " + sdk_path);
141 } else {
143 "Could not find macOS SDK - JIT may fail to find system headers");
144 }
145#endif
146
147 for (
const auto& path :
m_impl->include_paths) {
148 m_impl->compile_flags.push_back(
"-I" + path);
149 }
150
151#ifdef MAYAFLUX_PLATFORM_WINDOWS
152 m_impl->compile_flags.emplace_back(
"-fno-function-sections");
153 m_impl->compile_flags.emplace_back(
"-fno-data-sections");
154 m_impl->compile_flags.emplace_back(
"-fno-unique-section-names");
155#endif
156
157 std::vector<const char*> args;
158 for (
const auto& flag :
m_impl->compile_flags) {
159 args.push_back(flag.c_str());
160 }
161
162 clang::IncrementalCompilerBuilder ICB;
163 ICB.SetCompilerArgs(args);
164
165 auto CI = ICB.CreateCpp();
166 if (!CI) {
167 m_last_error =
"Failed to create CompilerInstance: " + llvm::toString(CI.takeError());
169 return false;
170 }
171
172 auto interp = clang::Interpreter::create(std::move(*CI));
173 if (!interp) {
174 m_last_error =
"Failed to create interpreter: " + llvm::toString(interp.takeError());
176 return false;
177 }
178
179 m_impl->interpreter = std::move(*interp);
180
181 LILA_INFO(Emitter::INTERPRETER,
"Clang interpreter created successfully");
182
183 auto result =
m_impl->interpreter->ParseAndExecute(
184 "#include \"pch.h\"\n"
185 "#include \"Lila/LiveAid.hpp\"\n");
186
187 if (result) {
188 std::string warning = "Failed to load MayaFlux headers: " + llvm::toString(std::move(result));
189 LILA_WARN(Emitter::INTERPRETER, warning);
190 } else {
191 LILA_INFO(Emitter::INTERPRETER,
"MayaFlux headers loaded successfully");
192 }
193
194 result =
m_impl->interpreter->ParseAndExecute(
"std::cout << \"Ready for Live\" << std::flush;");
195
196 return true;
197}
std::unique_ptr< Impl > m_impl
Internal implementation details.