From ebdb00960f361d35cb9ae8628ef47bdbe5f4017f Mon Sep 17 00:00:00 2001 From: Lourival Vieira Neto Date: Wed, 25 Nov 2026 23:27:23 -0300 Subject: [PATCH 1/3] luaebpf: build and install with its own Makefile make builds the loader and the bytecode tables against the Lunatik checkout LUNATIK names, .. by default, where Lunatik mounts this tree at luaebpf/, and make install puts the compiler under the host's LUA_PATH as luaebpf/ and the loader under LUA_CPATH as luaebpf/loader.so, where lunatik run --dev, stop and unload find it. The rules are Lunatik's, Makefile lines 18, 40-48, 103, 109-118, 124-125, 180-188 and 209-210 at luainkernel/lunatik@2d4fad8, with the paths rebased on LUNATIK and the kernel table built with the flags that Makefile builds lunatic with. The variables keep Lunatik's names, LUA_PATH, LUA_CPATH and INSTALL among them, so the overrides Lunatik's build passes reach the $(MAKE) -C luaebpf LUNATIK=$(CURDIR) that replaces those rules there (luainkernel/lunatik#2719). The bytecode directory is written out rather than named BYTECODE, which would take the BYTECODE=1 a Lunatik install is run with: that make hands its command line on to this one. Built against a clone of Lunatik's master, the tables and the loader are byte for byte the ones Lunatik's rules build, and an install lays out the files Lunatik's does. Co-Authored-By: Claude Opus 5.5 --- Makefile | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 13 ++++++++++-- 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9ed8473 --- /dev/null +++ b/Makefile @@ -0,0 +1,63 @@ +# +# SPDX-FileCopyrightText: (c) 2026 Ring Zero Desenvolvimento de Software LTDA +# SPDX-License-Identifier: MIT OR GPL-2.0-only +# + +# the Lunatik checkout whose lua/ and lunatik_conf.h the tables and the loader are built from, +# by default the one that mounts this tree at luaebpf/ +LUNATIK ?= .. + +LUA ?= lua5.5 +LUA_PATH = $(shell $(LUA) -E -e 'print(package.path:match("([^;]*)/%?%.lua;"))') +LUA_CPATH = $(shell $(LUA) -E -e 'print(package.cpath:match("([^;]*)/%?%.so;"))') +LUAEBPF_INSTALL_PATH = ${LUA_PATH}/luaebpf + +HOSTCC ?= cc +RM = rm -f +MKDIR = mkdir -p -m 0755 +INSTALL = install -o root -g root + +# the flags Lunatik's Makefile builds lunatic with +LUNATIC_CFLAGS := -std=gnu99 -O2 -Wall -D_KERNEL -DLUA_USE_LINUX -I${LUNATIK} -I${LUNATIK}/lua + +# what reading and rewriting a chunk takes from lua/, printed by bytecode.c built as lunatic +# is and as the CLI's Lua is, so a chunk lunatic writes loads in the CLI's Lua +TABLES := bytecode/kernel.lua bytecode/host.lua +TABLES_SRCS := bytecode.c ${LUNATIK}/lua/lopcodes.c + +# luaebpf.loader: a C module of the CLI's Lua, built against lua/ as that Lua is, over libbpf +LOADER := loader.so +LOADER_CFLAGS := -std=gnu99 -O2 -Wall -fPIC -shared -DLUA_USE_LINUX -I${LUNATIK}/lua + +.PHONY: all clean install uninstall + +all: ${TABLES} ${LOADER} + +bytecode/kernel.lua: TABLES_CFLAGS := ${LUNATIC_CFLAGS} +bytecode/host.lua: TABLES_CFLAGS := -std=gnu99 -O2 -Wall -DLUA_USE_LINUX -I${LUNATIK}/lua +${TABLES}: ${TABLES_SRCS} $(wildcard ${LUNATIK}/lua/*.h) ${LUNATIK}/lunatik_conf.h + ${MKDIR} bytecode + ${HOSTCC} ${TABLES_CFLAGS} -o $@.bin ${TABLES_SRCS} + ./$@.bin > $@ + ${RM} $@.bin + +${LOADER}: loader.c $(wildcard ${LUNATIK}/lua/*.h) + ${HOSTCC} ${LOADER_CFLAGS} -o $@ $< -lbpf + +clean: + ${RM} ${LOADER} + ${RM} -r bytecode + +install: + ${INSTALL} -D -m 0755 ${LOADER} ${LUA_CPATH}/luaebpf/loader.so + ${RM} -r ${LUAEBPF_INSTALL_PATH} + ${MKDIR} ${LUAEBPF_INSTALL_PATH}/bytecode ${LUAEBPF_INSTALL_PATH}/emit ${LUAEBPF_INSTALL_PATH}/lib/bpf + ${INSTALL} -m 0644 *.lua ${LUAEBPF_INSTALL_PATH} + ${INSTALL} -m 0644 ${TABLES} ${LUAEBPF_INSTALL_PATH}/bytecode + ${INSTALL} -m 0644 emit/*.lua ${LUAEBPF_INSTALL_PATH}/emit + ${INSTALL} -m 0644 lib/*.lua ${LUAEBPF_INSTALL_PATH}/lib + ${INSTALL} -m 0644 lib/bpf/*.lua ${LUAEBPF_INSTALL_PATH}/lib/bpf + +uninstall: + ${RM} -r ${LUAEBPF_INSTALL_PATH} ${LUA_CPATH}/luaebpf + diff --git a/README.md b/README.md index a17793d..b2608bf 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,17 @@ sudo lunatik run --dev=eth0 runt **Experimental.** The compiler, the modules a program file's body requires and the part of Lua it compiles may change in a minor release. -luaebpf builds and runs inside Lunatik, which mounts it at `luaebpf/`; `lunatik bpf` compiles a -program file and `lunatik run --dev` attaches it. [Compiling Lua to eBPF](doc/guide/08-luaebpf.md) is +luaebpf runs on a host Lunatik is installed on: `lunatik bpf` compiles a program file and +`lunatik run --dev` attaches it. `make` builds it against libbpf and a Lunatik checkout with its `lua` +submodule, the one `LUNATIK` names, `..` by default, where Lunatik mounts luaebpf at `luaebpf/`, and +`sudo make install` puts it where the host's Lua finds it: + +```Shell +make LUNATIK=../lunatik +sudo make install +``` + +[Compiling Lua to eBPF](doc/guide/08-luaebpf.md) is the guide for the author of a program file, [Lua to eBPF by example](doc/guide/09-luaebpf-by-example.md) lists what each construct compiles to, and the [design notes](doc/design/luaebpf/README.md) say how the compiler is built. [AGENTS.md](AGENTS.md) holds the conventions for contributors. From 5b519390689ebe53b8c8730f8fa0638fa2279b16 Mon Sep 17 00:00:00 2001 From: Lourival Vieira Neto Date: Wed, 25 Nov 2026 23:28:23 -0300 Subject: [PATCH 2/3] AGENTS: luaebpf builds against the Lunatik checkout it names Build, install, test names the targets of luaebpf's Makefile and what each reads of the Lunatik checkout, and says what the compiler keeps reading of a Lunatik install once it builds alone: lunatic, lunatik.class and lunatik.config, the kernel scripts and the linux.* tables, which the update on #28 keeps in Lunatik. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index aa82219..a59e0da 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -19,7 +19,7 @@ calls the kernel Lua scripts Lunatik runs. | `init.lua` and the modules beside it | the compiler, `luaebpf`, a package of the CLI's Lua: `proto.lua` and `undump.lua` read the chunk `lunatic` writes, `emit/` lowers it, a module per construct, `btf.lua`, `elf.lua` and `writer.lua` write the object, `errors.lua` reads the errors a running program counted | | `lib/` | the modules a program file's body requires to declare what the object carries, `bpf.xdp`, `bpf.tc`, `bpf.map` and `bpf.vmlinux`; one named as a kernel module of Lunatik is that module's compile-time twin | | `loader.c` | `luaebpf.loader`, the C module over libbpf that loads, attaches and pins an object | -| `bytecode.c` | the program Lunatik's build runs, as `lunatic` is built, to print the tables of the kernel's Lua bytecode the compiler reads | +| `bytecode.c` | the program `make` runs, as `lunatic` is built, to print the tables of the kernel's Lua bytecode the compiler reads | | `doc/guide/` | the guide's two pages of the compiler, which Lunatik's site builds | | `doc/design/luaebpf/` | the compiler's design notes | | `harness/` | the harness luaebpf shares with Lunatik (luainkernel/harness), mounted as a submodule | @@ -30,16 +30,26 @@ calls the kernel Lua scripts Lunatik runs. ## Build, install, test -luaebpf builds and runs inside a Lunatik checkout, which mounts it at `luaebpf/` (luainkernel/lunatik#2598): -Lunatik's Makefile builds `lunatic`, the bytecode tables and the loader against its own `lua/` and -libbpf, and installs the compiler where the CLI's Lua finds it; the program files of its tests live in -Lunatik's `tests/luaebpf/`, and its examples in Lunatik's `examples/`. A change is built, installed and -run there, with the mount at the change's commit, as Lunatik's AGENTS.md says of a build and of its -suite; the program files Lunatik's suite compiles are the compiler's tests. - -The compiler reads what Lunatik installs, the `linux.*` tables under `/lib/modules/lua`, the ones the -kernel scripts a program calls read too, and `lunatik.config`: a constant the compiler needs enters -Lunatik's `autogen/specs.lua` through a pull request of Lunatik. + make LUNATIK= # .. by default + sudo make install + sudo make uninstall + make clean + +`make` builds the loader against libbpf and the bytecode tables, both over the `lua/` of the Lunatik +checkout `LUNATIK` names, `..` by default, which is Lunatik's root where it mounts luaebpf at +`luaebpf/` (luainkernel/lunatik#2598); the kernel's table takes that checkout's `lunatik_conf.h` too, +and the flags Lunatik's Makefile builds `lunatic` with. `sudo make install` puts the compiler under +the host's `LUA_PATH` as `luaebpf/` and the loader under `LUA_CPATH` as `luaebpf/loader.so`. + +The program files of its tests live in Lunatik's `tests/luaebpf/`, and its examples in Lunatik's +`examples/`. A change is built, installed and run in a Lunatik checkout, with the mount at the +change's commit, as Lunatik's AGENTS.md says of a build and of its suite; the program files Lunatik's +suite compiles are the compiler's tests. + +The compiler reads what Lunatik installs: `lunatic` on the `PATH`, `lunatik.class` and +`lunatik.config` under the host's `LUA_PATH`, and under `/lib/modules/lua` the kernel scripts a +program file's body requires and the `linux.*` tables, the ones those scripts read too. A constant +the compiler needs enters Lunatik's `autogen/specs.lua` through a pull request of Lunatik. ### Checks From 3786299e3af6946cfc7b6e16c9a55bc88c86cde5 Mon Sep 17 00:00:00 2001 From: Lourival Vieira Neto Date: Wed, 25 Nov 2026 23:29:09 -0300 Subject: [PATCH 3/3] ci: build luaebpf against Lunatik's master The job clones Lunatik with its lua submodule, as a contributor's checkout beside luaebpf's holds it, and runs make over it: a change that breaks the build against the Lunatik it is built with fails its pull request rather than the build of the Lunatik that moves the mount to it. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/build.yml | 31 +++++++++++++++++++++++++++++++ AGENTS.md | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..428d976 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,31 @@ +# +# SPDX-FileCopyrightText: (c) 2026 Ring Zero Desenvolvimento de Software LTDA +# SPDX-License-Identifier: MIT OR GPL-2.0-only +# + +name: Build +on: + pull_request: + push: + branches: + - master +jobs: + build: + name: Build against Lunatik's master + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + # retry.sh is the harness's + submodules: true + - name: Checkout Lunatik + # make reads lua/ and lunatik_conf.h of the Lunatik checkout LUNATIK names + run: | + git clone --depth 1 https://github.com/luainkernel/lunatik.git .lunatik + git -C .lunatik submodule update --init --depth 1 lua + - name: Install dependencies + # apt-get update exits 0 on an index it could not fetch, so each try fetches the indexes again + run: bash harness/tools/retry.sh 5 90 sudo sh -c 'apt-get update -qq && apt-get install -yq libbpf-dev' + - name: Build + run: make LUNATIK=.lunatik diff --git a/AGENTS.md b/AGENTS.md index a59e0da..5cc5a5d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -26,7 +26,7 @@ calls the kernel Lua scripts Lunatik runs. | `.agents/rules/` | luaebpf's own rules for a kind of file | | `.agents/harness.conf` | what the shared scripts of `harness/` read of this tree | | `.claude/` | Claude Code's wiring of the guards, the checks, the skills, the agents and the rules, with no logic of its own | -| `.github/` | the CI workflow | +| `.github/` | the CI workflows | ## Build, install, test