From 209373b1f5770dc175e06996a152df6484f59af2 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 22 Aug 2025 15:39:40 +0200 Subject: feat: implement `CompilerKitDylibTraits` container for future and current frontend tools. Signed-off-by: Amlal El Mahrouss --- dev/CompilerKit/utils/DylibHelpers.h | 50 ++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'dev/CompilerKit/utils') diff --git a/dev/CompilerKit/utils/DylibHelpers.h b/dev/CompilerKit/utils/DylibHelpers.h index 1ae7c03..31e0b68 100644 --- a/dev/CompilerKit/utils/DylibHelpers.h +++ b/dev/CompilerKit/utils/DylibHelpers.h @@ -7,7 +7,57 @@ #pragma once #include +#include #include +struct CompilerKitDylibTraits; + typedef Int32 (*CompilerKitEntrypoint)(Int32 argc, Char const* argv[]); typedef VoidPtr CompilerKitDylib; + +struct CompilerKitDylibTraits final { + CompilerKitDylib fDylib{nullptr}; + CompilerKitEntrypoint fEntrypoint{nullptr}; + std::mutex fMutex; + + CompilerKitDylibTraits& operator()(const Char* path, const Char* fEntrypoint) { + std::lock_guard lock(this->fMutex); + + if (!path || !fEntrypoint) return *this; + + if (this->fDylib) { + dlclose(this->fDylib); + this->fDylib = nullptr; + } + + this->fDylib = dlopen(path, RTLD_LAZY); + + if (!this->fDylib) { + return *this; + } + + this->fEntrypoint = (CompilerKitEntrypoint)dlsym(this->fDylib, fEntrypoint); + + if (!this->fEntrypoint) { + dlclose(this->fDylib); + this->fDylib = nullptr; + + return *this; + } + + return *this; + } + + NECTI_COPY_DELETE(CompilerKitDylibTraits); + + CompilerKitDylibTraits() = default; + + ~CompilerKitDylibTraits() { + if (this->fDylib) { + dlclose(this->fDylib); + this->fDylib = nullptr; + } + + this->fEntrypoint = nullptr; + } +}; -- cgit v1.2.3