Skip to content

Commit 2ce3a3c

Browse files
committed
fix running C extensions with the msvc native toolchain
1 parent 4f6ae6e commit 2ce3a3c

File tree

4 files changed

+46
-68
lines changed

4 files changed

+46
-68
lines changed

graalpython/com.oracle.graal.python.cext/include/pyconfig.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@
5151
# define MS_WINDOWS
5252
# define Py_ENABLE_SHARED
5353
# define HAVE_DECLSPEC_DLL
54-
# pragma comment(lib, "python-native.lib")
54+
// This pragma is only understood by MSVC, not our LLVM toolchain, so it's only
55+
// relevant for code that is compiled without bitcode and will run only
56+
// natively. Since the pythonjni library contains all the trampolines to call
57+
// into the python-native.dll in this case, we must only depend on that.
58+
# pragma comment(lib, "pythonjni.lib")
5559
# endif
5660
#endif
5761

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@
4141
#include "capi.h"
4242
#include <stdio.h>
4343
#include <time.h>
44-
#ifdef MS_WINDOWS
45-
#include "libloaderapi.h"
46-
#include "pathcch.h"
47-
#pragma comment(lib, "Pathcch.lib")
48-
#endif
4944

5045
#define ASSERTIONS
5146

@@ -1453,63 +1448,6 @@ PyAPI_FUNC(void) initialize_graal_capi(ptr_cache_t _pythonToNative, void_ptr_cac
14531448
initialize_hashes();
14541449
initialize_bufferprocs();
14551450

1456-
#ifdef MS_WINDOWS
1457-
// when initializing the C API, the appropriate libraries (like
1458-
// python-native.dll or graalvm-llvm.dll) are loaded with their full paths.
1459-
// However, they are not automatically on the search path when any
1460-
// extension modules are loaded later, and Windows wants to resolve them
1461-
// again. So we get their runtime paths here and add those to the dll
1462-
// search path.
1463-
LPSTR lpMsgBuf;
1464-
wchar_t path[MAX_PATH];
1465-
char pathA[MAX_PATH];
1466-
HMODULE hm = NULL;
1467-
if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
1468-
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
1469-
(LPCWSTR) &initialize_graal_capi, &hm) == 0) {
1470-
int ret = GetLastError();
1471-
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1472-
NULL, ret, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &lpMsgBuf, 0, NULL);
1473-
PyTruffle_Log(PY_TRUFFLE_LOG_FINE, "finding python-native.dll handle failed, error = %s\n", lpMsgBuf);
1474-
LocalFree(lpMsgBuf);
1475-
} else {
1476-
if (GetModuleFileNameW(hm, (LPWSTR)path, sizeof(path)) == 0) {
1477-
int ret = GetLastError();
1478-
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1479-
NULL, ret, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &lpMsgBuf, 0, NULL);
1480-
PyTruffle_Log(PY_TRUFFLE_LOG_FINE, "finding python-native.dll path failed, error = %s\n", lpMsgBuf);
1481-
LocalFree(lpMsgBuf);
1482-
} else {
1483-
wcstombs(pathA, path, sizeof(pathA));
1484-
PyTruffle_Log(PY_TRUFFLE_LOG_FINE, "Adding python-native.dll path '%s' to search path.\n", pathA);
1485-
PathCchRemoveFileSpec((PWSTR)path, sizeof(path));
1486-
AddDllDirectory((LPWSTR)path);
1487-
}
1488-
}
1489-
if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
1490-
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
1491-
(LPCWSTR) &polyglot_from_string, &hm) == 0) {
1492-
int ret = GetLastError();
1493-
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1494-
NULL, ret, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &lpMsgBuf, 0, NULL);
1495-
PyTruffle_Log(PY_TRUFFLE_LOG_FINE, "finding graalvm-llvm.dll handle failed, error = %s\n", lpMsgBuf);
1496-
LocalFree(lpMsgBuf);
1497-
} else {
1498-
if (GetModuleFileNameW(hm, (LPWSTR)path, sizeof(path)) == 0) {
1499-
int ret = GetLastError();
1500-
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1501-
NULL, ret, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &lpMsgBuf, 0, NULL);
1502-
PyTruffle_Log(PY_TRUFFLE_LOG_FINE, "finding graalvm-llvm.dll path failed, error = %s\n", lpMsgBuf);
1503-
LocalFree(lpMsgBuf);
1504-
} else {
1505-
wcstombs(pathA, path, sizeof(pathA));
1506-
PyTruffle_Log(PY_TRUFFLE_LOG_FINE, "Adding graalvm-llvm.dll path '%s' to search path.\n", pathA);
1507-
PathCchRemoveFileSpec((LPWSTR)path, sizeof(path));
1508-
AddDllDirectory((LPWSTR)path);
1509-
}
1510-
}
1511-
#endif
1512-
15131451
// TODO: initialize during cext initialization doesn't work at the moment
15141452
// This is hardcoded the same way in capi_native.c
15151453
Py_FileSystemDefaultEncoding = "utf-8"; // strdup(PyUnicode_AsUTF8(GraalPyTruffle_FileSystemDefaultEncoding()));

mx.graalpython/mx_graalpython.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,21 @@ def wants_debug_build(flags=os.environ.get("CFLAGS", "")):
116116

117117

118118
if wants_debug_build():
119-
mx_native.DefaultNativeProject.cflags = property(lambda self: self._cflags + ["-fPIC", "-ggdb3"])
119+
mx_native.DefaultNativeProject._original_cflags = mx_native.DefaultNativeProject.cflags
120+
mx_native.DefaultNativeProject.cflags = property(
121+
lambda self: self._original_cflags + (["/Z7"] if WIN32 else ["-fPIC", "-ggdb3"])
122+
)
123+
124+
125+
if WIN32:
126+
# we need the .lib for pythonjni
127+
original_DefaultNativeProject_getArchivableResults = mx_native.DefaultNativeProject.getArchivableResults
128+
def getArchivableResultsWithLib(self, *args, **kwargs):
129+
for result in original_DefaultNativeProject_getArchivableResults(self, *args, **kwargs):
130+
if any(r.endswith("pythonjni.dll") for r in result):
131+
yield tuple(r.replace(".dll", ".lib") for r in result)
132+
yield result
133+
mx_native.DefaultNativeProject.getArchivableResults = getArchivableResultsWithLib
120134

121135

122136
def _sibling(filename):
@@ -1147,6 +1161,9 @@ def graalpython_gate_runner(args, tasks):
11471161
report = lambda: (not is_collecting_coverage()) and task
11481162
nonZeroIsFatal = not is_collecting_coverage()
11491163
if WIN32:
1164+
# Windows support is still experimental. we exclude a number of our
1165+
# unittests on Windows for now. If you add unittests and cannot get
1166+
# them to work on Windows, yet, add their files here.
11501167
excluded_tests = [
11511168
"test_zipimport.py", # sys.getwindowsversion
11521169
"test_thread", # sys.getwindowsversion
@@ -1158,8 +1175,27 @@ def graalpython_gate_runner(args, tasks):
11581175
"test_imports", # import posix
11591176
"test_ctypes_callbacks.py", # ctypes error
11601177
"test_code.py", # forward slash in path problem
1161-
"test_csv.py", # module 'os' has no attribute 'O_TEMPORARY'
1162-
"*/cpyext/*",
1178+
"*/cpyext/test_wiki.py",
1179+
"*/cpyext/test_unicode.py",
1180+
"*/cpyext/test_thread.py",
1181+
"*/cpyext/test_simple.py",
1182+
"*/cpyext/test_object.py",
1183+
"*/cpyext/test_modsupport.py",
1184+
"*/cpyext/test_mmap.py",
1185+
"*/cpyext/test_mixed_inheritance.py",
1186+
"*/cpyext/test_method.py",
1187+
"*/cpyext/test_memoryview.py",
1188+
"*/cpyext/test_member.py",
1189+
"*/cpyext/test_long.py",
1190+
"*/cpyext/test_functions.py",
1191+
"*/cpyext/test_float.py",
1192+
"*/cpyext/test_exceptionobject.py",
1193+
"*/cpyext/test_err.py",
1194+
"*/cpyext/test_descr.py",
1195+
"*/cpyext/test_datetime.py",
1196+
"*/cpyext/test_bytes.py",
1197+
"*/cpyext/test_bool.py",
1198+
"*/cpyext/test_abstract.py",
11631199
]
11641200
else:
11651201
excluded_tests = []

mx.graalpython/suite.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,15 +980,15 @@
980980
"extracted-dependency:graalpython:GRAALPYTHON_PYTHON_LIB",
981981
],
982982
"./libs/": [
983-
"extracted-dependency:GRAALPYTHON_NATIVE_LIBS/python-native.lib",
983+
"extracted-dependency:GRAALPYTHON_NATIVE_LIBS/pythonjni.lib",
984984
],
985985
"./lib-graalpython/": [
986986
"file:graalpython/lib-graalpython/*",
987987
{
988988
"source_type": "extracted-dependency",
989989
"dependency": "GRAALPYTHON_NATIVE_LIBS",
990990
"path": "*",
991-
"exclude": ["python-native.lib"],
991+
"exclude": ["pythonjni.lib"],
992992
},
993993
"file:graalpython/com.oracle.graal.python.cext/CEXT-WINDOWS-README.md",
994994
],

0 commit comments

Comments
 (0)