Split the modules and include files between weston and libweston

The backends are now installed in lib/libweston-0, and the include
files that will be used by libweston in include/libweston-0. The other
modules and weston-specific include files are kept in the old paths.
A new wet_load_module() is added to load plugins in the old path,
which is not part of libweston, but weston only and defined in main.c.
To allow that to be used by out of tree weston plugins, the function
is declared in a new weston.h, installed in include/weston.

The -0 in the paths is the abi version of libweston, and it will also
be used by the libweston .so. If the abi changes the number will need
to be increased.

Signed-off-by: Giulio Camuffo <giuliocamuffo@gmail.com>
Reviewed-by: Quentin Glidic <sardemff7+git@sardemff7.net>
Acked-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
diff --git a/Makefile.am b/Makefile.am
index fd10c13..92b4e5e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -5,6 +5,8 @@
 libexec_PROGRAMS =
 moduledir = $(libdir)/weston
 module_LTLIBRARIES =
+libweston_moduledir = $(libdir)/libweston-${LIBWESTON_ABI_VERSION}
+libweston_module_LTLIBRARIES =
 noinst_LTLIBRARIES =
 BUILT_SOURCES =
 
@@ -50,6 +52,7 @@
 	-I$(top_builddir)/protocol		\
 	-DDATADIR='"$(datadir)"'		\
 	-DMODULEDIR='"$(moduledir)"'		\
+	-DLIBWESTON_MODULEDIR='"$(libweston_moduledir)"' \
 	-DLIBEXECDIR='"$(libexecdir)"'		\
 	-DBINDIR='"$(bindir)"'
 
@@ -212,8 +215,8 @@
 wayland_sessiondir = $(datadir)/wayland-sessions
 dist_wayland_session_DATA = src/weston.desktop
 
-westonincludedir = $(includedir)/weston
-westoninclude_HEADERS =				\
+libwestonincludedir = $(includedir)/libweston-${LIBWESTON_ABI_VERSION}
+libwestoninclude_HEADERS =				\
 	src/version.h				\
 	src/compositor.h			\
 	src/compositor-drm.h			\
@@ -223,19 +226,21 @@
 	src/compositor-wayland.h		\
 	src/compositor-x11.h			\
 	src/timeline-object.h			\
-	src/weston.h				\
 	shared/matrix.h				\
 	shared/config-parser.h			\
 	shared/zalloc.h				\
 	shared/platform.h
 
+westonincludedir = $(includedir)/weston
+westoninclude_HEADERS = src/weston.h
+
 if ENABLE_IVI_SHELL
 westoninclude_HEADERS +=				\
 	ivi-shell/ivi-layout-export.h
 endif
 
 if ENABLE_EGL
-module_LTLIBRARIES += gl-renderer.la
+libweston_module_LTLIBRARIES += gl-renderer.la
 gl_renderer_la_LDFLAGS = -module -avoid-version
 gl_renderer_la_LIBADD = $(COMPOSITOR_LIBS) $(EGL_LIBS)
 gl_renderer_la_CFLAGS =				\
@@ -252,7 +257,7 @@
 endif
 
 if ENABLE_X11_COMPOSITOR
-module_LTLIBRARIES += x11-backend.la
+libweston_module_LTLIBRARIES += x11-backend.la
 x11_backend_la_LDFLAGS = -module -avoid-version
 x11_backend_la_LIBADD = $(COMPOSITOR_LIBS) $(X11_COMPOSITOR_LIBS) \
 	libshared-cairo.la
@@ -278,7 +283,7 @@
 	shared/helpers.h
 
 if ENABLE_DRM_COMPOSITOR
-module_LTLIBRARIES += drm-backend.la
+libweston_module_LTLIBRARIES += drm-backend.la
 drm_backend_la_LDFLAGS = -module -avoid-version
 drm_backend_la_LIBADD =				\
 	$(COMPOSITOR_LIBS)			\
@@ -309,7 +314,7 @@
 endif
 
 if ENABLE_WAYLAND_COMPOSITOR
-module_LTLIBRARIES += wayland-backend.la
+libweston_module_LTLIBRARIES += wayland-backend.la
 wayland_backend_la_LDFLAGS = -module -avoid-version
 wayland_backend_la_LIBADD =			\
 	$(COMPOSITOR_LIBS)			\
@@ -332,7 +337,7 @@
 endif
 
 if ENABLE_HEADLESS_COMPOSITOR
-module_LTLIBRARIES += headless-backend.la
+libweston_module_LTLIBRARIES += headless-backend.la
 headless_backend_la_LDFLAGS = -module -avoid-version
 headless_backend_la_LIBADD = $(COMPOSITOR_LIBS) libshared.la
 headless_backend_la_CFLAGS = $(COMPOSITOR_CFLAGS) $(AM_CFLAGS)
@@ -343,7 +348,7 @@
 endif
 
 if ENABLE_FBDEV_COMPOSITOR
-module_LTLIBRARIES += fbdev-backend.la
+libweston_module_LTLIBRARIES += fbdev-backend.la
 fbdev_backend_la_LDFLAGS = -module -avoid-version
 fbdev_backend_la_LIBADD =			\
 	$(COMPOSITOR_LIBS)			\
@@ -365,7 +370,7 @@
 endif
 
 if ENABLE_RDP_COMPOSITOR
-module_LTLIBRARIES += rdp-backend.la
+libweston_module_LTLIBRARIES += rdp-backend.la
 rdp_backend_la_LDFLAGS = -module -avoid-version
 rdp_backend_la_LIBADD = $(COMPOSITOR_LIBS) \
 	$(RDP_COMPOSITOR_LIBS) \
diff --git a/configure.ac b/configure.ac
index 525810f..90db672 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3,6 +3,7 @@
 m4_define([weston_micro_version], [90])
 m4_define([weston_version],
           [weston_major_version.weston_minor_version.weston_micro_version])
+m4_define([libweston_abi_version], [0])
 
 AC_PREREQ([2.64])
 AC_INIT([weston],
@@ -17,6 +18,7 @@
 AC_SUBST([WESTON_VERSION_MINOR], [weston_minor_version])
 AC_SUBST([WESTON_VERSION_MICRO], [weston_micro_version])
 AC_SUBST([WESTON_VERSION], [weston_version])
+AC_SUBST([LIBWESTON_ABI_VERSION], [libweston_abi_version])
 
 AC_CONFIG_AUX_DIR([build-aux])
 AC_CONFIG_HEADERS([config.h])
diff --git a/ivi-shell/ivi-layout.c b/ivi-shell/ivi-layout.c
index 9661378..81e5621 100644
--- a/ivi-shell/ivi-layout.c
+++ b/ivi-shell/ivi-layout.c
@@ -60,6 +60,7 @@
 #include <string.h>
 #include <assert.h>
 
+#include "weston.h"
 #include "compositor.h"
 #include "ivi-shell.h"
 #include "ivi-layout-export.h"
@@ -2071,7 +2072,7 @@
 		end = strchrnul(p, ',');
 		snprintf(buffer, sizeof buffer, "%.*s", (int)(end - p), p);
 
-		controller_module_init = weston_load_module(buffer, "controller_module_init");
+		controller_module_init = wet_load_module(buffer, "controller_module_init");
 		if (!controller_module_init)
 			return -1;
 
diff --git a/src/compositor.c b/src/compositor.c
index 2ec2f18..3904ef0 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -4791,7 +4791,7 @@
 		if (builddir)
 			snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
 		else
-			snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
+			snprintf(path, sizeof path, "%s/%s", LIBWESTON_MODULEDIR, name);
 	} else {
 		snprintf(path, sizeof path, "%s", name);
 	}
diff --git a/src/main.c b/src/main.c
index e34f8f5..733bf09 100644
--- a/src/main.c
+++ b/src/main.c
@@ -40,12 +40,14 @@
 #include <sys/socket.h>
 #include <libinput.h>
 #include <sys/time.h>
+#include <linux/limits.h>
 
 #ifdef HAVE_LIBUNWIND
 #define UNW_LOCAL_ONLY
 #include <libunwind.h>
 #endif
 
+#include "weston.h"
 #include "compositor.h"
 #include "../shared/os-compatibility.h"
 #include "../shared/helpers.h"
@@ -690,6 +692,49 @@
 	return 0;
 }
 
+WL_EXPORT void *
+wet_load_module(const char *name, const char *entrypoint)
+{
+	const char *builddir = getenv("WESTON_BUILD_DIR");
+	char path[PATH_MAX];
+	void *module, *init;
+
+	if (name == NULL)
+		return NULL;
+
+	if (name[0] != '/') {
+		if (builddir)
+			snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
+		else
+			snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
+	} else {
+		snprintf(path, sizeof path, "%s", name);
+	}
+
+	module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
+	if (module) {
+		weston_log("Module '%s' already loaded\n", path);
+		dlclose(module);
+		return NULL;
+	}
+
+	weston_log("Loading module '%s'\n", path);
+	module = dlopen(path, RTLD_NOW);
+	if (!module) {
+		weston_log("Failed to load module: %s\n", dlerror());
+		return NULL;
+	}
+
+	init = dlsym(module, entrypoint);
+	if (!init) {
+		weston_log("Failed to lookup init function: %s\n", dlerror());
+		dlclose(module);
+		return NULL;
+	}
+
+	return init;
+}
+
 static int
 load_modules(struct weston_compositor *ec, const char *modules,
 	     int *argc, char *argv[])
@@ -706,7 +751,7 @@
 	while (*p) {
 		end = strchrnul(p, ',');
 		snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
-		module_init = weston_load_module(buffer, "module_init");
+		module_init = wet_load_module(buffer, "module_init");
 		if (!module_init)
 			return -1;
 		if (module_init(ec, argc, argv) < 0)
diff --git a/src/weston.h b/src/weston.h
index ea42166..da7c7a9 100644
--- a/src/weston.h
+++ b/src/weston.h
@@ -30,7 +30,7 @@
 extern "C" {
 #endif
 
-#include "compositor.h"
+#include <compositor.h>
 
 void
 screenshooter_create(struct weston_compositor *ec);
@@ -60,6 +60,9 @@
 struct weston_config *
 wet_get_config(struct weston_compositor *compositor);
 
+void *
+wet_load_module(const char *name, const char *entrypoint);
+
 #ifdef  __cplusplus
 }
 #endif
diff --git a/src/weston.pc.in b/src/weston.pc.in
index c560eb3..f2ffc9e 100644
--- a/src/weston.pc.in
+++ b/src/weston.pc.in
@@ -9,4 +9,4 @@
 Description: Header files for Weston plugin development
 Version: @WESTON_VERSION@
 Requires.private: wayland-server pixman-1 xkbcommon
-Cflags: -I${includedir}
+Cflags: -I${includedir}/weston -I{includedir}/libweston-@LIBWESTON_ABI_VERSION@