git: 94e1374b55c7 - stable/14 - loader: Move to using linker sets to bring in optional bits

From: Warner Losh <imp_at_FreeBSD.org>
Date: Tue, 16 Apr 2024 20:12:49 UTC
The branch stable/14 has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=94e1374b55c7c3ac484bbe0631591545a5536c8d

commit 94e1374b55c7c3ac484bbe0631591545a5536c8d
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2024-02-16 03:53:47 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-04-16 19:54:24 +0000

    loader: Move to using linker sets to bring in optional bits
    
    The graphics stuff is optional. When it is pulled into the system, we
    use a linker set to initialize the lua bindings for it now.
    
    Sponsored by:           Netflix
    Reviewed by:            kevans, jhb
    Differential Revision:  https://reviews.freebsd.org/D43906
    
    (cherry picked from commit 0921a771da8a9117edf26352a8a047bacbfcee45)
---
 stand/common/interp_lua.c | 29 +++--------------------------
 stand/liblua/gfx_utils.c  | 27 +++++++++++++++++++++++++++
 stand/liblua/lutils.h     |  9 +++++++++
 3 files changed, 39 insertions(+), 26 deletions(-)

diff --git a/stand/common/interp_lua.c b/stand/common/interp_lua.c
index a347526b67c9..11595b78a7bd 100644
--- a/stand/common/interp_lua.c
+++ b/stand/common/interp_lua.c
@@ -97,31 +97,6 @@ static const luaL_Reg loadedlibs[] = {
   {NULL, NULL}
 };
 
-static void
-interp_init_md(lua_State *L)
-{
-	luaL_requiref(L, "gfx", luaopen_gfx, 1);
-	lua_pop(L, 1);	/* Remove lib */
-
-	/*
-	 * Add in the comparability references in the loader table.  Doing it with
-	 * a pseudo-embedded script is easier than the raw calls.
-	 */
-	if (luaL_dostring(L,
-		"loader.fb_bezier = gfx.fb_bezier\n"
-		"loader.fb_drawrect = gfx.fb_drawrect\n"
-		"loader.fb_line = gfx.fb_line\n"
-		"loader.fb_putimage = gfx.fb_putimage\n"
-		"loader.fb_setpixel = gfx.fb_setpixel\n"
-		"loader.term_drawrect = gfx.term_drawrect\n"
-		"loader.term_putimage = gfx.term_putimage") != 0) {
-		lua_pop(L, 1);
-		const char *errstr = lua_tostring(L, -1);
-		errstr = errstr == NULL ? "unknown" : errstr;
-		printf("Error adding compat loader bindings: %s.\n", errstr);
-	}
-}
-
 void
 interp_init(void)
 {
@@ -129,6 +104,7 @@ interp_init(void)
 	struct interp_lua_softc	*softc = &lua_softc;
 	const char *filename;
 	const luaL_Reg *lib;
+	lua_init_md_t **fnpp;
 
 	TSENTER();
 
@@ -148,7 +124,8 @@ interp_init(void)
 		lua_pop(luap, 1);  /* remove lib */
 	}
 
-	interp_init_md(luap);
+	LUA_FOREACH_SET(fnpp)
+	    (*fnpp)(luap);
 
 	filename = getenv("loader_lua");
 	if (filename == NULL)
diff --git a/stand/liblua/gfx_utils.c b/stand/liblua/gfx_utils.c
index d2d22738c929..8d2aaacbd688 100644
--- a/stand/liblua/gfx_utils.c
+++ b/stand/liblua/gfx_utils.c
@@ -245,3 +245,30 @@ void
 gfx_interp_md(void)
 {
 }
+
+static void
+gfx_init_md(lua_State *L)
+{
+	luaL_requiref(L, "gfx", luaopen_gfx, 1);
+	lua_pop(L, 1);	/* Remove lib */
+
+	/*
+	 * Add in the compatibility references in the loader table.  Doing it with
+	 * a pseudo-embedded script is easier than the raw calls.
+	 */
+	if (luaL_dostring(L,
+		"loader.fb_bezier = gfx.fb_bezier\n"
+		"loader.fb_drawrect = gfx.fb_drawrect\n"
+		"loader.fb_line = gfx.fb_line\n"
+		"loader.fb_putimage = gfx.fb_putimage\n"
+		"loader.fb_setpixel = gfx.fb_setpixel\n"
+		"loader.term_drawrect = gfx.term_drawrect\n"
+		"loader.term_putimage = gfx.term_putimage") != 0) {
+		lua_pop(L, 1);
+		const char *errstr = lua_tostring(L, -1);
+		errstr = errstr == NULL ? "unknown" : errstr;
+		printf("Error adding compat loader bindings: %s.\n", errstr);
+	}
+}
+
+LUA_COMPILE_SET(gfx_init_md);
diff --git a/stand/liblua/lutils.h b/stand/liblua/lutils.h
index c1fd6af496e5..522abfd3d0d4 100644
--- a/stand/liblua/lutils.h
+++ b/stand/liblua/lutils.h
@@ -30,3 +30,12 @@ int	luaopen_gfx(lua_State *);
 int	luaopen_loader(lua_State *);
 int	luaopen_io(lua_State *);
 int	luaopen_pager(lua_State *);
+
+#include <sys/linker_set.h>
+
+typedef void lua_init_md_t(lua_State *);
+#define LUA_COMPILE_SET(func)	\
+	DATA_SET(Xficl_compile_set, func)	/* XXX linker set know by ldscrips */
+#define LUA_FOREACH_SET(s) \
+	SET_FOREACH((s), Xficl_compile_set)
+SET_DECLARE(Xficl_compile_set, lua_init_md_t);