| # SPDX-License-Identifier: GPL-2.0-only | |
| # Copyright (c) 2026 Jarkko Sakkinen | |
| VERSION := 0.1.0 | |
| BPF_CLANG ?= clang | |
| BPFTOOL ?= bpftool | |
| PKG_CONFIG ?= pkg-config | |
| CLANG_FORMAT ?= clang-format | |
| SHELLCHECK ?= shellcheck | |
| INSTALL ?= install | |
| BUILD_DIR ?= build | |
| UAPI_DIR ?= $(CURDIR)/build/uapi/include | |
| PREFIX ?= /usr/local | |
| BINDIR ?= $(PREFIX)/bin | |
| CFLAGS ?= -O2 -g | |
| BPF_CFLAGS ?= -O2 -g | |
| TARGET := $(shell $(CC) -dumpmachine) | |
| ifeq ($(and $(filter x86_64-% aarch64-%,$(TARGET)),$(findstring -linux,$(TARGET))),) | |
| $(error ucells requires x86-64 or AArch64 Linux (got $(TARGET))) | |
| endif | |
| DEPS := libbpf >= 1.0 | |
| ifeq ($(filter clean uninstall format ci check,$(MAKECMDGOALS)),) | |
| ifneq ($(shell $(PKG_CONFIG) --exists '$(DEPS)' && echo ok),ok) | |
| $(error missing dependencies: $(DEPS)) | |
| endif | |
| ifneq ($(words $(wildcard $(addprefix $(UAPI_DIR)/,asm/unistd.h linux/runfd.h))),2) | |
| $(error runfd UAPI headers not found in $(UAPI_DIR); run make headers_install) | |
| endif | |
| endif | |
| DEP_CFLAGS := $(shell $(PKG_CONFIG) --cflags libbpf 2>/dev/null) | |
| DEP_LIBS := $(shell $(PKG_CONFIG) --libs libbpf 2>/dev/null) | |
| SRCS := $(wildcard src/*.c) | |
| OBJS := $(SRCS:src/%.c=$(BUILD_DIR)/%.o) | |
| BIN := $(BUILD_DIR)/ucells | |
| BPF_OBJ := $(BUILD_DIR)/ucells_lsm.bpf.o | |
| BPF_SKEL := $(BUILD_DIR)/ucells_lsm.skel.h | |
| UCELLS_CPPFLAGS := -D_GNU_SOURCE -DUCELLS_VERSION='"$(VERSION)"' \ | |
| -Ibpf -I$(BUILD_DIR) -isystem $(UAPI_DIR) $(DEP_CFLAGS) $(CPPFLAGS) | |
| UCELLS_CFLAGS := -std=c23 -Wall -Wextra -Wpedantic -Werror $(CFLAGS) | |
| .PHONY: all check format ci install uninstall clean | |
| all: $(BIN) | |
| $(BIN): $(OBJS) | |
| $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(DEP_LIBS) $(LDLIBS) | |
| $(BUILD_DIR)/%.o: src/%.c | $(BUILD_DIR) | |
| $(CC) $(UCELLS_CPPFLAGS) $(UCELLS_CFLAGS) -MD -MP -c -o $@ $< | |
| # The skeleton embeds the BPF object as a single string literal. | |
| $(BUILD_DIR)/bpf_lsm.o: UCELLS_CFLAGS += -Wno-overlength-strings | |
| $(BUILD_DIR)/bpf_lsm.o: $(BPF_SKEL) | |
| $(BPF_SKEL): $(BPF_OBJ) | |
| $(BPFTOOL) gen skeleton $< name ucells_lsm > $@.tmp | |
| mv $@.tmp $@ | |
| $(BPF_OBJ): bpf/ucells_lsm.bpf.c | $(BUILD_DIR) | |
| $(BPF_CLANG) $(BPF_CFLAGS) -Wall -Wextra -Werror -target bpf \ | |
| -MMD -MP -c -o $@ $< | |
| $(BUILD_DIR): | |
| mkdir -p $@ | |
| check: ci | |
| format: | |
| $(CLANG_FORMAT) --dry-run --Werror src/*.c src/*.h | |
| ci: | |
| MAKE="$(MAKE)" BPF_CLANG="$(BPF_CLANG)" BPFTOOL="$(BPFTOOL)" \ | |
| CLANG_FORMAT="$(CLANG_FORMAT)" SHELLCHECK="$(SHELLCHECK)" \ | |
| ./scripts/ci.sh | |
| install: $(BIN) | |
| $(INSTALL) -D -m 0755 $(BIN) "$(DESTDIR)$(BINDIR)/ucells" | |
| uninstall: | |
| rm -f "$(DESTDIR)$(BINDIR)/ucells" | |
| clean: | |
| rm -rf "$(BUILD_DIR)" | |
| -include $(OBJS:.o=.d) $(BPF_OBJ:.o=.d) |