-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move selftest/libbpf-module to libbpf * Fix: small fix after uprobe support Closes: #33 $ make libbpfgo-static-test ./libbpfgo.go:790:15: errptrError format %s has arg pid of wrong type int ./libbpfgo.go:802:14: Sprintf format %s has arg offset of wrong type uint32 * Readme.md: small rephrasing * Update Readme.md with build instructions and minor fixes * Makefile: improvements for the entire tree Currently you will find the following GNU Makefile rules: | Makefile Rule | Description | |--------------------------|-----------------------------------| | all | builds libbpfgo (dynamic) | | clean | cleans entire tree | | selftest | builds all selftests (static) | | selftest-run | runs all selftests (static) | * libbpf dynamically linked (libbpf from OS) | Makefile Rule | Description | |--------------------------|-----------------------------------| | libbpfgo-dynamic | builds dynamic libbpfgo (libbpf) | | libbpfgo-dynamic-test | 'go test' with dynamic libbpfgo | | selftest-dynamic | build tests with dynamic libbpfgo | | selftest-dynamic-run | run tests using dynamic libbpfgo | * statically compiled (libbpf submodule) | Makefile Rule | Description | |--------------------------|-----------------------------------| | libbpfgo-static | builds static libbpfgo (libbpf) | | libbpfgo-static-test | 'go test' with static libbpfgo | | selftest-static | build tests with static libbpfgo | | selftest-static-run | run tests using static libbpfgo | * examples $ make libbpfgo-static => libbpfgo statically linked with libbpf $ make -C selftest/perfbuffers => single selftest build (static libbpf) $ make -C selftest/perfbuffers run-dynamic => single selftest run (dynamic libbpf) $ make selftest-static-run => will build & run all static selftests > Note 01: dynamic builds need your OS to have a *recent enough* libbpf package (and its headers) installed. Sometimes, recent features might require the use of backported OS packages in order for your OS to contain latest *libbpf* features (sometimes required by libbpfgo). > Note 02: static builds need `git submodule init` first. Make sure to sync the *libbpf* git submodule before trying to statically compile or test the *libbpfgo* repository. * selftest/map-update: fixes for the new tree structure * Makefile: better info on errors * Makefile: turn static builds into full static All static binaries (and selftests) were being built statically against libbpf only. This change makes them full static binaries. * Makefile: un-phony vmlinux.h
- Loading branch information
Rafael David Tinoco
authored
Jul 12, 2021
1 parent
4928d36
commit a683635
Showing
41 changed files
with
675 additions
and
407 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
selftest/dist | ||
output* | ||
selftest/*/main | ||
selftest/*/*.o | ||
selftest/*/*-static | ||
selftest/*/*-dynamic | ||
selftest/uprobe/ctest | ||
selftest/uprobe/gotest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
[submodule "selftest/libbpf-module"] | ||
path = selftest/libbpf-module | ||
url = https://github.com/libbpf/libbpf | ||
[submodule "libbpf"] | ||
path = libbpf | ||
url = https://github.com/libbpf/libbpf.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,161 @@ | ||
TARGET_BPF := test/test.bpf.o | ||
VMLINUX_H = test/vmlinux.h | ||
BASEDIR = $(abspath ./) | ||
|
||
GO_SRC := $(shell find . -type f -name '*.go') | ||
BPF_SRC := $(shell find . -type f -name '*.bpf.c') | ||
PWD := $(shell pwd) | ||
OUTPUT = ./output | ||
SELFTEST = ./selftest | ||
|
||
LIBBPF_HEADERS := /usr/include/bpf | ||
LIBBPF := "-lbpf" | ||
CC = gcc | ||
CLANG = clang | ||
|
||
.PHONY: all | ||
all: test | ||
ARCH := $(shell uname -m) | ||
ARCH := $(subst x86_64,amd64,$(ARCH)) | ||
|
||
$(VMLINUX_H): | ||
bpftool btf dump file /sys/kernel/btf/vmlinux format c > test/vmlinux.h | ||
BTFFILE = /sys/kernel/btf/vmlinux | ||
BPFTOOL = $(shell which bpftool || /bin/false) | ||
VMLINUXH = $(OUTPUT)/vmlinux.h | ||
|
||
go_env := CC=gcc CGO_CFLAGS="-I $(LIBBPF_HEADERS)" CGO_LDFLAGS="$(LIBBPF)" | ||
.PHONY: test | ||
test: $(TARGET_BPF) $(GO_SRC) | ||
$(go_env) go test -ldflags '-extldflags "-static"' . | ||
# libbpf | ||
|
||
$(TARGET_BPF): $(BPF_SRC) $(VMLINUX_H) | ||
clang \ | ||
-g -O2 -c -target bpf \ | ||
-o $@ $< | ||
LIBBPF_SRC = $(abspath ./libbpf/src) | ||
LIBBPF_OBJ = $(abspath ./$(OUTPUT)/libbpf.a) | ||
LIBBPF_OBJDIR = $(abspath ./$(OUTPUT)/libbpf) | ||
LIBBPF_DESTDIR = $(abspath ./$(OUTPUT)) | ||
|
||
.PHONY: clean | ||
clean: | ||
rm $(TARGET_BPF) $(VMLINUX_H) | ||
CFLAGS = -g -O2 -Wall -fpie | ||
LDFLAGS = | ||
|
||
# golang | ||
|
||
CGO_CFLAGS_STATIC = "-I$(abspath $(OUTPUT))" | ||
CGO_LDFLAGS_STATIC = "-lelf -lz $(LIBBPF_OBJ)" | ||
CGO_EXTLDFLAGS_STATIC = '-w -extldflags "-static"' | ||
|
||
CGO_CFGLAGS_DYN = "-I. -I/usr/include/" | ||
CGO_LDFLAGS_DYN = "-lelf -lz -lbpf" | ||
|
||
# default == shared lib from OS package | ||
|
||
all: libbpfgo-dynamic | ||
test: libbpfgo-dynamic-test | ||
|
||
# libbpfgo test object | ||
|
||
libbpfgo-test-bpf-static: libbpfgo-static # needed for serialization | ||
$(MAKE) -C $(SELFTEST)/build | ||
|
||
libbpfgo-test-bpf-dynamic: libbpfgo-dynamic # needed for serialization | ||
$(MAKE) -C $(SELFTEST)/build | ||
|
||
libbpfgo-test-bpf-clean: | ||
$(MAKE) -C $(SELFTEST)/build clean | ||
|
||
# libbpf: shared | ||
|
||
libbpfgo-dynamic: $(OUTPUT)/libbpf | ||
CC=$(CLANG) \ | ||
CGO_CFLAGS=$(CGO_CFLAGS_DYN) \ | ||
CGO_LDFLAGS=$(CGO_LDFLAGS_DYN) \ | ||
go build . | ||
|
||
libbpfgo-dynamic-test: libbpfgo-test-bpf-dynamic | ||
CC=$(CLANG) \ | ||
CGO_CFLAGS=$(CGO_CFLAGS_DYN) \ | ||
CGO_LDFLAGS=$(CGO_LDFLAGS_DYN) \ | ||
sudo -E go test . | ||
|
||
# libbpf: static | ||
|
||
libbpfgo-static: $(VMLINUXH) | $(LIBBPF_OBJ) | ||
CC=$(CLANG) \ | ||
CGO_CFLAGS=$(CGO_CFLAGS_STATIC) \ | ||
CGO_LDFLAGS=$(CGO_LDFLAGS_STATIC) \ | ||
GOOS=linux GOARCH=$(ARCH) \ | ||
go build \ | ||
-tags netgo -ldflags $(CGO_EXTLDFLAGS_STATIC) \ | ||
. | ||
|
||
libbpfgo-static-test: libbpfgo-test-bpf-static | ||
CC=$(CLANG) \ | ||
CGO_CFLAGS=$(CGO_CFLAGS_STATIC) \ | ||
CGO_LDFLAGS=$(CGO_LDFLAGS_STATIC) \ | ||
GOOS=linux GOARCH=$(ARCH) \ | ||
sudo -E -- go test \ | ||
-tags netgo -ldflags $(CGO_EXTLDFLAGS_STATIC) \ | ||
. | ||
|
||
# vmlinux header file | ||
|
||
.PHONY: vmlinuxh | ||
vmlinuxh: $(VMLINUXH) | ||
|
||
$(VMLINUXH): $(OUTPUT) | ||
@if [ ! -f $(BTFFILE) ]; then \ | ||
echo "ERROR: kernel does not seem to support BTF"; \ | ||
exit 1; \ | ||
fi | ||
@if [ ! -f $(VMLINUXH) ]; then \ | ||
echo "INFO: generating $(VMLINUXH) from $(BTFFILE)"; \ | ||
$(BPFTOOL) btf dump file $(BTFFILE) format c > $(VMLINUXH); \ | ||
fi | ||
|
||
# static libbpf generation for the git submodule | ||
|
||
$(LIBBPF_OBJ): $(wildcard $(LIBBPF_SRC)/*.[ch]) | $(OUTPUT)/libbpf | ||
CC="$(CC)" CFLAGS="$(CFLAGS)" LD_FLAGS="$(LDFLAGS)" \ | ||
$(MAKE) -C $(LIBBPF_SRC) \ | ||
BUILD_STATIC_ONLY=1 \ | ||
OBJDIR=$(LIBBPF_OBJDIR) \ | ||
DESTDIR=$(LIBBPF_DESTDIR) \ | ||
INCLUDEDIR= LIBDIR= UAPIDIR= install | ||
|
||
# selftests | ||
|
||
SELFTESTS = $(shell find $(SELFTEST) -mindepth 1 -maxdepth 1 -type d ! -name 'common' ! -name 'build') | ||
|
||
define FOREACH | ||
SELFTESTERR=0; \ | ||
for DIR in $(SELFTESTS); do \ | ||
echo "INFO: entering $$DIR..."; \ | ||
$(MAKE) -j8 -C $$DIR $(1) || SELFTESTERR=1; \ | ||
done; \ | ||
if [ $$SELFTESTERR -eq 1 ]; then \ | ||
exit 1; \ | ||
fi | ||
endef | ||
|
||
.PHONY: selftest | ||
.PHONY: selftest-static | ||
.PHONY: selftest-dynamic | ||
.PHONY: selftest-run | ||
.PHONY: selftest-static-run | ||
.PHONY: selftest-dynamic-run | ||
.PHONY: selftest-clean | ||
|
||
selftest: selftest-static | ||
|
||
selftest-static: | ||
$(call FOREACH, main-static) | ||
selftest-dynamic: | ||
$(call FOREACH, main-dynamic) | ||
|
||
selftest-run: selftest-static-run | ||
|
||
selftest-static-run: | ||
$(call FOREACH, run-static) | ||
selftest-dynamic-run: | ||
$(call FOREACH, run-dynamic) | ||
|
||
selftest-clean: | ||
$(call FOREACH, clean) | ||
|
||
# output | ||
|
||
$(OUTPUT): | ||
mkdir -p $(OUTPUT) | ||
|
||
$(OUTPUT)/libbpf: | ||
mkdir -p $(OUTPUT)/libbpf | ||
|
||
# cleanup | ||
|
||
clean: selftest-clean libbpfgo-test-bpf-clean | ||
rm -rf $(OUTPUT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
BASEDIR = $(abspath ../../) | ||
|
||
OUTPUT = ../../output | ||
|
||
CC = gcc | ||
CLANG = clang | ||
|
||
CFLAGS = -g -O2 -Wall -fpie | ||
LDFLAGS = | ||
|
||
TEST = libbpfgo_test | ||
|
||
all: $(TEST).bpf.o | ||
|
||
## main tree dependency | ||
|
||
outputdir: | ||
$(MAKE) -C $(BASEDIR) outputdir | ||
|
||
vmlinuxh: outputdir | ||
$(MAKE) -C $(BASEDIR) vmlinuxh | ||
|
||
## test bpf dependency | ||
|
||
$(TEST).bpf.o: $(TEST).bpf.c | ||
$(MAKE) -C $(BASEDIR) vmlinuxh | ||
$(CLANG) $(CFLAGS) -target bpf -I$(OUTPUT) -c $< -o $@ | ||
|
||
## clean | ||
|
||
clean: | ||
rm -f *.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.