diff --git a/ports/py-add-vcpkg-dll-path/portfile.cmake b/ports/py-add-vcpkg-dll-path/portfile.cmake deleted file mode 100644 index 0ec18a3..0000000 --- a/ports/py-add-vcpkg-dll-path/portfile.cmake +++ /dev/null @@ -1,7 +0,0 @@ -file(INSTALL - "${CMAKE_CURRENT_LIST_DIR}/sitecustomize.py" - DESTINATION "${CURRENT_PACKAGES_DIR}/tools/python3/Lib") - -vcpkg_install_copyright(FILE_LIST "${VCPKG_ROOT_DIR}/LICENSE.txt") - -set(VCPKG_POLICY_EMPTY_INCLUDE_FOLDER enabled) diff --git a/ports/py-add-vcpkg-dll-path/sitecustomize.py b/ports/py-add-vcpkg-dll-path/sitecustomize.py deleted file mode 100644 index 967fc9e..0000000 --- a/ports/py-add-vcpkg-dll-path/sitecustomize.py +++ /dev/null @@ -1,8 +0,0 @@ -import os -import sys -from pathlib import Path - -vcpkg_bin_path = Path(sys.prefix) / '/../../bin' -if not vcpkg_bin_path.is_dir(): - raise RuntimeError(f'Could not add "{vcpkg_bin_path}" to dll paths because it does not exist. If you copied the files from vcpkg either delete this file or adjust it accordingly.') -os.add_dll_directory(vcpkg_bin_path) diff --git a/ports/py-add-vcpkg-dll-path/vcpkg.json b/ports/py-add-vcpkg-dll-path/vcpkg.json deleted file mode 100644 index bcb285b..0000000 --- a/ports/py-add-vcpkg-dll-path/vcpkg.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "name": "py-add-vcpkg-dll-path", - "version-date": "2024-03-13", - "description": "Adds the vcpkg dll path to python", - "documentation": "https://vcpkg.io/en/docs/README.html", - "license": "MIT", - "supports": "windows" -} diff --git a/ports/python3/add-vcpkg-search-path.patch b/ports/python3/add-vcpkg-search-path.patch new file mode 100644 index 0000000..5236c4c --- /dev/null +++ b/ports/python3/add-vcpkg-search-path.patch @@ -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 ++# include ++# include ++# include + 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); + } + diff --git a/ports/python3/portfile.cmake b/ports/python3/portfile.cmake index cd32fd1..b24ec25 100644 --- a/ports/python3/portfile.cmake +++ b/ports/python3/portfile.cmake @@ -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") diff --git a/ports/python3/vcpkg.json b/ports/python3/vcpkg.json index e68e603..108a175 100644 --- a/ports/python3/vcpkg.json +++ b/ports/python3/vcpkg.json @@ -1,7 +1,7 @@ { "name": "python3", "version": "3.11.9", - "port-version": 1, + "port-version": 2, "description": "The Python programming language", "homepage": "https://github.com/python/cpython", "license": "Python-2.0", @@ -49,11 +49,7 @@ "host": true, "platform": "windows" }, - "zlib", - { - "name": "py-add-vcpkg-dll-path", - "platform": "windows" - } + "zlib" ], "features": { "deprecated-win7-support": {