Skip to content

Commit

Permalink
Revert #23
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed May 14, 2024
1 parent 7d25350 commit 38f7987
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 29 deletions.
7 changes: 0 additions & 7 deletions ports/py-add-vcpkg-dll-path/portfile.cmake

This file was deleted.

8 changes: 0 additions & 8 deletions ports/py-add-vcpkg-dll-path/sitecustomize.py

This file was deleted.

8 changes: 0 additions & 8 deletions ports/py-add-vcpkg-dll-path/vcpkg.json

This file was deleted.

126 changes: 126 additions & 0 deletions ports/python3/add-vcpkg-search-path.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index eeaf20b46..13bb1fe7b 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -32,7 +32,10 @@
#ifdef MS_WINDOWS
# undef BYTE
# include "windows.h"
-
+# include <shlwapi.h>
+# include <string.h>
+# include <malloc.h>
+# include <libloaderapi.h>
extern PyTypeObject PyWindowsConsoleIO_Type;
# define PyWindowsConsoleIO_Check(op) \
(PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type))
@@ -88,8 +91,97 @@ __attribute__ ((section (".PyRuntime")))
= _PyRuntimeState_INIT;
_Py_COMP_DIAG_POP

static int runtime_initialized = 0;

+#if defined(MS_WINDOWS)
+
+typedef void (WINAPI *ADD)(PCWSTR NewDirectory);
+
+static int* is_vcpkg_search_path_added(void) {
+ static int is_search_path_added = 0;
+ return &is_search_path_added;
+}
+
+static ADD getAddDllDirectory(void) {
+ static ADD pAddDllDirectory = NULL;
+ if(pAddDllDirectory == NULL) {
+ pAddDllDirectory = (ADD)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "AddDllDirectory");
+ }
+ return pAddDllDirectory;
+}
+
+PyStatus Init_vcpkg_DllSearchPath(void) {
+ HMODULE dll_handle = NULL;
+ static wchar_t *module_dirname = NULL;
+
+ if (GetModuleHandleExW((GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT),
+ (LPCWSTR) &Init_vcpkg_DllSearchPath, &dll_handle) == 0) {
+ // Getting the pythonxx.dll path failed
+ }
+
+ int buffer_size = 1024;
+ module_dirname = (wchar_t*)malloc(buffer_size*sizeof(wchar_t));
+ if( module_dirname == NULL)
+ return PyStatus_NoMemory();
+ if(GetModuleFileNameW(dll_handle, module_dirname, buffer_size - 1) == 0) {
+ // Call Failed!
+ return PyStatus_Error("vcpkg: Unable to retrieve module file name!");
+ }
+
+ wchar_t *old_module_dirname;
+ while( GetLastError() == ERROR_INSUFFICIENT_BUFFER)
+ {
+ buffer_size += buffer_size/2;
+ old_module_dirname = module_dirname;
+ module_dirname = (wchar_t*)realloc(module_dirname,buffer_size*sizeof(wchar_t));
+ if( module_dirname == NULL) {
+ // No memory?
+ free(old_module_dirname);
+ return PyStatus_NoMemory();
+ }
+ if(GetModuleFileNameW(dll_handle, &module_dirname[0], buffer_size - 1) == 0) {
+ return PyStatus_Error("vcpkg: Unable to retrieve module file name!");
+ }
+ }
+
+ wchar_t* last_path_seperator = wcsrchr(module_dirname, L'\\');
+
+ if(last_path_seperator) {
+ last_path_seperator[0] = L'\0'; // Remove file name from module_dirname
+ }
+
+ const wchar_t python_path[] = L"\\tools\\python3";
+ const size_t python_path_len = wcslen(python_path);
+ const size_t module_dirname_len = wcslen(module_dirname);
+ wchar_t* prefix_path_sep = wcsstr(module_dirname, python_path);
+
+ const size_t remaining_len = (size_t)(prefix_path_sep - module_dirname) - (module_dirname_len-python_path_len);
+
+ if(prefix_path_sep == NULL || remaining_len != 0) {
+ free(module_dirname);
+ return PyStatus_Ok(); // We are somewhere then vcpkg -> do nothing
+ }
+
+ prefix_path_sep[1] = L'b'; // module_dirname is now vcpkg_bin_dir
+ prefix_path_sep[2] = L'i';
+ prefix_path_sep[3] = L'n';
+ prefix_path_sep[4] = L'\0';
+
+ DWORD fileattr = GetFileAttributesW(module_dirname);
+
+ if( fileattr & FILE_ATTRIBUTE_DIRECTORY && !*is_vcpkg_search_path_added()) {
+ ADD pAddDllDirectory = getAddDllDirectory();
+ pAddDllDirectory(module_dirname);
+ fwprintf(stderr, L"Added vcpkg bin path to search for dlls. Path added: %ls \n", module_dirname);
+ *is_vcpkg_search_path_added() = 1;
+ }
+
+ free(module_dirname);
+ return PyStatus_Ok();
+};
+
+#endif
+
PyStatus
_PyRuntime_Initialize(void)
{
@@ -102,6 +193,11 @@ _PyRuntime_Initialize(void)
}
runtime_initialized = 1;

+#ifdef MS_WINDOWS
+ PyStatus err = Init_vcpkg_DllSearchPath();
+ if(PyStatus_IsError(err))
+ return err;
+#endif
return _PyRuntimeState_Init(&_PyRuntime);
}

1 change: 1 addition & 0 deletions ports/python3/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ set(PATCHES
0014-fix-get-python-inc-output.patch
0015-dont-use-WINDOWS-def.patch
0018-fix-sysconfig-include.patch
add-vcpkg-search-path.patch
)

if(VCPKG_LIBRARY_LINKAGE STREQUAL "static")
Expand Down
7 changes: 1 addition & 6 deletions ports/python3/vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "python3",
"version": "3.11.9",
"port-version": 1,
"description": "The Python programming language",
"homepage": "https://github.com/python/cpython",
"license": "Python-2.0",
Expand Down Expand Up @@ -49,11 +48,7 @@
"host": true,
"platform": "windows"
},
"zlib",
{
"name": "py-add-vcpkg-dll-path",
"platform": "windows"
}
"zlib"
],
"features": {
"deprecated-win7-support": {
Expand Down

0 comments on commit 38f7987

Please sign in to comment.