From 3915a942384f297195cfd3d581339ba6b4b66fd2 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Sun, 10 May 2020 16:31:28 +0200 Subject: [PATCH 1/4] Fix geninterop.py include paths --- tools/geninterop/geninterop.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/geninterop/geninterop.py b/tools/geninterop/geninterop.py index 1f4751939..902296229 100644 --- a/tools/geninterop/geninterop.py +++ b/tools/geninterop/geninterop.py @@ -174,6 +174,8 @@ def preprocess_python_headers(): include_py = sysconfig.get_config_var("INCLUDEPY") include_dirs.append(include_py) + include_args = [c for p in include_dirs for c in ["-I", p]] + defines = [ "-D", "__attribute__(x)=", "-D", "__inline__=inline", @@ -198,7 +200,7 @@ def preprocess_python_headers(): defines.extend(("-D", "PYTHON_WITH_WIDE_UNICODE")) python_h = os.path.join(include_py, "Python.h") - cmd = ["clang", "-pthread", "-I"] + include_dirs + defines + ["-E", python_h] + cmd = ["clang", "-pthread"] + include_args + defines + ["-E", python_h] # normalize as the parser doesn't like windows line endings. lines = [] From 57815f6277131bff0acd34466c12e3042579c5e4 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Sun, 10 May 2020 21:17:39 +0200 Subject: [PATCH 2/4] Ensure that monoclr is linked as a C++ library --- setup.py | 1 + src/monoclr/pynetclr.h | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/setup.py b/setup.py index 2dc3e2ca8..ed0852bb5 100644 --- a/setup.py +++ b/setup.py @@ -404,6 +404,7 @@ def _build_monoclr(self): # build the clr python module clr_ext = Extension( "clr", + language="c++", sources=["src/monoclr/pynetinit.c", "src/monoclr/clrmod.c"], extra_compile_args=cflags.split(" "), extra_link_args=libs.split(" "), diff --git a/src/monoclr/pynetclr.h b/src/monoclr/pynetclr.h index 5b25ee8c9..1863b1d43 100644 --- a/src/monoclr/pynetclr.h +++ b/src/monoclr/pynetclr.h @@ -12,6 +12,10 @@ #define MONO_DOMAIN "Python.Runtime" #define PR_ASSEMBLY "Python.Runtime.dll" +#ifdef __cplusplus +extern "C" { +#endif + typedef struct { MonoDomain *domain; @@ -29,4 +33,8 @@ void PyNet_Finalize(PyNet_Args *); void main_thread_handler(PyNet_Args *user_data); char *PyNet_ExceptionToString(MonoObject *); +#ifdef __cplusplus +} +#endif + #endif // PYNET_CLR_H From 7b155124692637f8b629389d1dea8f4d3effb661 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Sun, 10 May 2020 22:37:28 +0200 Subject: [PATCH 3/4] Force preload libmono and fix bug in LD_LIBRARY_PATH forwarding --- src/monoclr/pynetinit.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/monoclr/pynetinit.c b/src/monoclr/pynetinit.c index 7208878de..aeaa07d4a 100644 --- a/src/monoclr/pynetinit.c +++ b/src/monoclr/pynetinit.c @@ -19,6 +19,11 @@ PyNet_Args *PyNet_Init(int ext) pn_args->shutdown = NULL; pn_args->module = NULL; +#ifdef __linux__ + // Force preload libmono-2.0 as global + dlopen("libmono-2.0.so", RTLD_LAZY | RTLD_GLOBAL); +#endif + if (ext == 0) { pn_args->init_name = "Python.Runtime:Initialize()"; @@ -122,7 +127,7 @@ void main_thread_handler(PyNet_Args *user_data) strcpy(new_ld_library_path, py_libdir); strcat(new_ld_library_path, ":"); strcat(new_ld_library_path, ld_library_path); - setenv("LD_LIBRARY_PATH", py_libdir, 1); + setenv("LD_LIBRARY_PATH", new_ld_library_path, 1); } } From a3c29cde8c277ace25d18b9ba5be874948cd6efc Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Mon, 11 May 2020 10:13:33 +0200 Subject: [PATCH 4/4] Update pynetinit.c --- src/monoclr/pynetinit.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/monoclr/pynetinit.c b/src/monoclr/pynetinit.c index aeaa07d4a..149d52296 100644 --- a/src/monoclr/pynetinit.c +++ b/src/monoclr/pynetinit.c @@ -20,7 +20,11 @@ PyNet_Args *PyNet_Init(int ext) pn_args->module = NULL; #ifdef __linux__ - // Force preload libmono-2.0 as global + // Force preload libmono-2.0 as global. Without this, on some systems + // symbols from libmono are not found by libmononative (which implements + // some of the System.* namespaces). Since the only happened on Linux so + // far, we hardcode the library name, load the symbols into the global + // namespace and leak the handle. dlopen("libmono-2.0.so", RTLD_LAZY | RTLD_GLOBAL); #endif