|
| 1 | +# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) |
| 2 | +OUTPUT := .output |
| 3 | +CLANG ?= clang |
| 4 | +LIBBPF_SRC := $(abspath ../../../third_party/libbpf/src) |
| 5 | +BPFTOOL_SRC := $(abspath ../../../third_party/bpftool/src) |
| 6 | +LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a) |
| 7 | +BPFTOOL_OUTPUT ?= $(abspath $(OUTPUT)/bpftool) |
| 8 | +BPFTOOL ?= $(BPFTOOL_OUTPUT)/bootstrap/bpftool |
| 9 | +ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' \ |
| 10 | + | sed 's/arm.*/arm/' \ |
| 11 | + | sed 's/aarch64/arm64/' \ |
| 12 | + | sed 's/ppc64le/powerpc/' \ |
| 13 | + | sed 's/mips.*/mips/' \ |
| 14 | + | sed 's/riscv64/riscv/' \ |
| 15 | + | sed 's/loongarch64/loongarch/') |
| 16 | +VMLINUX := ../../../third_party/vmlinux/$(ARCH)/vmlinux.h |
| 17 | +# Use our own libbpf API headers and Linux UAPI headers distributed with |
| 18 | +# libbpf to avoid dependency on system-wide headers, which could be missing or |
| 19 | +# outdated |
| 20 | +INCLUDES := -I$(OUTPUT) -I../../../third_party/libbpf/include/uapi -I$(dir $(VMLINUX)) |
| 21 | +CFLAGS := -g -Wall |
| 22 | +ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS) |
| 23 | + |
| 24 | +APPS = launchlate # minimal minimal_legacy uprobe kprobe fentry usdt sockfilter tc ksyscall |
| 25 | + |
| 26 | +CARGO ?= $(shell which cargo) |
| 27 | +ifeq ($(strip $(CARGO)),) |
| 28 | +BZS_APPS := |
| 29 | +else |
| 30 | +BZS_APPS := # profile |
| 31 | +APPS += $(BZS_APPS) |
| 32 | +# Required by libblazesym |
| 33 | +ALL_LDFLAGS += -lrt -ldl -lpthread -lm |
| 34 | +endif |
| 35 | + |
| 36 | +# Get Clang's default includes on this system. We'll explicitly add these dirs |
| 37 | +# to the includes list when compiling with `-target bpf` because otherwise some |
| 38 | +# architecture-specific dirs will be "missing" on some architectures/distros - |
| 39 | +# headers such as asm/types.h, asm/byteorder.h, asm/socket.h, asm/sockios.h, |
| 40 | +# sys/cdefs.h etc. might be missing. |
| 41 | +# |
| 42 | +# Use '-idirafter': Don't interfere with include mechanics except where the |
| 43 | +# build would have failed anyways. |
| 44 | +CLANG_BPF_SYS_INCLUDES ?= $(shell $(CLANG) -v -E - </dev/null 2>&1 \ |
| 45 | + | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') |
| 46 | + |
| 47 | +ifeq ($(V),1) |
| 48 | + Q = |
| 49 | + msg = |
| 50 | +else |
| 51 | + Q = @ |
| 52 | + msg = @printf ' %-8s %s%s\n' \ |
| 53 | + "$(1)" \ |
| 54 | + "$(patsubst $(abspath $(OUTPUT))/%,%,$(2))" \ |
| 55 | + "$(if $(3), $(3))"; |
| 56 | + MAKEFLAGS += --no-print-directory |
| 57 | +endif |
| 58 | + |
| 59 | +define allow-override |
| 60 | + $(if $(or $(findstring environment,$(origin $(1))),\ |
| 61 | + $(findstring command line,$(origin $(1)))),,\ |
| 62 | + $(eval $(1) = $(2))) |
| 63 | +endef |
| 64 | + |
| 65 | +$(call allow-override,CC,$(CROSS_COMPILE)cc) |
| 66 | +$(call allow-override,LD,$(CROSS_COMPILE)ld) |
| 67 | + |
| 68 | +.PHONY: all |
| 69 | +all: $(APPS) vec_add |
| 70 | + |
| 71 | +vec_add: vec_add.cu |
| 72 | + @if command -v nvcc >/dev/null 2>&1; then \ |
| 73 | + nvcc -cudart shared vec_add.cu -o vec_add -g; \ |
| 74 | + else \ |
| 75 | + echo "Warning: CUDA not found, skipping vec_add build"; \ |
| 76 | + fi |
| 77 | + |
| 78 | +.PHONY: clean |
| 79 | +clean: |
| 80 | + $(call msg,CLEAN) |
| 81 | + $(Q)rm -rf $(OUTPUT) $(APPS) vec_add |
| 82 | + |
| 83 | +$(OUTPUT) $(OUTPUT)/libbpf $(BPFTOOL_OUTPUT): |
| 84 | + $(call msg,MKDIR,$@) |
| 85 | + $(Q)mkdir -p $@ |
| 86 | + |
| 87 | +# Build libbpf |
| 88 | +$(LIBBPF_OBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(OUTPUT)/libbpf |
| 89 | + $(call msg,LIB,$@) |
| 90 | + $(Q)$(MAKE) -C $(LIBBPF_SRC) BUILD_STATIC_ONLY=1 \ |
| 91 | + OBJDIR=$(dir $@)/libbpf DESTDIR=$(dir $@) \ |
| 92 | + INCLUDEDIR= LIBDIR= UAPIDIR= \ |
| 93 | + install |
| 94 | + |
| 95 | +# Build bpftool |
| 96 | +$(BPFTOOL): | $(BPFTOOL_OUTPUT) |
| 97 | + $(call msg,BPFTOOL,$@) |
| 98 | + $(Q)$(MAKE) ARCH= CROSS_COMPILE= OUTPUT=$(BPFTOOL_OUTPUT)/ -C $(BPFTOOL_SRC) bootstrap |
| 99 | + |
| 100 | + |
| 101 | +$(LIBBLAZESYM_SRC)/target/release/libblazesym.a:: |
| 102 | + $(Q)cd $(LIBBLAZESYM_SRC) && $(CARGO) build --features=cheader,dont-generate-test-files --release |
| 103 | + |
| 104 | +$(LIBBLAZESYM_OBJ): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPUT) |
| 105 | + $(call msg,LIB, $@) |
| 106 | + $(Q)cp $(LIBBLAZESYM_SRC)/target/release/libblazesym.a $@ |
| 107 | + |
| 108 | +$(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPUT) |
| 109 | + $(call msg,LIB,$@) |
| 110 | + $(Q)cp $(LIBBLAZESYM_SRC)/target/release/blazesym.h $@ |
| 111 | + |
| 112 | +# Build BPF code |
| 113 | +$(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL) |
| 114 | + $(call msg,BPF,$@) |
| 115 | + $(Q)$(CLANG) -Xlinker --export-dynamic -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \ |
| 116 | + $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \ |
| 117 | + -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) |
| 118 | + $(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@) |
| 119 | + |
| 120 | +# Generate BPF skeletons |
| 121 | +$(OUTPUT)/%.skel.h: $(OUTPUT)/%.bpf.o | $(OUTPUT) $(BPFTOOL) |
| 122 | + $(call msg,GEN-SKEL,$@) |
| 123 | + $(Q)$(BPFTOOL) gen skeleton $< > $@ |
| 124 | + |
| 125 | +# Build user-space code |
| 126 | +$(patsubst %,$(OUTPUT)/%.o,$(APPS)): %.o: %.skel.h |
| 127 | + |
| 128 | +$(OUTPUT)/%.o: %.c $(wildcard %.h) | $(OUTPUT) |
| 129 | + $(call msg,CC,$@) |
| 130 | + $(Q)$(CC) $(CFLAGS) $(INCLUDES) -c $(filter %.c,$^) -o $@ |
| 131 | + |
| 132 | +$(patsubst %,$(OUTPUT)/%.o,$(BZS_APPS)): $(LIBBLAZESYM_HEADER) |
| 133 | + |
| 134 | +$(BZS_APPS): $(LIBBLAZESYM_OBJ) |
| 135 | + |
| 136 | +# Build application binary |
| 137 | +$(APPS): %: $(OUTPUT)/%.o $(LIBBPF_OBJ) | $(OUTPUT) |
| 138 | + $(call msg,BINARY,$@) |
| 139 | + $(Q)$(CC) $(CFLAGS) $^ $(ALL_LDFLAGS) -lelf -lz -o $@ |
| 140 | + |
| 141 | +# delete failed targets |
| 142 | +.DELETE_ON_ERROR: |
| 143 | + |
| 144 | +# keep intermediate (.skel.h, .bpf.o, etc) targets |
| 145 | +.SECONDARY: |
0 commit comments