mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-21 12:00:18 +00:00
remove dylib support
This commit is contained in:
parent
a23e4bfff5
commit
acbfd11366
92
.github/workflows/dylib.yml
vendored
92
.github/workflows/dylib.yml
vendored
@ -1,92 +0,0 @@
|
||||
name: build dylib
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
paths-ignore:
|
||||
- 'docs/**'
|
||||
- 'web/**'
|
||||
- '**.md'
|
||||
pull_request:
|
||||
paths-ignore:
|
||||
- 'docs/**'
|
||||
- 'web/**'
|
||||
- '**.md'
|
||||
|
||||
jobs:
|
||||
build_win:
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: ilammy/msvc-dev-cmd@v1
|
||||
- name: Compile
|
||||
shell: powershell
|
||||
run: |
|
||||
Invoke-Expression (Invoke-Webrequest 'https://xmake.io/psget.text' -UseBasicParsing).Content
|
||||
mkdir -p output/windows/x86_64
|
||||
cd dylib
|
||||
xmake f -p windows -a x64
|
||||
xmake
|
||||
cp build/windows/x64/release/test.dll ../output/windows/x86_64
|
||||
- uses: actions/upload-artifact@v3
|
||||
with:
|
||||
path: output
|
||||
build_linux_android:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Install xmake
|
||||
run: |
|
||||
wget https://xmake.io/shget.text -O - | bash
|
||||
xmake --version
|
||||
- name: Compile linux
|
||||
run: |
|
||||
mkdir -p output/linux/x86_64
|
||||
cd dylib
|
||||
xmake f -p linux -a x86_64
|
||||
xmake
|
||||
cp build/linux/x86_64/release/libtest.so ../output/linux/x86_64
|
||||
- uses: android-actions/setup-android@v2
|
||||
- uses: nttld/setup-ndk@v1
|
||||
id: setup-ndk
|
||||
with:
|
||||
ndk-version: r25b
|
||||
add-to-path: false
|
||||
local-cache: true
|
||||
- name: Compile android
|
||||
run: |
|
||||
mkdir -p output/android/armeabi-v7a
|
||||
mkdir -p output/android/arm64-v8a
|
||||
cd dylib
|
||||
xmake f -p android -a armeabi-v7a --ndk=$ANDROID_NDK_HOME
|
||||
xmake
|
||||
cp build/android/armeabi-v7a/release/libtest.so ../output/android/armeabi-v7a
|
||||
xmake f -p android -a arm64-v8a --ndk=$ANDROID_NDK_HOME
|
||||
xmake
|
||||
cp build/android/arm64-v8a/release/libtest.so ../output/android/arm64-v8a
|
||||
env:
|
||||
ANDROID_NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}
|
||||
- uses: actions/upload-artifact@v3
|
||||
with:
|
||||
path: output
|
||||
build_macos:
|
||||
runs-on: macos-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Install xmake
|
||||
run: |
|
||||
wget https://xmake.io/shget.text -O - | bash
|
||||
source ~/.xmake/profile
|
||||
xmake --version
|
||||
- name: Compile
|
||||
run: |
|
||||
source ~/.xmake/profile
|
||||
mkdir -p output/macos/x86_64
|
||||
cd dylib
|
||||
xmake f -p macosx -a x86_64
|
||||
xmake
|
||||
cp build/macosx/x86_64/release/libtest.dylib ../output/macos/x86_64
|
||||
- uses: actions/upload-artifact@v3
|
||||
with:
|
||||
path: output
|
||||
|
||||
|
@ -20,8 +20,6 @@ else()
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions -O2")
|
||||
endif()
|
||||
|
||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
add_library(
|
||||
box2d
|
||||
STATIC
|
||||
|
@ -16,8 +16,6 @@ else()
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions -O2")
|
||||
endif()
|
||||
|
||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
add_library(
|
||||
cjson
|
||||
STATIC
|
||||
|
@ -35,11 +35,6 @@ aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/src POCKETPY_SRC)
|
||||
|
||||
option(PK_USE_BOX2D "Use Box2D" OFF)
|
||||
option(PK_USE_CJSON "Use cJSON" OFF)
|
||||
option(PK_USE_DYLIB "Use dylib" OFF)
|
||||
|
||||
if(PK_USE_DYLIB)
|
||||
add_definitions(-DPK_USE_DYLIB)
|
||||
endif()
|
||||
|
||||
if(PK_USE_BOX2D)
|
||||
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/3rd/box2d)
|
||||
@ -77,10 +72,6 @@ else()
|
||||
target_link_libraries(${PROJECT_EXE_NAME} ${CMAKE_DL_LIBS})
|
||||
endif()
|
||||
|
||||
if(PK_USE_DYLIB)
|
||||
target_link_libraries(${PROJECT_NAME} ${CMAKE_DL_LIBS})
|
||||
endif()
|
||||
|
||||
if(PK_USE_BOX2D)
|
||||
target_link_libraries(${PROJECT_NAME} box2d)
|
||||
endif()
|
||||
|
@ -1,62 +0,0 @@
|
||||
---
|
||||
icon: dot
|
||||
label: 'Use dynamic library'
|
||||
order: 45
|
||||
---
|
||||
|
||||
!!!
|
||||
This feature is optional. Set `PK_USE_DYLIB` to `1` to enable it.
|
||||
!!!
|
||||
|
||||
You can import a native module from a dynamic library at runtime.
|
||||
This feature is supported on Windows, Linux, macOS, and Android.
|
||||
|
||||
## Create a module as a dynamic library
|
||||
|
||||
Implement a `pkpy_module__init__` function and export it as a symbol.
|
||||
This is the entry point of the module. When users call `__import__` function,
|
||||
the VM will call this function to initialize the module.
|
||||
|
||||
You can create one or more modules inside `pkpy_module__init__` function,
|
||||
and return the name of the module you want users to import directly.
|
||||
|
||||
You should use C-APIs to interact with the VM in the dynamic library.
|
||||
This is to make sure the dynamic library is compatible with different compilers.
|
||||
|
||||
```c
|
||||
#include "pocketpy_c.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static int hello(pkpy_vm* vm){
|
||||
printf("Hello from dylib!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PK_EXPORT
|
||||
const char* pkpy_module__init__(pkpy_vm* vm, const char* version){
|
||||
printf("version: %s\n", version);
|
||||
pkpy_push_function(vm, "hello()", hello);
|
||||
pkpy_push_module(vm, "test");
|
||||
pkpy_setattr(vm, pkpy_name("hello"));
|
||||
// check if initialization failed
|
||||
if(pkpy_clear_error(vm, NULL)) return NULL;
|
||||
return "test";
|
||||
}
|
||||
```
|
||||
|
||||
## Load a dynamic library
|
||||
|
||||
You can load a dynamic library with `__import__` function with a path to the library.
|
||||
|
||||
```python
|
||||
test = __import__("test.dll") # Windows
|
||||
|
||||
test = __import__("libtest.so") # Linux
|
||||
|
||||
test = __import__("libtest.dylib") # macOS
|
||||
|
||||
test = __import__("libtest.so") # Android
|
||||
|
||||
test.hello() # Hello from dylib!
|
||||
```
|
@ -1,19 +0,0 @@
|
||||
#include "pocketpy_c.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static int hello(pkpy_vm* vm){
|
||||
printf("Hello from dylib!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PK_EXPORT
|
||||
const char* pkpy_module__init__(pkpy_vm* vm, const char* version){
|
||||
printf("version: %s\n", version);
|
||||
pkpy_push_function(vm, "hello()", hello);
|
||||
pkpy_push_module(vm, "test");
|
||||
pkpy_setattr(vm, pkpy_name("hello"));
|
||||
// check if initialization failed
|
||||
if(pkpy_clear_error(vm, NULL)) return NULL;
|
||||
return "test";
|
||||
}
|
@ -1,88 +0,0 @@
|
||||
add_rules("mode.debug", "mode.release")
|
||||
|
||||
set_languages("c11")
|
||||
|
||||
root_dir = "../"
|
||||
|
||||
-- Include directories
|
||||
add_includedirs(root_dir .. "include")
|
||||
|
||||
-- Define the shared library target for pocketpy
|
||||
target("pocketpy")
|
||||
set_kind("shared")
|
||||
add_files(root_dir .. "src2/pocketpy_c.c")
|
||||
|
||||
-- Define the shared library target
|
||||
target("test")
|
||||
set_kind("shared")
|
||||
add_files("src/test.c")
|
||||
add_deps("pocketpy")
|
||||
|
||||
--
|
||||
-- If you want to known more usage about xmake, please see https://xmake.io
|
||||
--
|
||||
-- ## FAQ
|
||||
--
|
||||
-- You can enter the project directory firstly before building project.
|
||||
--
|
||||
-- $ cd projectdir
|
||||
--
|
||||
-- 1. How to build project?
|
||||
--
|
||||
-- $ xmake
|
||||
--
|
||||
-- 2. How to configure project?
|
||||
--
|
||||
-- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release]
|
||||
--
|
||||
-- 3. Where is the build output directory?
|
||||
--
|
||||
-- The default output directory is `./build` and you can configure the output directory.
|
||||
--
|
||||
-- $ xmake f -o outputdir
|
||||
-- $ xmake
|
||||
--
|
||||
-- 4. How to run and debug target after building project?
|
||||
--
|
||||
-- $ xmake run [targetname]
|
||||
-- $ xmake run -d [targetname]
|
||||
--
|
||||
-- 5. How to install target to the system directory or other output directory?
|
||||
--
|
||||
-- $ xmake install
|
||||
-- $ xmake install -o installdir
|
||||
--
|
||||
-- 6. Add some frequently-used compilation flags in xmake.lua
|
||||
--
|
||||
-- @code
|
||||
-- -- add debug and release modes
|
||||
-- add_rules("mode.debug", "mode.release")
|
||||
--
|
||||
-- -- add macro definition
|
||||
-- add_defines("NDEBUG", "_GNU_SOURCE=1")
|
||||
--
|
||||
-- -- set warning all as error
|
||||
-- set_warnings("all", "error")
|
||||
--
|
||||
-- -- set language: c99, c++11
|
||||
-- set_languages("c99", "c++11")
|
||||
--
|
||||
-- -- set optimization: none, faster, fastest, smallest
|
||||
-- set_optimize("fastest")
|
||||
--
|
||||
-- -- add include search directories
|
||||
-- add_includedirs("/usr/include", "/usr/local/include")
|
||||
--
|
||||
-- -- add link libraries and search directories
|
||||
-- add_links("tbox")
|
||||
-- add_linkdirs("/usr/local/lib", "/usr/lib")
|
||||
--
|
||||
-- -- add system link libraries
|
||||
-- add_syslinks("z", "pthread")
|
||||
--
|
||||
-- -- add compilation and link flags
|
||||
-- add_cxflags("-stdnolib", "-fno-strict-aliasing")
|
||||
-- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true})
|
||||
--
|
||||
-- @endcode
|
||||
--
|
@ -3,59 +3,32 @@
|
||||
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
|
||||
//define something for Windows (32-bit and 64-bit, this part is common)
|
||||
#define PK_EXPORT __declspec(dllexport)
|
||||
#ifdef PK_USE_DYLIB
|
||||
#define PK_SUPPORT_DYLIB 1
|
||||
#else
|
||||
#define PK_SUPPORT_DYLIB 0
|
||||
#endif
|
||||
#define PK_SYS_PLATFORM "win32"
|
||||
#elif __EMSCRIPTEN__
|
||||
#include <emscripten.h>
|
||||
#define PK_EXPORT EMSCRIPTEN_KEEPALIVE
|
||||
#define PK_SUPPORT_DYLIB 0
|
||||
#define PK_SYS_PLATFORM "emscripten"
|
||||
#elif __APPLE__
|
||||
#include <TargetConditionals.h>
|
||||
#if TARGET_IPHONE_SIMULATOR
|
||||
// iOS, tvOS, or watchOS Simulator
|
||||
#define PK_SYS_PLATFORM "ios"
|
||||
#define PK_SUPPORT_DYLIB 4
|
||||
#elif TARGET_OS_IPHONE
|
||||
// iOS, tvOS, or watchOS device
|
||||
#define PK_SYS_PLATFORM "ios"
|
||||
#define PK_SUPPORT_DYLIB 4
|
||||
#elif TARGET_OS_MAC
|
||||
#define PK_SYS_PLATFORM "darwin"
|
||||
#ifdef PK_USE_DYLIB
|
||||
#include <dlfcn.h>
|
||||
#define PK_SUPPORT_DYLIB 2
|
||||
#else
|
||||
#define PK_SUPPORT_DYLIB 0
|
||||
#endif
|
||||
#else
|
||||
# error "Unknown Apple platform"
|
||||
#endif
|
||||
#define PK_EXPORT __attribute__((visibility("default")))
|
||||
#elif __ANDROID__
|
||||
#ifdef PK_USE_DYLIB
|
||||
#include <dlfcn.h>
|
||||
#define PK_SUPPORT_DYLIB 3
|
||||
#else
|
||||
#define PK_SUPPORT_DYLIB 0
|
||||
#endif
|
||||
#define PK_EXPORT __attribute__((visibility("default")))
|
||||
#define PK_SYS_PLATFORM "android"
|
||||
#elif __linux__
|
||||
#ifdef PK_USE_DYLIB
|
||||
#include <dlfcn.h>
|
||||
#define PK_SUPPORT_DYLIB 2
|
||||
#else
|
||||
#define PK_SUPPORT_DYLIB 0
|
||||
#endif
|
||||
#define PK_EXPORT __attribute__((visibility("default")))
|
||||
#define PK_SYS_PLATFORM "linux"
|
||||
#else
|
||||
#define PK_EXPORT
|
||||
#define PK_SUPPORT_DYLIB 0
|
||||
#define PK_SYS_PLATFORM "unknown"
|
||||
#endif
|
@ -8,74 +8,8 @@
|
||||
#include "cJSONw.hpp"
|
||||
#endif
|
||||
|
||||
#if defined (_WIN32) && PK_SUPPORT_DYLIB == 1
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
namespace pkpy{
|
||||
|
||||
using dylib_entry_t = const char* (*)(void*, const char*);
|
||||
|
||||
#if PK_ENABLE_OS
|
||||
|
||||
#if PK_SUPPORT_DYLIB == 1
|
||||
// win32
|
||||
static dylib_entry_t load_dylib(const char* path){
|
||||
std::error_code ec;
|
||||
auto p = std::filesystem::absolute(path, ec);
|
||||
if(ec) return nullptr;
|
||||
HMODULE handle = LoadLibraryA(p.string().c_str());
|
||||
if(!handle){
|
||||
DWORD errorCode = GetLastError();
|
||||
// Convert the error code to text
|
||||
LPSTR errorMessage = nullptr;
|
||||
FormatMessageA(
|
||||
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
nullptr,
|
||||
errorCode,
|
||||
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
|
||||
(LPSTR)&errorMessage,
|
||||
0,
|
||||
nullptr
|
||||
);
|
||||
printf("%lu: %s\n", errorCode, errorMessage);
|
||||
LocalFree(errorMessage);
|
||||
return nullptr;
|
||||
}
|
||||
return (dylib_entry_t)GetProcAddress(handle, "pkpy_module__init__");
|
||||
}
|
||||
#elif PK_SUPPORT_DYLIB == 2
|
||||
// linux/darwin
|
||||
static dylib_entry_t load_dylib(const char* path){
|
||||
std::error_code ec;
|
||||
auto p = std::filesystem::absolute(path, ec);
|
||||
if(ec) return nullptr;
|
||||
void* handle = dlopen(p.c_str(), RTLD_LAZY);
|
||||
if(!handle) return nullptr;
|
||||
return (dylib_entry_t)dlsym(handle, "pkpy_module__init__");
|
||||
}
|
||||
|
||||
#elif PK_SUPPORT_DYLIB == 3
|
||||
// android
|
||||
static dylib_entry_t load_dylib(const char* path){
|
||||
void* handle = dlopen(path, RTLD_LAZY);
|
||||
if(!handle) return nullptr;
|
||||
return (dylib_entry_t)dlsym(handle, "pkpy_module__init__");
|
||||
}
|
||||
|
||||
#else
|
||||
static dylib_entry_t load_dylib(const char* path){
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
#else
|
||||
static dylib_entry_t load_dylib(const char* path){
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
void init_builtins(VM* _vm) {
|
||||
#define BIND_NUM_ARITH_OPT(name, op) \
|
||||
_vm->bind##name(_vm->tp_int, [](VM* vm, PyObject* lhs, PyObject* rhs) { \
|
||||
@ -205,23 +139,6 @@ void init_builtins(VM* _vm) {
|
||||
|
||||
_vm->bind_builtin_func<1>("__import__", [](VM* vm, ArgsView args) {
|
||||
const Str& name = CAST(Str&, args[0]);
|
||||
auto dot = name.sv().find_last_of(".");
|
||||
if(dot != std::string_view::npos){
|
||||
auto ext = name.sv().substr(dot);
|
||||
if(ext == ".so" || ext == ".dll" || ext == ".dylib"){
|
||||
dylib_entry_t entry = load_dylib(name.c_str());
|
||||
if(!entry){
|
||||
vm->ImportError("cannot load dynamic library: " + name.escape());
|
||||
}
|
||||
vm->_c.s_view.push(ArgsView(vm->s_data.end(), vm->s_data.end()));
|
||||
const char* name = entry(vm, PK_VERSION);
|
||||
vm->_c.s_view.pop();
|
||||
if(name == nullptr){
|
||||
vm->ImportError("module initialization failed: " + Str(name).escape());
|
||||
}
|
||||
return vm->_modules[name];
|
||||
}
|
||||
}
|
||||
return vm->py_import(name);
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user