diff --git a/contrib/libucl/lua/lua_ucl.c b/contrib/libucl/lua/lua_ucl.c --- a/contrib/libucl/lua/lua_ucl.c +++ b/contrib/libucl/lua/lua_ucl.c @@ -30,6 +30,8 @@ #include "lua_ucl.h" #include +#include "bootstrap.h" + /*** * @module ucl * This lua module allows to parse objects from strings and to store data into @@ -1571,3 +1573,5 @@ return (struct ucl_lua_funcdata*)obj->value.ud; } + +FLUA_MODULE(ucl); diff --git a/contrib/lyaml/ext/yaml/yaml.c b/contrib/lyaml/ext/yaml/yaml.c --- a/contrib/lyaml/ext/yaml/yaml.c +++ b/contrib/lyaml/ext/yaml/yaml.c @@ -35,6 +35,8 @@ #include "lyaml.h" +#include "bootstrap.h" + #define MYNAME "yaml" #define MYVERSION MYNAME " library for " LUA_VERSION " / " VERSION @@ -64,3 +66,5 @@ return 1; } + +FLUA_MODULE(yaml); diff --git a/libexec/flua/Makefile b/libexec/flua/Makefile --- a/libexec/flua/Makefile +++ b/libexec/flua/Makefile @@ -1,10 +1,35 @@ .include -SUBDIR+= libfreebsd -SUBDIR+= libhash -SUBDIR+= libjail -SUBDIR+= libucl -SUBDIR+= liblyaml +# New flua modules should be added here rather than to SUBDIR so that we can do +# the right thing for both bootstrap flua and target flua. The former does not +# do any shared libs, so we just build them all straight into flua itself rather +# than mucking about with the infrastructure to make them linkable -- thus, why +# these are all structured to have a Makefile that describes what we want +# *installed*, and a Makefile.inc that describes what we need to *build*. +FLUA_MODULES+= libhash +FLUA_MODULES+= libjail +FLUA_MODULES+= libucl +FLUA_MODULES+= liblyaml + +.ifdef BOOTSTRAPPING +FLUA_MODULES+= libfreebsd/sys/linker +FLUA_MODULES+= libfreebsd/kenv +CFLAGS+= -I${.CURDIR} -DBOOTSTRAPPING + +SHAREDIR= ${WORLDTMP}/legacy/usr/share/flua +FLUA_PATH= ${SHAREDIR}/?.lua;${SHAREDIR}/?/init.lua +CFLAGS+= -DBOOTSTRAP_FLUA_PATH=\"${FLUA_PATH:Q}\" + +.for mod in ${FLUA_MODULES} +.include "${mod}/Makefile.inc" +.endfor + +.else + +FLUA_MODULES+= libfreebsd +SUBDIR+= ${FLUA_MODULES} + +.endif LUASRC?= ${SRCTOP}/contrib/lua/src .PATH: ${LUASRC} @@ -14,7 +39,7 @@ CWARNFLAGS.gcc+= -Wno-format-nonliteral -LIBADD= lua +LIBADD+= lua # Entry point SRCS+= lua.c diff --git a/libexec/flua/Makefile.inc b/libexec/flua/Makefile.inc --- a/libexec/flua/Makefile.inc +++ b/libexec/flua/Makefile.inc @@ -2,4 +2,9 @@ CFLAGS+= \ -I${SRCTOP}/contrib/lua/src \ - -I${SRCTOP}/lib/liblua + -I${SRCTOP}/lib/liblua \ + -I${SRCTOP}/libexec/flua + +.ifdef BOOTSTRAPPING +CFLAGS+= -DBOOTSTRAPPING +.endif diff --git a/libexec/flua/bootstrap.h b/libexec/flua/bootstrap.h new file mode 100644 --- /dev/null +++ b/libexec/flua/bootstrap.h @@ -0,0 +1,30 @@ +/*- + * Copyright (c) 2025 Kyle Evans + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#ifndef FLUA_BOOTSTRAP_H +#define FLUA_BOOTSTRAP_H + +#ifdef BOOTSTRAPPING +#include + +#include + +SET_DECLARE(flua_module_set, const luaL_Reg); +#define FLUA_MODULE_DEF(ident, modname, openfn) \ + static const luaL_Reg ident = { modname, openfn }; \ + DATA_SET(flua_module_set, ident) + +#define FLUA_MODULE_NAMED(mod, name) \ + FLUA_MODULE_DEF(module_ ## mod, name, luaopen_ ## mod) +#define FLUA_MODULE(mod) \ + FLUA_MODULE_DEF(module_ ## mod, #mod, luaopen_ ## mod) +#else /* !BOOTSTRAPPING */ +#define FLUA_MODULE_DEF(ident, modname, openfn) +#define FLUA_MODULE_NAMED(mod, name) +#define FLUA_MODULE(modname) +#endif /* BOOTSTRAPPING */ + +#endif /* FLUA_BOOTSTRAP_H */ diff --git a/libexec/flua/libfreebsd/kenv/Makefile b/libexec/flua/libfreebsd/kenv/Makefile --- a/libexec/flua/libfreebsd/kenv/Makefile +++ b/libexec/flua/libfreebsd/kenv/Makefile @@ -1,5 +1,5 @@ SHLIB_NAME= kenv.so -SRCS+= kenv.c MAN= freebsd.kenv.3lua +.include "Makefile.inc" .include diff --git a/libexec/flua/libfreebsd/kenv/Makefile.inc b/libexec/flua/libfreebsd/kenv/Makefile.inc new file mode 100644 --- /dev/null +++ b/libexec/flua/libfreebsd/kenv/Makefile.inc @@ -0,0 +1,2 @@ +.PATH: ${.PARSEDIR} +SRCS+= kenv.c diff --git a/libexec/flua/libfreebsd/kenv/kenv.c b/libexec/flua/libfreebsd/kenv/kenv.c --- a/libexec/flua/libfreebsd/kenv/kenv.c +++ b/libexec/flua/libfreebsd/kenv/kenv.c @@ -14,6 +14,8 @@ #include #include +#include "bootstrap.h" + int luaopen_freebsd_kenv(lua_State *L); static int @@ -94,3 +96,5 @@ return (1); } + +FLUA_MODULE_NAMED(freebsd_kenv, "freebsd.kenv"); diff --git a/libexec/flua/libfreebsd/sys/linker/Makefile b/libexec/flua/libfreebsd/sys/linker/Makefile --- a/libexec/flua/libfreebsd/sys/linker/Makefile +++ b/libexec/flua/libfreebsd/sys/linker/Makefile @@ -1,7 +1,6 @@ SHLIB_NAME= linker.so -SRCS+= linker.c - MAN= freebsd.sys.linker.3lua +.include "Makefile.inc" .include diff --git a/libexec/flua/libfreebsd/sys/linker/Makefile.inc b/libexec/flua/libfreebsd/sys/linker/Makefile.inc new file mode 100644 --- /dev/null +++ b/libexec/flua/libfreebsd/sys/linker/Makefile.inc @@ -0,0 +1,2 @@ +.PATH: ${.PARSEDIR} +SRCS+= linker.c diff --git a/libexec/flua/libfreebsd/sys/linker/linker.c b/libexec/flua/libfreebsd/sys/linker/linker.c --- a/libexec/flua/libfreebsd/sys/linker/linker.c +++ b/libexec/flua/libfreebsd/sys/linker/linker.c @@ -15,6 +15,8 @@ #include #include +#include "bootstrap.h" + int luaopen_freebsd_sys_linker(lua_State *L); static int @@ -80,3 +82,5 @@ return (1); } + +FLUA_MODULE_NAMED(freebsd_sys_linker, "freebsd.sys.linker"); diff --git a/libexec/flua/libhash/Makefile b/libexec/flua/libhash/Makefile --- a/libexec/flua/libhash/Makefile +++ b/libexec/flua/libhash/Makefile @@ -1,9 +1,6 @@ SHLIB_NAME= hash.so -SRCS+= lhash.c - -LIBADD+= md - MAN= hash.3lua +.include "Makefile.inc" .include diff --git a/libexec/flua/libhash/Makefile.inc b/libexec/flua/libhash/Makefile.inc new file mode 100644 --- /dev/null +++ b/libexec/flua/libhash/Makefile.inc @@ -0,0 +1,3 @@ +.PATH: ${.PARSEDIR} +SRCS+= lhash.c +LIBADD+= md diff --git a/libexec/flua/libhash/lhash.c b/libexec/flua/libhash/lhash.c --- a/libexec/flua/libhash/lhash.c +++ b/libexec/flua/libhash/lhash.c @@ -11,6 +11,8 @@ #include #include +#include "bootstrap.h" + #define SHA256_META "SHA256 meta table" #define SHA256_DIGEST_LEN 32 @@ -175,3 +177,5 @@ return 1; } + +FLUA_MODULE(hash); diff --git a/libexec/flua/libjail/Makefile b/libexec/flua/libjail/Makefile --- a/libexec/flua/libjail/Makefile +++ b/libexec/flua/libjail/Makefile @@ -1,9 +1,6 @@ SHLIB_NAME= jail.so -SRCS+= lua_jail.c - -LIBADD+= jail - MAN= jail.3lua +.include "Makefile.inc" .include diff --git a/libexec/flua/libjail/Makefile.inc b/libexec/flua/libjail/Makefile.inc new file mode 100644 --- /dev/null +++ b/libexec/flua/libjail/Makefile.inc @@ -0,0 +1,3 @@ +.PATH: ${.PARSEDIR} +SRCS+= lua_jail.c +LIBADD+= jail diff --git a/libexec/flua/libjail/lua_jail.c b/libexec/flua/libjail/lua_jail.c --- a/libexec/flua/libjail/lua_jail.c +++ b/libexec/flua/libjail/lua_jail.c @@ -38,6 +38,8 @@ #include #include +#include "bootstrap.h" + #define JAIL_METATABLE "jail iterator metatable" /* @@ -716,3 +718,5 @@ return (1); } + +FLUA_MODULE(jail); diff --git a/libexec/flua/liblyaml/Makefile b/libexec/flua/liblyaml/Makefile --- a/libexec/flua/liblyaml/Makefile +++ b/libexec/flua/liblyaml/Makefile @@ -1,22 +1,4 @@ SHLIB_NAME= yaml.so -WARNS= 1 -LYAMLSRC?= ${SRCTOP}/contrib/lyaml -.PATH: ${LYAMLSRC}/ext/yaml ${LYAMLSRC}/lib/lyaml -SRCS= emitter.c \ - parser.c \ - scanner.c \ - yaml.c -CFLAGS+= \ - -I${LYAMLSRC}/ext/yaml \ - -I${SRCTOP}/contrib/libyaml/include \ - -DVERSION=\"6.2.8\" -LIBADD+= yaml - -FILES= explicit.lua \ - functional.lua \ - implicit.lua \ - init.lua -FILESDIR= ${SHAREDIR}/flua/lyaml - +.include "Makefile.inc" .include diff --git a/libexec/flua/liblyaml/Makefile b/libexec/flua/liblyaml/Makefile.inc copy from libexec/flua/liblyaml/Makefile copy to libexec/flua/liblyaml/Makefile.inc --- a/libexec/flua/liblyaml/Makefile +++ b/libexec/flua/liblyaml/Makefile.inc @@ -1,9 +1,8 @@ -SHLIB_NAME= yaml.so - WARNS= 1 + LYAMLSRC?= ${SRCTOP}/contrib/lyaml .PATH: ${LYAMLSRC}/ext/yaml ${LYAMLSRC}/lib/lyaml -SRCS= emitter.c \ +SRCS+= emitter.c \ parser.c \ scanner.c \ yaml.c @@ -13,10 +12,9 @@ -DVERSION=\"6.2.8\" LIBADD+= yaml -FILES= explicit.lua \ +FILESGROUPS+= YAML +YAML= explicit.lua \ functional.lua \ implicit.lua \ init.lua -FILESDIR= ${SHAREDIR}/flua/lyaml - -.include +YAMLDIR= ${SHAREDIR}/flua/lyaml diff --git a/libexec/flua/libucl/Makefile b/libexec/flua/libucl/Makefile --- a/libexec/flua/libucl/Makefile +++ b/libexec/flua/libucl/Makefile @@ -1,14 +1,4 @@ SHLIB_NAME= ucl.so -WARNS= 2 - -UCLSRC?= ${SRCTOP}/contrib/libucl -.PATH: ${UCLSRC}/lua -SRCS+= lua_ucl.c -CFLAGS+= \ - -I${UCLSRC}/include \ - -I${UCLSRC}/src \ - -I${UCLSRC}/uthash -LIBADD+= ucl - +.include "Makefile.inc" .include diff --git a/libexec/flua/libucl/Makefile b/libexec/flua/libucl/Makefile.inc copy from libexec/flua/libucl/Makefile copy to libexec/flua/libucl/Makefile.inc --- a/libexec/flua/libucl/Makefile +++ b/libexec/flua/libucl/Makefile.inc @@ -1,8 +1,8 @@ -SHLIB_NAME= ucl.so - +.if ${WARNS:U6} > 2 WARNS= 2 +.endif -UCLSRC?= ${SRCTOP}/contrib/libucl +UCLSRC?= ${SRCTOP}/contrib/libucl .PATH: ${UCLSRC}/lua SRCS+= lua_ucl.c CFLAGS+= \ @@ -10,5 +10,3 @@ -I${UCLSRC}/src \ -I${UCLSRC}/uthash LIBADD+= ucl - -.include diff --git a/libexec/flua/linit_flua.c b/libexec/flua/linit_flua.c --- a/libexec/flua/linit_flua.c +++ b/libexec/flua/linit_flua.c @@ -26,8 +26,8 @@ #include "lprefix.h" - #include +#include #include "lua.h" @@ -37,6 +37,8 @@ #include "lposix.h" #include "lfbsd.h" +#include "bootstrap.h" + /* ** these libs are loaded by lua.c and are readily available to any Lua ** program @@ -62,6 +64,28 @@ {NULL, NULL} }; +#ifdef BOOTSTRAPPING +static void __attribute__((constructor)) flua_init_env(void) { + /* + * This happens in the middle of luaopen_package(). We could move it into + * flua_setup_mods(), but it seems better to avoid its timing being so + * important that it would break some of our bootstrap modules if someone + * were to reorder things. + */ + if (getenv("LUA_PATH") == NULL) + setenv("LUA_PATH", BOOTSTRAP_FLUA_PATH, 1); +} + +static void flua_setup_mods (lua_State *L) { + const luaL_Reg **flib; + + SET_FOREACH(flib, flua_module_set) { + luaL_requiref(L, (*flib)->name, (*flib)->func, 1); + lua_pop(L, 1); /* remove lib */ + } +}; +#endif + LUALIB_API void luaL_openlibs (lua_State *L) { const luaL_Reg *lib; /* "require" functions from 'loadedlibs' and set results to global table */ @@ -69,4 +93,7 @@ luaL_requiref(L, lib->name, lib->func, 1); lua_pop(L, 1); /* remove lib */ } +#ifdef BOOTSTRAPPING + flua_setup_mods(L); +#endif } diff --git a/tools/build/Makefile b/tools/build/Makefile --- a/tools/build/Makefile +++ b/tools/build/Makefile @@ -497,6 +497,7 @@ usr/include/casper \ usr/include/openssl \ usr/include/private/ucl \ + usr/include/private/yaml \ usr/include/private/zstd \ usr/lib \ usr/libdata/pkgconfig \