add yaze_ext lib for extensions
This commit is contained in:
@@ -74,6 +74,7 @@ if(APPLE)
|
||||
endif()
|
||||
|
||||
include(app/CMakeLists.txt)
|
||||
include(ext/CMakeLists.txt)
|
||||
include(py/CMakeLists.txt)
|
||||
# include(cli/CMakeLists.txt) Excluded for now, macOS include breaks action build
|
||||
|
||||
|
||||
16
src/ext/CMakeLists.txt
Normal file
16
src/ext/CMakeLists.txt
Normal file
@@ -0,0 +1,16 @@
|
||||
add_library(
|
||||
yaze_ext
|
||||
ext/extension.cc
|
||||
)
|
||||
|
||||
target_include_directories(
|
||||
yaze_ext PUBLIC
|
||||
lib/
|
||||
app/
|
||||
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers
|
||||
)
|
||||
|
||||
target_link_libraries(
|
||||
yaze_ext PUBLIC
|
||||
Python
|
||||
)
|
||||
53
src/ext/extension.cc
Normal file
53
src/ext/extension.cc
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "extension.h"
|
||||
|
||||
#include <Python.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
void* handle = nullptr;
|
||||
Extension* extension = nullptr;
|
||||
|
||||
Extension* GetExtension() { return nullptr; }
|
||||
|
||||
void loadCExtension(const char* extensionPath) {
|
||||
handle = dlopen(extensionPath, RTLD_LAZY);
|
||||
if (!handle) {
|
||||
std::cerr << "Cannot open extension: " << dlerror() << std::endl;
|
||||
return;
|
||||
}
|
||||
dlerror(); // Clear any existing error
|
||||
|
||||
Extension* (*getExtension)();
|
||||
getExtension = (Extension * (*)()) dlsym(handle, "getExtension");
|
||||
const char* dlsym_error = dlerror();
|
||||
if (dlsym_error) {
|
||||
std::cerr << "Cannot load symbol 'getExtension': " << dlsym_error
|
||||
<< std::endl;
|
||||
dlclose(handle);
|
||||
return;
|
||||
}
|
||||
|
||||
extension = getExtension();
|
||||
extension->initialize();
|
||||
}
|
||||
|
||||
void LoadPythonExtension(const char* script_path) {
|
||||
Py_Initialize();
|
||||
FILE* fp = fopen(script_path, "r");
|
||||
if (fp) {
|
||||
PyRun_SimpleFile(fp, script_path);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
PyObject* pModule = PyImport_AddModule("__main__");
|
||||
PyObject* pFunc = PyObject_GetAttrString(pModule, "get_extension");
|
||||
|
||||
if (pFunc && PyCallable_Check(pFunc)) {
|
||||
PyObject* pExtension = PyObject_CallObject(pFunc, nullptr);
|
||||
if (pExtension) {
|
||||
// Assume the Python extension has the same structure as the C extension
|
||||
extension = reinterpret_cast<Extension*>(PyLong_AsVoidPtr(pExtension));
|
||||
}
|
||||
}
|
||||
}
|
||||
34
src/ext/extension.h
Normal file
34
src/ext/extension.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef EXTENSION_INTERFACE_H
|
||||
#define EXTENSION_INTERFACE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct Extension {
|
||||
const char* name;
|
||||
const char* version;
|
||||
|
||||
// Initialization function
|
||||
void (*initialize)(void);
|
||||
|
||||
// Cleanup function
|
||||
void (*cleanup)(void);
|
||||
|
||||
// Function to extend editor functionality
|
||||
void (*extendFunctionality)(void* editorContext);
|
||||
} Extension;
|
||||
|
||||
// Function to get the extension instance
|
||||
Extension* GetExtension();
|
||||
|
||||
void LoadCExtension(const char* extension_path);
|
||||
|
||||
// Function to load a Python script as an extension
|
||||
void LoadPythonExtension(const char* script_path);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // EXTENSION_INTERFACE_H
|
||||
Reference in New Issue
Block a user