Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -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
34 changes: 22 additions & 12 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,37 @@ 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 |
| `.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

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=<a Lunatik checkout> # .. 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

Expand Down
63 changes: 63 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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

13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading