Ilya Zakharevich on Thu, 02 May 2019 12:40:03 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

PATCH: support install() on Win: [Re: Please test pari-2.11.2 prerelease 1]


On Sun, Apr 28, 2019 at 04:08:23PM +0200, Bill Allombert wrote:
> We have made available a prerelease of PARI 2.11.2 (stable).

Strangely, install() is not supported on Windows (with static
builds).  It is so much easier on Windows than on OS/2?????????you could not
export symbols from an exe file???

I attach a patch to es.c.  Additionally, a def file should be linked
to the exe.  The patch below shows one way to do it.  (I do not know
how to modify the build environment to make this change to Makefile.)

Ilya

--- Makefile-pre	2019-05-02 00:53:18.694715700 -0700
+++ Makefile	2019-05-02 03:31:48.838664800 -0700
@@ -59,6 +59,7 @@ DL_DFLT_NAME = \"$(LIBPARI_SO)\"
 LD_INSTALL_NAME  = 
 
 EXPORT_FILE   = $(LIBPARI).def
+EXPORT_FILE_EXE   = $(LIBPARI)_exe.def
 EXPORT_CREATE = 
 EXPORT_LIB    = 
 EXPORT_LIB_CREATE = 
@@ -172,9 +173,9 @@ tune-dyn: tune$(_O) $(LIBPARI_DYN)
 tune-sta: tune$(_O) $(LIBPARI_STA)
 	$(LD) $(LDFLAGS) $(RUNPTH) -o $@ $< $(GMPOBJS) ./$(LIBPARI_STA) $(STA_LIBS)
 
-gp-sta.exe: ../src/funclist $(OBJS) $(OBJSGP)
+gp-sta.exe: ../src/funclist $(OBJS) $(OBJSGP) $(EXPORT_FILE_EXE)
 	$(RM) $@
-	$(LD) -o $@ $(LDFLAGS) $(OBJS) $(OBJSGP) $(RUNPTH) $(RLLIBS) $(PLOTLIBS) $(STA_LIBS)
+	$(LD) -o $@ $(LDFLAGS) $(OBJS) $(OBJSGP) $(RUNPTH) $(RLLIBS) $(PLOTLIBS) $(STA_LIBS) $(EXPORT_FILE_EXE)
 	: gp-sta.exe
 
 clean_pari_ps::
@@ -1020,8 +1021,11 @@ dotest-time::
 gp-sta: gp-sta.exe
 gp-dyn: gp-dyn.exe
 
+$(EXPORT_FILE_EXE): $(EXPORT_FILE)
+
 $(EXPORT_FILE): $(OBJS)
 	$(DLLTOOL) --export-all-symbols -k -z $@.tmp $(OBJS)
+	echo "NAME $(LIBPARI)_exe" > $(EXPORT_FILE_EXE) && cat $@.tmp >> $(EXPORT_FILE_EXE)
 	echo "LIBRARY $(LIBPARI)" > $@ && cat $@.tmp >> $@ && rm $@.tmp
 
 gp-dyn.exe: ../src/funclist $(OBJSGP) $(LIBPARI_DYN) $(EXPORT_LIB)
--- pari-2.11.1/src/language/es.c-pre	2018-07-25 11:00:21.000000000 -0700
+++ pari-2.11.1/src/language/es.c	2019-05-02 03:25:45.470881300 -0700
@@ -5200,7 +5200,6 @@ install0(const char *name, const char *l
 #  define RTLD_GLOBAL 0
 #endif
   handle = gp_dlopen(lib, RTLD_LAZY|RTLD_GLOBAL);
-
   if (!handle)
   {
     const char *s = dlerror(); if (s) err_printf("%s\n\n",s);
@@ -5236,12 +5235,24 @@ gp_LoadLibrary(const char *name)
   }
   return NULL;
 }
+static int I_m_a_DLL = -1;
+static HMODULE hmod_exe, hmod_dll;
 static void *
 install0(const char *name, const char *lib)
 {
   HMODULE handle;
 
-  handle = gp_LoadLibrary(lib);
+  if (lib == pari_library_path) {
+    if (I_m_a_DLL < 0) {
+      hmod_exe = hmod_dll = GetModuleHandleA(NULL);
+      if (!hmod_exe	/* Can this happen? */
+          || !(GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
+          		          (LPCSTR)install0, &hmod_dll)))
+        I_m_a_DLL = hmod_exe != hmod_dll;
+    }
+    handle = hmod_dll;
+  } else
+    handle = gp_LoadLibrary(lib);
   if (!handle)
   {
     if (lib) pari_err(e_MISC,"couldn't open dynamic library '%s'",lib);