Skip to content

Commit 53a6def

Browse files
committed
another push of fix hoping it succeeds this time (lmao)
1 parent db73d86 commit 53a6def

3 files changed

Lines changed: 168 additions & 77 deletions

File tree

CMakeLists.txt

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,79 @@
11
cmake_minimum_required(VERSION 3.21)
22

3-
# Set C++ standard to 20 for compatibility with Geode's {fmt}
3+
# Standard C++20 for compatibility with modern Geode and {fmt}
44
set(CMAKE_CXX_STANDARD 20)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6-
76
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
87

9-
if (Python3_ROOT_DIR)
10-
set(Python3_FIND_STRATEGY LOCATION)
11-
endif()
8+
# --- Python Discovery ---
9+
# We need to find the Python development headers and libraries.
10+
# On mobile (Android/iOS), we often rely on prebuilt versions.
1211

1312
if (ANDROID)
14-
if (NOT DEFINED PREBUILT_PYTHON_PATH)
15-
if (Python3_ROOT_DIR)
16-
set(PREBUILT_PYTHON_PATH ${Python3_ROOT_DIR})
17-
else()
18-
message(WARNING "PREBUILT_PYTHON_PATH and Python3_ROOT_DIR not set for Android!")
19-
endif()
13+
message(STATUS "Configuring Python for Android...")
14+
15+
# I literally spent all of yesterday fighting with Android's find_library.
16+
# Why is it so inconsistent with the NDK? If this pathing logic
17+
# ever breaks again, I'm quitting.
18+
if (NOT DEFINED PREBUILT_PYTHON_PATH AND Python3_ROOT_DIR)
19+
set(PREBUILT_PYTHON_PATH ${Python3_ROOT_DIR})
2020
endif()
2121

2222
if (PREBUILT_PYTHON_PATH)
23+
message(STATUS "Using prebuilt Python from: ${PREBUILT_PYTHON_PATH}")
2324
add_library(Python3_Mobile INTERFACE)
24-
target_include_directories(Python3_Mobile INTERFACE ${PREBUILT_PYTHON_PATH}/include)
25+
target_include_directories(Python3_Mobile INTERFACE "${PREBUILT_PYTHON_PATH}/include")
2526

26-
find_library(PYTHON_LIB python3.11 PATHS ${PREBUILT_PYTHON_PATH}/lib NO_DEFAULT_PATH)
27+
# We try to find the specific library version
28+
find_library(PYTHON_LIB NAMES python3.11 python3 PATHS "${PREBUILT_PYTHON_PATH}/lib" NO_DEFAULT_PATH)
2729
if (NOT PYTHON_LIB)
28-
find_library(PYTHON_LIB python3 PATHS ${PREBUILT_PYTHON_PATH}/lib NO_DEFAULT_PATH)
30+
message(FATAL_ERROR "Could not find Python library in ${PREBUILT_PYTHON_PATH}/lib")
2931
endif()
30-
target_link_libraries(Python3_Mobile INTERFACE ${PYTHON_LIB})
3132

33+
target_link_libraries(Python3_Mobile INTERFACE ${PYTHON_LIB})
3234
set(PYTHON_TARGET Python3_Mobile)
3335
else()
34-
message(STATUS "Python Runtime will attempt to find Python despite no PREBUILT_PYTHON_PATH provided.")
36+
message(WARNING "Neither PREBUILT_PYTHON_PATH nor Python3_ROOT_DIR is set for Android. Build might fail.")
3537
find_package(Python3 REQUIRED COMPONENTS Development.Embed)
3638
set(PYTHON_TARGET Python3::Python)
3739
endif()
3840
else()
41+
# On desktop platforms, standard discovery is usually fine.
3942
find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Embed)
4043
set(PYTHON_TARGET Python3::Python)
44+
message(STATUS "Found Python: ${Python3_VERSION} at ${Python3_EXECUTABLE}")
4145
endif()
4246

47+
# --- Project Setup ---
4348
project(PythonRuntime VERSION 1.0.0)
4449

50+
# Automatically pick up any .cpp files in the src/ directory
4551
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*.cpp)
52+
4653
add_library(${PROJECT_NAME} SHARED ${SOURCES})
4754

48-
# Use plain linking to match Geode's internal style
55+
# --- Linking & Includes ---
4956
target_link_libraries(${PROJECT_NAME} ${PYTHON_TARGET})
5057

51-
if (NOT ANDROID)
52-
target_include_directories(${PROJECT_NAME} PRIVATE ${Python3_INCLUDE_DIRS})
53-
endif()
54-
58+
# Ensure we have access to our own public headers
5559
target_include_directories(${PROJECT_NAME} INTERFACE include)
60+
61+
# This define is used in PythonRuntime.hpp to handle DLL exports
5662
target_compile_definitions(${PROJECT_NAME} PRIVATE PYTHON_RUNTIME_EXPORT)
5763

64+
# Desktop-specific includes if find_package was used
65+
if (NOT ANDROID AND Python3_INCLUDE_DIRS)
66+
target_include_directories(${PROJECT_NAME} PRIVATE ${Python3_INCLUDE_DIRS})
67+
endif()
68+
69+
# --- Geode Integration ---
70+
# Geode is required for this mod to function.
5871
if (NOT DEFINED ENV{GEODE_SDK})
59-
message(FATAL_ERROR "Unable to find Geode SDK! Please define GEODE_SDK environment variable to point to Geode")
72+
message(FATAL_ERROR "GEODE_SDK environment variable is not set! Please point it to your Geode SDK directory.")
6073
endif()
6174

75+
message(STATUS "Geode SDK found at: $ENV{GEODE_SDK}")
6276
add_subdirectory($ENV{GEODE_SDK} ${CMAKE_CURRENT_BINARY_DIR}/geode)
6377

78+
# Finalize the Geode mod setup
6479
setup_geode_mod(${PROJECT_NAME} VERSION 4.0.0)

include/PythonRuntime.hpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#include <string>
55
#include <filesystem>
66

7+
/**
8+
* DLL Export/Import macros for Windows.
9+
* This allows other mods to potentially use the PythonRuntime class if they link against it.
10+
*/
711
#ifdef GEODE_IS_WINDOWS
812
#ifdef PYTHON_RUNTIME_EXPORT
913
#define PYTHON_API __declspec(dllexport)
@@ -14,30 +18,44 @@
1418
#define PYTHON_API [[gnu::visibility("default")]]
1519
#endif
1620

21+
/**
22+
* The main interface for interacting with the Python environment within Geode.
23+
* This class handles initialization, dependency management, and script execution.
24+
*/
1725
class PYTHON_API PythonRuntime {
1826
public:
1927
/**
20-
* @brief Initialize the Python environment for a specific mod.
28+
* Scans a mod for Python-related content (main.py, requirements.txt, etc.)
29+
* and sets up the environment for that specific mod.
30+
* @param mod The mod to initialize.
2131
*/
2232
static void initializeForMod(geode::Mod* mod);
2333

2434
/**
25-
* @brief Run a python string in the context of a mod.
35+
* Executes a raw string of Python code.
36+
* @param code The Python source code to run.
37+
* @param mod Optional mod context (currently unused but reserved for future scoped execution).
2638
*/
2739
static void runString(std::string const& code, geode::Mod* mod = nullptr);
2840

2941
/**
30-
* @brief Run a python file in the context of a mod.
42+
* Loads and executes a Python script from the filesystem.
43+
* @param path Path to the .py file.
44+
* @param mod Optional mod context.
3145
*/
3246
static void runFile(std::filesystem::path const& path, geode::Mod* mod = nullptr);
3347

3448
/**
35-
* @brief Setup the security sandbox.
49+
* Configures the Python audit hooks and basic security restrictions.
50+
* This is called once during the initial startup of the runtime.
3651
*/
3752
static void setupSandbox();
3853

3954
/**
40-
* @brief Check and install dependencies for a mod.
55+
* Checks for dependency files (.env or requirements.txt) and attempts
56+
* to install them into the mod's 'site-packages' directory using pip.
57+
* Note: This is a no-op on mobile platforms.
58+
* @param mod The mod to check dependencies for.
4159
*/
4260
static void ensureDependencies(geode::Mod* mod);
4361
};

0 commit comments

Comments
 (0)