Skip to content

Commit

Permalink
Remove _libparsec.so, and instead embed it within the main `libpyth…
Browse files Browse the repository at this point in the history
…onscript.so`
  • Loading branch information
touilleMan committed Dec 26, 2024
1 parent ff93779 commit 2d90bf1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 73 deletions.
8 changes: 4 additions & 4 deletions src/_pythonscript.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ def _setup_config_entry(name: str, default_value: object):
cdef PythonScriptLanguage _pythons_script_language = None


cdef api GDExtensionObjectPtr _pythonscript_create_instance(
cdef public GDExtensionObjectPtr _pythonscript_create_instance(
void *p_userdata
) noexcept with gil:
return NULL


cdef api void _pythonscript_free_instance(
cdef public void _pythonscript_free_instance(
void *p_userdata, GDExtensionClassInstancePtr p_instance
) noexcept with gil:
pass
Expand Down Expand Up @@ -292,7 +292,7 @@ cdef void _print_banner():
print(f"PYTHONPATH: {sys.path}", flush=True)


cdef api void _pythonscript_initialize(int p_level) noexcept with gil:
cdef public void _pythonscript_initialize(int p_level) noexcept with gil:
if p_level == GDEXTENSION_INITIALIZATION_SERVERS:
_register_pythonscript_classes()

Expand All @@ -309,7 +309,7 @@ cdef api void _pythonscript_initialize(int p_level) noexcept with gil:
_initialize_callback_hook(p_level)


cdef api void _pythonscript_deinitialize(int p_level) noexcept with gil:
cdef public void _pythonscript_deinitialize(int p_level) noexcept with gil:
global _pythons_script_language

# /!\ When this function is called, the Python interpreter is fully operational
Expand Down
93 changes: 29 additions & 64 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
##############################################################################


# Input missing the template file
# List everything `generate_tmpl.py` needs to work, except the template file
# itself, as it is the only thing that changes between the different targets.
generate_tmpl_base_input = [
join_paths(scripts_dir, 'generate_tmpl.py'), # Must stay first !
godot_extension_api_json, # Must stay second !
Expand Down Expand Up @@ -156,7 +157,7 @@ pxds_godot = [


##############################################################################
# _pythonscript.c & _pythonscript_api.h generation #
# _pythonscript.c & _pythonscript.h generation #
##############################################################################


Expand All @@ -176,27 +177,25 @@ foreach pxi: [
endforeach

# _pythonscript.c
# _pythonscript_api.h
# _pythonscript.h
# ├─ _pythonscript.pyx
# ├─ _pythonscript_editor.pxi
# ├─ _pythonscript_registration.pxi
# ├─ godot/hazmat/gdapi.pxd
# ├─ _pythonscript_extension_class_language.pxi
# ├─ _pythonscript_extension_class_script.pxi
# ├─ godot/builtins.pxd
# | └─ ...
# └─ godot/hazmat/gdextension_interface.pxd
# ├─ godot/classes.pxd
# | └─ ...
# ├─ godot/hazmat/*.pxd
# └─ ...


# `_pythonscript_api.h` is needed to compile `libpythonscript.so` (see below)
# Note we don't need to link `libpythonscript.so` against `_pythonscript.so`
# given `_pythonscript_api.h` contains a static inline functions that uses
# Python's import system to dynamically load `_pythonscript.so`

# `_pythonscript.c` & `_pythonscript.h` are needed to compile `libpythonscript.so` (see below)

# `_pythonscript_api.h` is used to expose the Python callbacks used by `pythonscript.c`,
# `_pythonscript.h` is used to expose the Python callbacks used by `pythonscript.c`,
# hence this header is private and won't be provided in the release
c_pythonscript = custom_target('_pythonscript.c & _pythonscript.h & _pythonscript_api.h',
output : ['_pythonscript.c', '_pythonscript_api.h'],
c_pythonscript = custom_target('_pythonscript.c & _pythonscript.h',
output : ['_pythonscript.c', '_pythonscript.h'],
input : [
'_pythonscript.pyx',
'_pythonscript_editor.pxi',
Expand All @@ -205,8 +204,8 @@ c_pythonscript = custom_target('_pythonscript.c & _pythonscript.h & _pythonscrip
],
command : cythonize_command,
)
h_pythonscript_api = c_pythonscript[1] # Header for pythonscript.c
c_pythonscript = c_pythonscript[0]
# h_pythonscript_api = c_pythonscript[1] # Header for pythonscript.c
# c_pythonscript = c_pythonscript[0]


##############################################################################
Expand All @@ -216,7 +215,8 @@ c_pythonscript = c_pythonscript[0]

# libpythonscript.so
# ├─ pythonscript.c
# └─ _pythonscript_api.h
# └─ _pythonscript.c
# └─ _pythonscript.h
# └─ ...


Expand All @@ -232,10 +232,21 @@ else
lib_pythonscript_rpath = ''
endif

# Cython's cdef inline methods are turned into static inline C functions that
# produced `defined but not used` errors, hence the `-Wno-unused-function`
# AutoPxd currently strip const qualifier when generating `gdextension_interface.pxd`,
# hence -Wno-discarded-qualifiers
if host_platform.startswith('windows')
lib__pythonscript_c_args = []
else
lib__pythonscript_c_args = ['-Wno-discarded-qualifiers', '-Wno-incompatible-pointer-types-discards-qualifiers', '-Wno-unused-function']
endif


lib_pythonscript = shared_library(
'pythonscript',
['pythonscript.c', h_pythonscript_api],
['pythonscript.c', c_pythonscript],
c_args: lib__pythonscript_c_args,
dependencies: [dep_godot, dep_python],
install_rpath: lib_pythonscript_rpath, # To find libpython
install: true,
Expand All @@ -253,52 +264,6 @@ dep_pythonscript = declare_dependency(
)


##############################################################################
# _pythonscript.pyx compilation (required by other .pyx) #
##############################################################################


# _pythonscript.so
# ├─ libpythonscript.so
# | └─ ...
# └─ _pythonscript.c
# └─ _pythonscript.pyx
# └─ ...


if host_platform.startswith('linux')
lib__pythonscript_rpath = '$ORIGIN/../..'
elif host_platform.startswith('macos')
lib__pythonscript_rpath = '@loader_path/../..'
else
lib__pythonscript_rpath = ''
endif


# Cython's cdef inline methods are turned into static inline C functions that
# produced `defined but not used` errors, hence the `-Wno-unused-function`
# AutoPxd currently strip const qualifier when generating `gdextension_interface.pxd`,
# hence -Wno-discarded-qualifiers
if host_platform.startswith('windows')
lib__pythonscript_c_args = []
else
lib__pythonscript_c_args = ['-Wno-discarded-qualifiers', '-Wno-incompatible-pointer-types-discards-qualifiers', '-Wno-unused-function']
endif


lib__pythonscript = shared_library(
'_pythonscript',
c_pythonscript,
c_args: lib__pythonscript_c_args,
dependencies : [dep_godot, dep_python, dep_pythonscript],
install_rpath: lib__pythonscript_rpath, # To find libpython
name_prefix: python_native_module_name_prefix,
name_suffix: python_native_module_name_suffix,
install: true,
install_dir: python_site_packages_install_path,
)


##############################################################################
# Load subdirs (and their config) #
##############################################################################
Expand Down
15 changes: 10 additions & 5 deletions src/pythonscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <Python.h>

#include <godot/gdextension_interface.h>
#include "_pythonscript_api.h"
#include "_pythonscript.h"

#ifdef _WIN32
# define DLL_EXPORT __declspec(dllexport)
Expand Down Expand Up @@ -234,6 +234,12 @@ static void _initialize_python() {
}
}

if (PyImport_AppendInittab("_pythonscript", PyInit__pythonscript) == -1) {
GD_PRINT_ERROR("Pythonscript: Cannot extend in-built modules table");
goto error;

}

// TODO
// Update sys.path with projet config
// status = PyWideStringList_Append(&config.module_search_paths,
Expand Down Expand Up @@ -282,16 +288,15 @@ static void _initialize_python() {
PyRun_SimpleString("import sys\nprint('PYTHON_PATH:', sys.path)\n");
#endif


{
int ret = import__pythonscript();
if (ret != 0) {
PyObject *pmodule = PyImport_ImportModule("_pythonscript");
if (!pmodule) {
GD_PRINT_ERROR("Pythonscript: Cannot load Python module `_pythonscript`");
goto post_init_error;
}
Py_DecRef(pmodule);
}


PyConfig_Clear(&config);

// Release the Kraken... er I mean the GIL !
Expand Down

0 comments on commit 2d90bf1

Please sign in to comment.