From b744b2fa3216740c74d74bd62b26b6a539f026d1 Mon Sep 17 00:00:00 2001
From: Bram Moolenaar <Bram@vim.org>
Date: Fri, 13 Aug 2010 16:22:57 +0200
Subject: [PATCH] Add a configure check for RTLD_GLOBAL. (James Vega, Roland
 Puntaier)

---
 runtime/doc/if_pyth.txt |  6 ++-
 runtime/doc/todo.txt    |  9 ++---
 src/Makefile            |  1 +
 src/auto/configure      | 87 ++++++++++++++++++++++++++++++++++++++---
 src/config.h.in         |  3 ++
 src/configure.in        | 62 ++++++++++++++++++++++++++---
 src/if_python.c         | 15 +++----
 src/if_python3.c        | 15 +++----
 8 files changed, 162 insertions(+), 36 deletions(-)

diff --git a/runtime/doc/if_pyth.txt b/runtime/doc/if_pyth.txt
index d7d91b190e..edac295404 100644
--- a/runtime/doc/if_pyth.txt
+++ b/runtime/doc/if_pyth.txt
@@ -1,4 +1,4 @@
-*if_pyth.txt*   For Vim version 7.3f.  Last change: 2010 Aug 10
+*if_pyth.txt*   For Vim version 7.3f.  Last change: 2010 Aug 13
 
 
 		  VIM REFERENCE MANUAL    by Paul Moore
@@ -348,8 +348,10 @@ Vim can be built in four ways (:version output):
 3. Python 3 support only    (-python, +python3 or +python3/dyn)
 4. Python 2 and 3 support   (+python/dyn, +python3/dyn)
 
+							*E836* *E837*
 You can see that when Python 2 and Python 3 are both supported they must be
-loaded dynamically.
+loaded dynamically.  Depending on how the libraries have been build it might
+be impossible to use ":py3" and ":python" in one Vim session.
 
 On Linux/Unix systems this can only be done without importing global symbols.
 In this case python's "import" might fail, if the library expects the symbols 
diff --git a/runtime/doc/todo.txt b/runtime/doc/todo.txt
index 6d2036e454..dd85914847 100644
--- a/runtime/doc/todo.txt
+++ b/runtime/doc/todo.txt
@@ -30,15 +30,14 @@ be worked on, but only if you sponsor Vim development.  See |sponsor|.
 							*known-bugs*
 -------------------- Known bugs and current work -----------------------
 
-Have a close look at :find completion, anything that could be wrong?
-
 Patch for :python + :py3. (James Vega, Roland Puntaier, 2010 Aug 12)
 
 Patch for crash with cmdline editing functions. (Dominique Pelle, 2010 Aug 12)
 
-Test 73 fails on MS-Windows when compiled with DJGPP.
-:find completion with 'path' set to "./**" results in full path for
-"./subdir/file", should shorten to start with "./".
+Have a close look at :find completion, anything that could be wrong?
+
+Test 73 fails on MS-Windows when compiled with DJGPP and run twice.  How to
+delete the Xfind directory?
 
 Before release 7.3:
 - Rename vim73 branch to default (hints: Xavier de Gaye, 2010 May 23)
diff --git a/src/Makefile b/src/Makefile
index 4a66bfad5a..9f8aa4ba24 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -2255,6 +2255,7 @@ clean celan: testclean
 	-rm -f *.o objects/* core $(VIMTARGET).core $(VIMTARGET) vim xxd/*.o
 	-rm -f $(TOOLS) auto/osdef.h auto/pathdef.c auto/if_perl.c
 	-rm -f conftest* *~ auto/link.sed
+	-rm -f runtime pixmaps
 	-rm -rf $(APPDIR)
 	-rm -rf mzscheme_base.c
 	if test -d $(PODIR); then \
diff --git a/src/auto/configure b/src/auto/configure
index 22ec98a822..d09ee92ddf 100755
--- a/src/auto/configure
+++ b/src/auto/configure
@@ -1424,8 +1424,8 @@ Optional Features:
   --enable-luainterp=OPTS     Include Lua interpreter.  default=no OPTS=no/yes/dynamic
   --enable-mzschemeinterp   Include MzScheme interpreter.
   --enable-perlinterp=OPTS     Include Perl interpreter.  default=no OPTS=no/yes/dynamic
-  --enable-pythoninterp   Include Python interpreter.
-  --enable-python3interp   Include Python3 interpreter.
+  --enable-pythoninterp=OPTS   Include Python interpreter. default=no OPTS=no/yes/dynamic
+  --enable-python3interp=OPTS   Include Python3 interpreter. default=no OPTS=no/yes/dynamic
   --enable-tclinterp      Include Tcl interpreter.
   --enable-rubyinterp     Include Ruby interpreter.
   --enable-cscope         Include cscope interface.
@@ -5118,7 +5118,7 @@ fi
 
 { $as_echo "$as_me:${as_lineno-$LINENO}: result: $enable_pythoninterp" >&5
 $as_echo "$enable_pythoninterp" >&6; }
-if test "$enable_pythoninterp" = "yes"; then
+if test "$enable_pythoninterp" = "yes" -o "$enable_pythoninterp" = "dynamic"; then
     # Extract the first word of "python", so it can be a program name with args.
 set dummy python; ac_word=$2
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
@@ -5409,7 +5409,7 @@ fi
 
 { $as_echo "$as_me:${as_lineno-$LINENO}: result: $enable_python3interp" >&5
 $as_echo "$enable_python3interp" >&6; }
-if test "$enable_python3interp" = "yes"; then
+if test "$enable_python3interp" = "yes" -o "$enable_python3interp" = "dynamic"; then
     # Extract the first word of "python3", so it can be a program name with args.
 set dummy python3; ac_word=$2
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
@@ -5614,8 +5614,8 @@ rm -f core conftest.err conftest.$ac_objext \
 $as_echo "no" >&6; }
       fi
 
-                  { $as_echo "$as_me:${as_lineno-$LINENO}: checking if compile and link flags for Python are sane" >&5
-$as_echo_n "checking if compile and link flags for Python are sane... " >&6; }
+                  { $as_echo "$as_me:${as_lineno-$LINENO}: checking if compile and link flags for Python 3 are sane" >&5
+$as_echo_n "checking if compile and link flags for Python 3 are sane... " >&6; }
       cflags_save=$CFLAGS
       libs_save=$LIBS
       CFLAGS="$CFLAGS $PYTHON3_CFLAGS"
@@ -5667,10 +5667,85 @@ if test "$python_ok" = yes && test "$python3_ok" = yes; then
 
   $as_echo "#define DYNAMIC_PYTHON3 1" >>confdefs.h
 
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether we can do without RTLD_GLOBAL" >&5
+$as_echo_n "checking whether we can do without RTLD_GLOBAL... " >&6; }
+  cflags_save=$CFLAGS
+  CFLAGS="$CFLAGS $PYTHON3_CFLAGS"
+  ldflags_save=$LDFLAGS
+  LDFLAGS="$LDFLAGS -ldl"
+  if test "$cross_compiling" = yes; then :
+  { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
+$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
+as_fn_error "cannot run test program while cross compiling
+See \`config.log' for more details." "$LINENO" 5; }
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+    #include <dlfcn.h>
+    /* If this program fails, then RTLD_GLOBAL is needed.
+     * RTLD_GLOBAL will be used and then it is not possible to
+     * have both python versions enabled in the same vim instance.
+     * Only the first pyhton version used will be switched on.
+     */
+
+    int no_rtl_global_needed_for(char *python_instsoname)
+    {
+      int needed = 0;
+      void* pylib = dlopen(python_instsoname, RTLD_LAZY);
+      if (pylib != 0)
+      {
+          void (*init)(void) = dlsym(pylib, "Py_Initialize");
+          int (*simple)(char*) = dlsym(pylib, "PyRun_SimpleString");
+          void (*final)(void) = dlsym(pylib, "Py_Finalize");
+          (*init)();
+          needed = (*simple)("import termios") == -1;
+          (*final)();
+          dlclose(pylib);
+      }
+      return !needed;
+    }
+
+    int main(int argc, char** argv)
+    {
+      int not_needed = 0;
+      if (no_rtl_global_needed_for("libpython2.7.so.1.0") && no_rtl_global_needed_for("libpython3.1.so.1.0"))
+            not_needed = 1;
+      return !not_needed;
+    }
+_ACEOF
+if ac_fn_c_try_run "$LINENO"; then :
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+$as_echo "yes" >&6; };$as_echo "#define PY_NO_RTLD_GLOBAL 1" >>confdefs.h
+
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+fi
+rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \
+  conftest.$ac_objext conftest.beam conftest.$ac_ext
+fi
+
+  CFLAGS=$cflags_save
+  LDFLAGS=$ldflags_save
   PYTHON_SRC="if_python.c"
   PYTHON_OBJ="objects/if_python.o"
   PYTHON_CFLAGS="$PYTHON_CFLAGS -DDYNAMIC_PYTHON_DLL=\\\"${python_INSTSONAME}\\\""
   PYTHON_LIBS=
+  PYTHON3_SRC="if_python3.c"
+  PYTHON3_OBJ="objects/if_python3.o"
+  PYTHON3_CFLAGS="$PYTHON3_CFLAGS -DDYNAMIC_PYTHON3_DLL=\\\"${python3_INSTSONAME}\\\""
+  PYTHON3_LIBS=
+elif test "$python_ok" = yes && test "$enable_pythoninterp" = "dynamic"; then
+  $as_echo "#define DYNAMIC_PYTHON 1" >>confdefs.h
+
+  PYTHON_SRC="if_python.c"
+  PYTHON_OBJ="objects/if_python.o"
+  PYTHON_CFLAGS="$PYTHON_CFLAGS -DDYNAMIC_PYTHON_DLL=\\\"${python_INSTSONAME}\\\""
+  PYTHON_LIBS=
+elif test "$python3_ok" = yes && test "$enable_python3interp" = "dynamic"; then
+  $as_echo "#define DYNAMIC_PYTHON3 1" >>confdefs.h
+
   PYTHON3_SRC="if_python3.c"
   PYTHON3_OBJ="objects/if_python3.o"
   PYTHON3_CFLAGS="$PYTHON3_CFLAGS -DDYNAMIC_PYTHON3_DLL=\\\"${python3_INSTSONAME}\\\""
diff --git a/src/config.h.in b/src/config.h.in
index 2a28e0eb4e..f6c7fa4aa2 100644
--- a/src/config.h.in
+++ b/src/config.h.in
@@ -343,6 +343,9 @@
 /* Define for linking via dlopen() or LoadLibrary() */
 #undef DYNAMIC_PYTHON3
 
+/* Define if dynamic python does not require RTLD_GLOBAL */
+#undef PY_NO_RTLD_GLOBAL
+
 /* Define if you want to include the Ruby interpreter. */
 #undef FEAT_RUBY
 
diff --git a/src/configure.in b/src/configure.in
index 38fdea5909..152313b7ab 100644
--- a/src/configure.in
+++ b/src/configure.in
@@ -757,10 +757,10 @@ AC_SUBST(PERL_LIBS)
 
 AC_MSG_CHECKING(--enable-pythoninterp argument)
 AC_ARG_ENABLE(pythoninterp,
-	[  --enable-pythoninterp   Include Python interpreter.], ,
+	[  --enable-pythoninterp[=OPTS]   Include Python interpreter. [default=no] [OPTS=no/yes/dynamic]], ,
 	[enable_pythoninterp="no"])
 AC_MSG_RESULT($enable_pythoninterp)
-if test "$enable_pythoninterp" = "yes"; then
+if test "$enable_pythoninterp" = "yes" -o "$enable_pythoninterp" = "dynamic"; then
   dnl -- find the python executable
   AC_PATH_PROG(vi_cv_path_python, python)
   if test "X$vi_cv_path_python" != "X"; then
@@ -944,10 +944,10 @@ AC_SUBST(PYTHON_OBJ)
 
 AC_MSG_CHECKING(--enable-python3interp argument)
 AC_ARG_ENABLE(python3interp,
-	[  --enable-python3interp   Include Python3 interpreter.], ,
+	[  --enable-python3interp[=OPTS]   Include Python3 interpreter. [default=no] [OPTS=no/yes/dynamic]], ,
 	[enable_python3interp="no"])
 AC_MSG_RESULT($enable_python3interp)
-if test "$enable_python3interp" = "yes"; then
+if test "$enable_python3interp" = "yes" -o "$enable_python3interp" = "dynamic"; then
   dnl -- find the python3 executable
   AC_PATH_PROG(vi_cv_path_python3, python3)
   if test "X$vi_cv_path_python3" != "X"; then
@@ -1075,7 +1075,7 @@ eof
 
       dnl check that compiling a simple program still works with the flags
       dnl added for Python.
-      AC_MSG_CHECKING([if compile and link flags for Python are sane])
+      AC_MSG_CHECKING([if compile and link flags for Python 3 are sane])
       cflags_save=$CFLAGS
       libs_save=$LIBS
       CFLAGS="$CFLAGS $PYTHON3_CFLAGS"
@@ -1109,10 +1109,62 @@ dnl with dlopen(), dlsym(), dlclose()
 if test "$python_ok" = yes && test "$python3_ok" = yes; then
   AC_DEFINE(DYNAMIC_PYTHON)
   AC_DEFINE(DYNAMIC_PYTHON3)
+  AC_MSG_CHECKING(whether we can do without RTLD_GLOBAL)
+  cflags_save=$CFLAGS
+  CFLAGS="$CFLAGS $PYTHON3_CFLAGS"
+  ldflags_save=$LDFLAGS
+  LDFLAGS="$LDFLAGS -ldl"
+  AC_RUN_IFELSE([
+    #include <dlfcn.h>
+    /* If this program fails, then RTLD_GLOBAL is needed.
+     * RTLD_GLOBAL will be used and then it is not possible to
+     * have both python versions enabled in the same vim instance.
+     * Only the first pyhton version used will be switched on.
+     */
+
+    int no_rtl_global_needed_for(char *python_instsoname)
+    {
+      int needed = 0;
+      void* pylib = dlopen(python_instsoname, RTLD_LAZY);
+      if (pylib != 0)
+      {
+          void (*init)(void) = dlsym(pylib, "Py_Initialize");
+          int (*simple)(char*) = dlsym(pylib, "PyRun_SimpleString");
+          void (*final)(void) = dlsym(pylib, "Py_Finalize");
+          (*init)();
+          needed = (*simple)("import termios") == -1;
+          (*final)();
+          dlclose(pylib);
+      }
+      return !needed;
+    }
+
+    int main(int argc, char** argv)
+    {
+      int not_needed = 0;
+      if (no_rtl_global_needed_for("libpython2.7.so.1.0") && no_rtl_global_needed_for("libpython3.1.so.1.0"))
+            not_needed = 1;
+      return !not_needed;
+    }],
+    [AC_MSG_RESULT(yes);AC_DEFINE(PY_NO_RTLD_GLOBAL)], [AC_MSG_RESULT(no)])
+  CFLAGS=$cflags_save
+  LDFLAGS=$ldflags_save
+  PYTHON_SRC="if_python.c"
+  PYTHON_OBJ="objects/if_python.o"
+  PYTHON_CFLAGS="$PYTHON_CFLAGS -DDYNAMIC_PYTHON_DLL=\\\"${python_INSTSONAME}\\\""
+  PYTHON_LIBS=
+  PYTHON3_SRC="if_python3.c"
+  PYTHON3_OBJ="objects/if_python3.o"
+  PYTHON3_CFLAGS="$PYTHON3_CFLAGS -DDYNAMIC_PYTHON3_DLL=\\\"${python3_INSTSONAME}\\\""
+  PYTHON3_LIBS=
+elif test "$python_ok" = yes && test "$enable_pythoninterp" = "dynamic"; then
+  AC_DEFINE(DYNAMIC_PYTHON)
   PYTHON_SRC="if_python.c"
   PYTHON_OBJ="objects/if_python.o"
   PYTHON_CFLAGS="$PYTHON_CFLAGS -DDYNAMIC_PYTHON_DLL=\\\"${python_INSTSONAME}\\\""
   PYTHON_LIBS=
+elif test "$python3_ok" = yes && test "$enable_python3interp" = "dynamic"; then
+  AC_DEFINE(DYNAMIC_PYTHON3)
   PYTHON3_SRC="if_python3.c"
   PYTHON3_OBJ="objects/if_python3.o"
   PYTHON3_CFLAGS="$PYTHON3_CFLAGS -DDYNAMIC_PYTHON3_DLL=\\\"${python3_INSTSONAME}\\\""
diff --git a/src/if_python.c b/src/if_python.c
index 8a28f8bd4e..7b1d6e7b08 100644
--- a/src/if_python.c
+++ b/src/if_python.c
@@ -102,9 +102,7 @@ struct PyMethodDef { Py_ssize_t a; };
 #  include <dlfcn.h>
 #  define FARPROC void*
 #  define HINSTANCE void*
-#  ifdef FEAT_PYTHON3
-   /* Don't use RTLD_GLOBAL, it may cause a crash if both :python and :py3 are
-    * used. But without it importing may fail, e.g., for termios. */
+#  ifdef PY_NO_RTLD_GLOBAL
 #   define load_dll(n) dlopen((n), RTLD_LAZY)
 #  else
 #   define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
@@ -351,16 +349,15 @@ python_runtime_link_init(char *libname, int verbose)
 {
     int i;
 
-#if 0  /* this should be OK now that we don't use RTLD_GLOBAL */
-#if defined(UNIX) && defined(FEAT_PYTHON3)
-    /* Can't have Python and Python3 loaded at the same time, it may cause a
-     * crash. */
+#if !defined(PY_NO_RTLD_GLOBAL) && defined(UNIX) && defined(FEAT_PYTHON3)
+    /* Can't have Python and Python3 loaded at the same time.
+     * It cause a crash, because RTLD_GLOBAL is needed for
+     * standard C extension libraries of one or both python versions. */
     if (python3_loaded())
     {
-	EMSG(_("E999: Python: Cannot use :py and :py3 in one session"));
+	EMSG(_("E836: This Vim cannot execute :python after using :py3"));
 	return FAIL;
     }
-#endif
 #endif
 
     if (hinstPython)
diff --git a/src/if_python3.c b/src/if_python3.c
index 3b8948828f..4aa7edfd53 100644
--- a/src/if_python3.c
+++ b/src/if_python3.c
@@ -80,9 +80,7 @@ static void init_structs(void);
 #  include <dlfcn.h>
 #  define FARPROC void*
 #  define HINSTANCE void*
-#  ifdef FEAT_PYTHON
-   /* Don't use RTLD_GLOBAL, it may cause a crash if both :python and :py3 are
-    * used. But without it importing may fail, e.g., for termios. */
+#  ifdef PY_NO_RTLD_GLOBAL
 #   define load_dll(n) dlopen((n), RTLD_LAZY)
 #  else
 #   define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
@@ -338,16 +336,15 @@ py3_runtime_link_init(char *libname, int verbose)
     int i;
     void *ucs_from_string, *ucs_from_string_and_size;
 
-# if 0  /* this should be OK now that we don't use RTLD_GLOBAL */
-# if defined(UNIX) && defined(FEAT_PYTHON)
-    /* Can't have Python and Python3 loaded at the same time, it may cause a
-     * crash. */
+# if !defined(PY_NO_RTLD_GLOBAL) && defined(UNIX) && defined(FEAT_PYTHON)
+    /* Can't have Python and Python3 loaded at the same time.
+     * It cause a crash, because RTLD_GLOBAL is needed for
+     * standard C extension libraries of one or both python versions. */
     if (python_loaded())
     {
-	EMSG(_("E999: Python: Cannot use :py and :py3 in one session"));
+	EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
 	return FAIL;
     }
-# endif
 # endif
 
     if (hinstPy3 != 0)
-- 
GitLab