MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ initialize()

bool Lila::ClangInterpreter::initialize ( )

Initializes the Clang interpreter and prepares for code evaluation.

Returns
True if initialization succeeded, false otherwise

Definition at line 62 of file ClangInterpreter.cpp.

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;
90 LILA_DEBUG(Emitter::INTERPRETER,
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";
95 LILA_DEBUG(Emitter::INTERPRETER,
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";
100 LILA_ERROR(Emitter::INTERPRETER, m_last_error);
101 return false;
102 }
103
104 m_impl->compile_flags.push_back("-I" + pch_dir);
105
106 const std::string& resource_dir = MayaFlux::Platform::SystemConfig::get_clang_resource_dir();
107 if (!resource_dir.empty()) {
108 m_impl->compile_flags.push_back("-resource-dir=" + resource_dir);
109 LILA_DEBUG(Emitter::INTERPRETER,
110 std::string("Using clang resource dir: ") + resource_dir);
111 } else {
112 m_impl->compile_flags.emplace_back("-resource-dir=/usr/lib/clang/21");
113 LILA_WARN(Emitter::INTERPRETER,
114 "Using default clang resource dir: /usr/lib/clang/21");
115 }
116
117 const auto& system_includes = MayaFlux::Platform::SystemConfig::get_system_includes();
118 for (const auto& include : system_includes) {
119 m_impl->compile_flags.push_back("-isystem" + include);
120 }
121
122#ifndef MAYAFLUX_PLATFORM_WINDOWS
123 const auto& eigen_include = MayaFlux::Platform::SystemConfig::get_dep_includes("eigen");
124 if (!eigen_include.empty()) {
125 m_impl->compile_flags.push_back("-isystem" + eigen_include);
126 LILA_DEBUG(Emitter::INTERPRETER,
127 std::string("Added Eigen include path: ") + eigen_include);
128 } else {
129 LILA_WARN(Emitter::INTERPRETER,
130 "Could not find Eigen include path - some features may not work");
131 }
132#endif
133
134#ifdef MAYAFLUX_PLATFORM_MACOS
135 // CRITICAL: JIT uses Homebrew LLVM but needs macOS SDK for system headers
136 // Homebrew LLVM's libc++ requires pthread.h, sched.h, time.h, etc. from SDK
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 {
142 LILA_WARN(Emitter::INTERPRETER,
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());
168 LILA_ERROR(Emitter::INTERPRETER, m_last_error);
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());
175 LILA_ERROR(Emitter::INTERPRETER, m_last_error);
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}
#define LILA_WARN(emitter, msg)
#define LILA_ERROR(emitter, msg)
#define LILA_DEBUG(emitter, msg)
#define LILA_INFO(emitter, msg)
std::unique_ptr< Impl > m_impl
Internal implementation details.
static const std::string & get_clang_resource_dir()
static const std::string & get_dep_includes(const std::string &library_name)
static const std::vector< std::string > & get_system_includes()

References MayaFlux::Platform::SystemConfig::get_clang_resource_dir(), MayaFlux::Platform::SystemConfig::get_dep_includes(), MayaFlux::Platform::SystemConfig::get_system_includes(), LILA_DEBUG, LILA_ERROR, LILA_INFO, and LILA_WARN.

+ Here is the call graph for this function: