From 09ae0c2965271748450fdf3f45b217eaacc7ee4e Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Tue, 12 May 2020 11:43:19 +0200
Subject: [PATCH] Working!

---
 inc/lektor/module/module_sdl2.h |  2 ++
 inc/lektor/module/module_x11.h  |  2 ++
 meson.build                     | 11 +++++++++++
 src/config.c                    |  2 +-
 src/main/server.c               | 13 +++++++++++--
 src/module/module_sdl2.c        | 14 ++++++--------
 src/module/module_x11.c         | 14 ++++++--------
 src/reg.c                       | 12 ++++++------
 8 files changed, 45 insertions(+), 25 deletions(-)

diff --git a/inc/lektor/module/module_sdl2.h b/inc/lektor/module/module_sdl2.h
index 341bf644..cdd608eb 100644
--- a/inc/lektor/module/module_sdl2.h
+++ b/inc/lektor/module/module_sdl2.h
@@ -4,8 +4,10 @@
 #include <sqlite3.h>
 #include <lektor/window.h>
 
+#if defined(__STATIC_MODULES)
 /* The only function with a setted filename */
 int load_sdl2(void *mod, struct lkt_state *srv, void *handle);
+#endif
 
 /* Below there are needed functions for a window module.
  * Names are not fixed but they follow a convention (to get a code easy
diff --git a/inc/lektor/module/module_x11.h b/inc/lektor/module/module_x11.h
index b6dc05df..f1cee93f 100644
--- a/inc/lektor/module/module_x11.h
+++ b/inc/lektor/module/module_x11.h
@@ -4,8 +4,10 @@
 #include <sqlite3.h>
 #include <lektor/window.h>
 
+#if defined(__STATIC_MODULES)
 /* The only function with a setted filename */
 int load_x11(void *mod, struct lkt_state *srv, void *handle);
+#endif
 
 /* Below there are needed functions for a window module.
  * Names are not fixed but they follow a convention (to get a code easy
diff --git a/meson.build b/meson.build
index 7a1ea671..d3a1af20 100644
--- a/meson.build
+++ b/meson.build
@@ -27,6 +27,17 @@ if archi == 'unknown'
 endif
 add_global_arguments('-D_REENTRANT', '-D' + archi + '_ARCH', language: 'c')
 
+## Static modules ?
+if get_option('static_modules').enabled()
+  add_global_arguments('-D__STATIC_MODULES', language: 'c')
+  if get_option('module_sdl').enabled()
+    add_global_arguments('-D__STATIC_SDL2', language: 'c')
+  endif
+  if get_option('module_x11').enabled()
+    add_global_arguments('-D__STATIC_X11', language: 'c')
+  endif
+endif
+
 ## Module list
 lektor_modules = []
 
diff --git a/src/config.c b/src/config.c
index f22b7156..44008e7d 100644
--- a/src/config.c
+++ b/src/config.c
@@ -129,7 +129,7 @@ load_so(const char *const mod_path, const char *const mod_init, void *mod, struc
     if (module_set_function)
         return module_set_function(mod, srv, handle);
     else {
-        LOG_ERROR_SCT("LOAD", "Failed to find init symbol %s", mod_init);
+        LOG_ERROR_SCT("LOAD", "Failed to find init symbol %s in file %s", mod_init, mod_path);
         return 1;
     }
 }
diff --git a/src/main/server.c b/src/main/server.c
index 12cca8a7..86be43bd 100644
--- a/src/main/server.c
+++ b/src/main/server.c
@@ -18,11 +18,20 @@
 #include <pwd.h>
 #include <pthread.h>
 
+#ifdef __STATIC_SDL2
+#include <lektor/module/module_sdl2.h>
+#endif
+#ifdef __STATIC_X11
+#include <lektor/module/module_x11.h>
+#endif
+
 REG_BEGIN(server_reg)
 REG_ADD(load_repo_https)
 #ifdef __STATIC_SDL2
-#include <lektor/module/module_sdl2.h>
-REG_ADD(module_sdl2_new)
+REG_ADD(load_sdl2)
+#endif
+#ifdef __STATIC_X11
+REG_ADD(load_x11)
 #endif
 REG_END()
 
diff --git a/src/module/module_sdl2.c b/src/module/module_sdl2.c
index 5e0f0a0e..101ab1a8 100644
--- a/src/module/module_sdl2.c
+++ b/src/module/module_sdl2.c
@@ -255,10 +255,14 @@ loop:
 
 /* Exported functions */
 
-extern int
+int
+#if ! defined(__STATIC_MODULES)
 module_set_function(void *arg__, struct lkt_state *srv, void *handle__)
+#else
+load_sdl2(void *arg__, struct lkt_state *srv, void *handle__)
+#endif
 {
-    RETURN_UNLESS(arg__ && handle__, "Invalid argument", 1);
+    RETURN_UNLESS(arg__, "Invalid argument", 1);
     struct lkt_win *win = (struct lkt_win *) arg__;
     win->srv            = srv;
 
@@ -276,12 +280,6 @@ module_set_function(void *arg__, struct lkt_state *srv, void *handle__)
     return !module_sdl2_new(&srv->win);
 }
 
-int
-load_sdl2(void *arg, struct lkt_state *srv, void *handle)
-{
-    return module_set_function(arg, srv, handle);
-}
-
 bool
 module_sdl2_new(struct lkt_win *const win)
 {
diff --git a/src/module/module_x11.c b/src/module/module_x11.c
index d901ec8d..17c67093 100644
--- a/src/module/module_x11.c
+++ b/src/module/module_x11.c
@@ -42,10 +42,14 @@ struct module_x11_window {
  * NOW: THE ONLY EXPORTED FUNCTION :NOW
  */
 
-extern int
+int
+#if ! defined(__STATIC_MODULES)
 module_set_function(void *arg__, struct lkt_state *srv, void *handle__)
+#else
+load_x11(void *arg__, struct lkt_state *srv, void *handle__)
+#endif
 {
-    RETURN_UNLESS(arg__ && handle__, "Invalid arguments", 1);
+    RETURN_UNLESS(arg__, "Invalid arguments", 1);
     struct lkt_win *win = (struct lkt_win *) arg__;
     win->srv            = srv;
 
@@ -63,12 +67,6 @@ module_set_function(void *arg__, struct lkt_state *srv, void *handle__)
     return !module_x11_new(&srv->win);
 }
 
-int
-load_x11(void *arg, struct lkt_state *srv, void *handle)
-{
-    return module_set_function(arg, srv, handle);
-}
-
 /*
  *
  * NOW: PRIVATE X11 FUNCTIONS :NOW
diff --git a/src/reg.c b/src/reg.c
index c19c0391..69bc8afa 100644
--- a/src/reg.c
+++ b/src/reg.c
@@ -21,6 +21,7 @@ reg_pick(const char *file, void **handle, const char *symbol)
         goto use_reg;
 
     /* Use dlsym */
+    LOG_INFO_SCT("REG", "Using dlfcn to find %s", symbol);
     char *error;
     *handle = dlopen(file, RTLD_NOW);
 
@@ -41,13 +42,12 @@ reg_pick(const char *file, void **handle, const char *symbol)
 
     /* Read the reg */
 use_reg:
-    for (i = 0; reg[i].name && reg[i].func && !STR_MATCH(reg[i].name, symbol); ++i)
-        continue;
+    LOG_INFO_SCT("REG", "Using the reg structure to find %s", symbol);
+    for (i = 0; reg[i].name && reg[i].func; ++i)
+        if(STR_MATCH(reg[i].name, symbol))
+            return (void *) reg[i].func;
 
-    if (reg[i].func)
-        return (void *) reg[i].func;
-    else
-        return NULL;
+    return NULL;
 }
 
 void
-- 
GitLab