mirror of
https://github.com/pocketpy/pocketpy
synced 2026-03-24 14:10:18 +00:00
Compare commits
7 Commits
42bbf6fdb4
...
f05f48631d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f05f48631d | ||
|
|
e61b5cdc44 | ||
|
|
d7607ce398 | ||
|
|
2c7f0cdd07 | ||
|
|
0f1abcb1f6 | ||
|
|
53fa193355 | ||
|
|
41b396df9a |
@ -44,6 +44,11 @@ if(PK_ENABLE_OS)
|
|||||||
add_definitions(-DPK_ENABLE_OS=1)
|
add_definitions(-DPK_ENABLE_OS=1)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
option(PK_MODULE_WIN32 "" OFF)
|
||||||
|
if(PK_MODULE_WIN32)
|
||||||
|
add_definitions(-DPK_MODULE_WIN32=1)
|
||||||
|
endif()
|
||||||
|
|
||||||
# PK_IS_MAIN determines whether the project is being used from root
|
# PK_IS_MAIN determines whether the project is being used from root
|
||||||
# or if it is added as a dependency (through add_subdirectory for example).
|
# or if it is added as a dependency (through add_subdirectory for example).
|
||||||
if ("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
|
if ("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||||
@ -56,6 +61,8 @@ else()
|
|||||||
option(PK_BUILD_STATIC_LIB "Build static library" ON)
|
option(PK_BUILD_STATIC_LIB "Build static library" ON)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
option(PK_BUILD_STATIC_MAIN "Build static main" OFF)
|
||||||
|
|
||||||
if(PK_BUILD_SHARED_LIB)
|
if(PK_BUILD_SHARED_LIB)
|
||||||
message(">> Building shared library")
|
message(">> Building shared library")
|
||||||
add_library(${PROJECT_NAME} SHARED ${POCKETPY_SRC})
|
add_library(${PROJECT_NAME} SHARED ${POCKETPY_SRC})
|
||||||
@ -63,8 +70,13 @@ elseif(PK_BUILD_STATIC_LIB)
|
|||||||
message(">> Building static library")
|
message(">> Building static library")
|
||||||
add_library(${PROJECT_NAME} STATIC ${POCKETPY_SRC})
|
add_library(${PROJECT_NAME} STATIC ${POCKETPY_SRC})
|
||||||
else()
|
else()
|
||||||
|
if(PK_BUILD_STATIC_MAIN)
|
||||||
|
message(">> Building static library + executable")
|
||||||
|
add_library(${PROJECT_NAME} STATIC ${POCKETPY_SRC})
|
||||||
|
else()
|
||||||
message(">> Building shared library + executable")
|
message(">> Building shared library + executable")
|
||||||
add_library(${PROJECT_NAME} SHARED ${POCKETPY_SRC})
|
add_library(${PROJECT_NAME} SHARED ${POCKETPY_SRC})
|
||||||
|
endif()
|
||||||
add_executable(main src2/main.c)
|
add_executable(main src2/main.c)
|
||||||
target_link_libraries(main ${PROJECT_NAME})
|
target_link_libraries(main ${PROJECT_NAME})
|
||||||
endif()
|
endif()
|
||||||
@ -73,3 +85,7 @@ if(UNIX)
|
|||||||
target_link_libraries(${PROJECT_NAME} m)
|
target_link_libraries(${PROJECT_NAME} m)
|
||||||
target_link_libraries(${PROJECT_NAME} dl)
|
target_link_libraries(${PROJECT_NAME} dl)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if(PK_MODULE_WIN32)
|
||||||
|
target_link_libraries(${PROJECT_NAME} winmm.lib)
|
||||||
|
endif()
|
||||||
|
|||||||
@ -7,28 +7,36 @@ assert os.system("python prebuild.py") == 0
|
|||||||
if not os.path.exists("build"):
|
if not os.path.exists("build"):
|
||||||
os.mkdir("build")
|
os.mkdir("build")
|
||||||
|
|
||||||
assert len(sys.argv) <= 2
|
# python cmake_build.py [Debug|Release|RelWithDebInfo] ...
|
||||||
|
|
||||||
if len(sys.argv) == 2:
|
if len(sys.argv) > 1:
|
||||||
config = sys.argv[1]
|
config = sys.argv[1]
|
||||||
else:
|
else:
|
||||||
config = 'Release'
|
config = 'Release'
|
||||||
|
|
||||||
|
extra_flags = " ".join(sys.argv[2:])
|
||||||
|
|
||||||
assert config in ['Debug', 'Release', 'RelWithDebInfo']
|
assert config in ['Debug', 'Release', 'RelWithDebInfo']
|
||||||
|
|
||||||
os.chdir("build")
|
os.chdir("build")
|
||||||
|
|
||||||
code = os.system(f"cmake .. -DPK_ENABLE_OS=ON -DCMAKE_BUILD_TYPE={config}")
|
code = os.system(f"cmake .. -DPK_ENABLE_OS=ON -DCMAKE_BUILD_TYPE={config} {extra_flags}")
|
||||||
assert code == 0
|
assert code == 0
|
||||||
code = os.system(f"cmake --build . --config {config}")
|
code = os.system(f"cmake --build . --config {config}")
|
||||||
assert code == 0
|
assert code == 0
|
||||||
|
|
||||||
if sys.platform == "win32":
|
if sys.platform == "win32":
|
||||||
shutil.copy(f"{config}/main.exe", "../main.exe")
|
shutil.copy(f"{config}/main.exe", "../main.exe")
|
||||||
shutil.copy(f"{config}/pocketpy.dll", "../pocketpy.dll")
|
dll_path = f"{config}/pocketpy.dll"
|
||||||
|
if os.path.exists(dll_path):
|
||||||
|
shutil.copy(dll_path, "../pocketpy.dll")
|
||||||
elif sys.platform == "darwin":
|
elif sys.platform == "darwin":
|
||||||
shutil.copy("main", "../main")
|
shutil.copy("main", "../main")
|
||||||
shutil.copy("libpocketpy.dylib", "../libpocketpy.dylib")
|
dll_path = "libpocketpy.dylib"
|
||||||
|
if os.path.exists(dll_path):
|
||||||
|
shutil.copy(dll_path, "../libpocketpy.dylib")
|
||||||
else:
|
else:
|
||||||
shutil.copy("main", "../main")
|
shutil.copy("main", "../main")
|
||||||
shutil.copy("libpocketpy.so", "../libpocketpy.so")
|
dll_path = "libpocketpy.so"
|
||||||
|
if os.path.exists(dll_path):
|
||||||
|
shutil.copy(dll_path, "../libpocketpy.so")
|
||||||
|
|||||||
@ -1,7 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
void pk__add_module_pkpy();
|
|
||||||
void pk__add_module_conio();
|
|
||||||
void pk__add_module_os();
|
void pk__add_module_os();
|
||||||
void pk__add_module_sys();
|
void pk__add_module_sys();
|
||||||
void pk__add_module_math();
|
void pk__add_module_math();
|
||||||
@ -15,3 +13,5 @@ void pk__add_module_traceback();
|
|||||||
void pk__add_module_enum();
|
void pk__add_module_enum();
|
||||||
void pk__add_module_linalg();
|
void pk__add_module_linalg();
|
||||||
void pk__add_module_array2d();
|
void pk__add_module_array2d();
|
||||||
|
|
||||||
|
void pk__add_module_win32();
|
||||||
@ -4,60 +4,60 @@
|
|||||||
|
|
||||||
namespace pkbind {
|
namespace pkbind {
|
||||||
|
|
||||||
class module : public object {
|
class module_ : public object {
|
||||||
PKBIND_TYPE_IMPL(object, module, tp_module)
|
PKBIND_TYPE_IMPL(object, module_, tp_module)
|
||||||
|
|
||||||
static module __main__() { return module(py_getmodule("__main__"), object::ref_t{}); }
|
static module_ __main__() { return module_(py_getmodule("__main__"), object::ref_t{}); }
|
||||||
|
|
||||||
static module import(const char* name) {
|
static module_ import(const char* name) {
|
||||||
raise_call<py_import>(name);
|
raise_call<py_import>(name);
|
||||||
return borrow<module>(py_retval());
|
return borrow<module_>(py_retval());
|
||||||
}
|
}
|
||||||
|
|
||||||
static module create(const char* name) {
|
static module_ create(const char* name) {
|
||||||
auto m = py_newmodule(name);
|
auto m = py_newmodule(name);
|
||||||
return steal<module>(m);
|
return steal<module_>(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
module def_submodule(const char* name, const char* doc = nullptr) {
|
module_ def_submodule(const char* name, const char* doc = nullptr) {
|
||||||
// auto package = (attr("__package__").cast<std::string>() += ".") +=
|
// auto package = (attr("__package__").cast<std::string>() += ".") +=
|
||||||
// attr("__name__").cast<std::string_view>();
|
// attr("__name__").cast<std::string_view>();
|
||||||
auto fname = (attr("__name__").cast<std::string>() += ".") += name;
|
auto fname = (attr("__name__").cast<std::string>() += ".") += name;
|
||||||
auto m = py_newmodule(fname.c_str());
|
auto m = py_newmodule(fname.c_str());
|
||||||
setattr(*this, name, m);
|
setattr(*this, name, m);
|
||||||
return module(m, object::ref_t{});
|
return module_(m, object::ref_t{});
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Fn, typename... Extras>
|
template <typename Fn, typename... Extras>
|
||||||
module& def(const char* name, Fn&& fn, const Extras... extras) {
|
module_& def(const char* name, Fn&& fn, const Extras... extras) {
|
||||||
impl::bind_function<false, false>(*this, name, std::forward<Fn>(fn), extras...);
|
impl::bind_function<false, false>(*this, name, std::forward<Fn>(fn), extras...);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
using module_ = module;
|
using module = module_;
|
||||||
|
|
||||||
#define PYBIND11_EMBEDDED_MODULE(name, variable) \
|
#define PYBIND11_EMBEDDED_MODULE(name, variable) \
|
||||||
static void _pkbind_register_##name(::pkbind::module& variable); \
|
static void _pkbind_register_##name(::pkbind::module_& variable); \
|
||||||
namespace pkbind::impl { \
|
namespace pkbind::impl { \
|
||||||
auto _module_##name = [] { \
|
auto _module_##name = [] { \
|
||||||
::pkbind::action::register_start([] { \
|
::pkbind::action::register_start([] { \
|
||||||
auto m = ::pkbind::module(py_newmodule(#name), ::pkbind::object::ref_t{}); \
|
auto m = ::pkbind::module_::create(#name); \
|
||||||
_pkbind_register_##name(m); \
|
_pkbind_register_##name(m); \
|
||||||
}); \
|
}); \
|
||||||
return 1; \
|
return 1; \
|
||||||
}(); \
|
}(); \
|
||||||
} \
|
} \
|
||||||
static void _pkbind_register_##name(::pkbind::module& variable)
|
static void _pkbind_register_##name(::pkbind::module_& variable)
|
||||||
|
|
||||||
#define PYBIND11_MODULE(name, variable) \
|
#define PYBIND11_MODULE(name, variable) \
|
||||||
static void _pkbind_register_##name(::pkbind::module& variable); \
|
static void _pkbind_register_##name(::pkbind::module_& variable); \
|
||||||
extern "C" PK_EXPORT bool py_module_initialize() { \
|
extern "C" PK_EXPORT bool py_module_initialize() { \
|
||||||
auto m = ::pkbind::module::create(#name); \
|
auto m = ::pkbind::module_::create(#name); \
|
||||||
_pkbind_register_##name(m); \
|
_pkbind_register_##name(m); \
|
||||||
py_assign(py_retval(), m.ptr()); \
|
py_assign(py_retval(), m.ptr()); \
|
||||||
return true; \
|
return true; \
|
||||||
} \
|
} \
|
||||||
static void _pkbind_register_##name(::pkbind::module& variable)
|
static void _pkbind_register_##name(::pkbind::module_& variable)
|
||||||
|
|
||||||
} // namespace pkbind
|
} // namespace pkbind
|
||||||
|
|||||||
@ -1,2 +0,0 @@
|
|||||||
def _kbhit() -> int: ...
|
|
||||||
def _getch() -> int: ...
|
|
||||||
@ -1 +0,0 @@
|
|||||||
from typing import Any
|
|
||||||
4
include/typings/win32.pyi
Normal file
4
include/typings/win32.pyi
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
def _kbhit() -> int: ...
|
||||||
|
def _getch() -> int: ...
|
||||||
|
|
||||||
|
def PlaySoundA(pszSound: str, hmod: int, fdwSound: int) -> bool: ...
|
||||||
@ -204,9 +204,6 @@ void VM__ctor(VM* self) {
|
|||||||
pk__add_module_array2d();
|
pk__add_module_array2d();
|
||||||
|
|
||||||
// add modules
|
// add modules
|
||||||
pk__add_module_pkpy();
|
|
||||||
pk__add_module_conio();
|
|
||||||
|
|
||||||
pk__add_module_os();
|
pk__add_module_os();
|
||||||
pk__add_module_sys();
|
pk__add_module_sys();
|
||||||
pk__add_module_math();
|
pk__add_module_math();
|
||||||
@ -219,6 +216,9 @@ void VM__ctor(VM* self) {
|
|||||||
pk__add_module_traceback();
|
pk__add_module_traceback();
|
||||||
pk__add_module_enum();
|
pk__add_module_enum();
|
||||||
|
|
||||||
|
// add win32 module
|
||||||
|
pk__add_module_win32();
|
||||||
|
|
||||||
// add python builtins
|
// add python builtins
|
||||||
do {
|
do {
|
||||||
bool ok;
|
bool ok;
|
||||||
|
|||||||
@ -1,32 +0,0 @@
|
|||||||
#include "pocketpy/pocketpy.h"
|
|
||||||
|
|
||||||
void pk__add_module_pkpy() { py_newmodule("pkpy"); }
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
|
|
||||||
#include <conio.h>
|
|
||||||
|
|
||||||
static bool conio__kbhit(int argc, py_Ref argv) {
|
|
||||||
PY_CHECK_ARGC(0);
|
|
||||||
int ret = _kbhit();
|
|
||||||
py_newint(py_retval(), ret);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool conio__getch(int argc, py_Ref argv) {
|
|
||||||
PY_CHECK_ARGC(0);
|
|
||||||
int ret = _getch();
|
|
||||||
py_newint(py_retval(), ret);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void pk__add_module_conio() {
|
|
||||||
py_Ref mod = py_newmodule("conio");
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
py_bindfunc(mod, "_kbhit", conio__kbhit);
|
|
||||||
py_bindfunc(mod, "_getch", conio__getch);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
47
src/modules/win32.c
Normal file
47
src/modules/win32.c
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
#include "pocketpy/pocketpy.h"
|
||||||
|
|
||||||
|
#if defined(_WIN32) && defined(PK_MODULE_WIN32)
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
#include <conio.h>
|
||||||
|
|
||||||
|
static bool win32__kbhit(int argc, py_Ref argv) {
|
||||||
|
PY_CHECK_ARGC(0);
|
||||||
|
int ret = _kbhit();
|
||||||
|
py_newint(py_retval(), ret);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool win32__getch(int argc, py_Ref argv) {
|
||||||
|
PY_CHECK_ARGC(0);
|
||||||
|
int ret = _getch();
|
||||||
|
py_newint(py_retval(), ret);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool win32_PlaySoundA(int argc, py_Ref argv) {
|
||||||
|
PY_CHECK_ARGC(3);
|
||||||
|
PY_CHECK_ARG_TYPE(0, tp_str);
|
||||||
|
PY_CHECK_ARG_TYPE(1, tp_int);
|
||||||
|
PY_CHECK_ARG_TYPE(2, tp_int);
|
||||||
|
const char* pszSound = py_tostr(argv);
|
||||||
|
py_i64 hmod = py_toint(py_arg(1));
|
||||||
|
py_i64 fdwSound = py_toint(py_arg(2));
|
||||||
|
int ret = PlaySoundA(pszSound, (HMODULE)hmod, fdwSound);
|
||||||
|
py_newbool(py_retval(), ret);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
void pk__add_module_win32() {
|
||||||
|
#if defined(_WIN32) && defined(PK_MODULE_WIN32)
|
||||||
|
py_Ref mod = py_newmodule("win32");
|
||||||
|
|
||||||
|
py_bindfunc(mod, "_kbhit", win32__kbhit);
|
||||||
|
py_bindfunc(mod, "_getch", win32__getch);
|
||||||
|
|
||||||
|
py_bindfunc(mod, "PlaySoundA", win32_PlaySoundA);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user