|
| 1 | +# SPDX-License-Identifier: BSD-2-Clause |
| 2 | +VERISTAT_VERSION ?= 0.1 |
| 3 | +DEBUG ?= |
| 4 | + |
| 5 | +OUTPUT := .output |
| 6 | +LIBBPF_SRC := $(abspath ../../libbpf/src) |
| 7 | +LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a) |
| 8 | +# Use our own libbpf API headers and Linux UAPI headers distributed with |
| 9 | +# libbpf to avoid dependency on system-wide headers, which could be missing or |
| 10 | +# outdated |
| 11 | +INCLUDES := -I$(OUTPUT) -I../../libbpf/include/uapi |
| 12 | +CFLAGS := -g -Wall -DVERISTAT_VERSION=\"$(VERISTAT_VERSION)\" -O$(if $(DEBUG),0,2) |
| 13 | +ALL_CFLAGS := $(CFLAGS) $(EXTRA_LDFLAGS) |
| 14 | +ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS) |
| 15 | + |
| 16 | +export VERISTAT_VERSION |
| 17 | + |
| 18 | +ifeq ($(V),1) |
| 19 | + Q = |
| 20 | + msg = |
| 21 | +else |
| 22 | + Q = @ |
| 23 | + msg = @printf ' %-8s %s%s\n' \ |
| 24 | + "$(1)" \ |
| 25 | + "$(patsubst $(abspath $(OUTPUT))/%,%,$(2))" \ |
| 26 | + "$(if $(3), $(3))"; |
| 27 | + MAKEFLAGS += --no-print-directory |
| 28 | +endif |
| 29 | + |
| 30 | +define allow-override |
| 31 | + $(if $(or $(findstring environment,$(origin $(1))),\ |
| 32 | + $(findstring command line,$(origin $(1)))),,\ |
| 33 | + $(eval $(1) = $(2))) |
| 34 | +endef |
| 35 | + |
| 36 | +$(call allow-override,CC,$(CROSS_COMPILE)cc) |
| 37 | +$(call allow-override,LD,$(CROSS_COMPILE)ld) |
| 38 | + |
| 39 | +# for installation |
| 40 | +INSTALL := install |
| 41 | +prefix := /usr/local |
| 42 | +bindir := $(prefix)/bin |
| 43 | + |
| 44 | +.PHONY: all |
| 45 | +all: veristat |
| 46 | + |
| 47 | +.PHONY: clean |
| 48 | +clean: |
| 49 | + $(call msg,CLEAN) |
| 50 | + $(Q)rm -rf $(OUTPUT) veristat |
| 51 | + |
| 52 | +.PHONY: install |
| 53 | +install: all |
| 54 | + mkdir -p $(DESTDIR)$(bindir)/ |
| 55 | + $(INSTALL) -pt $(DESTDIR)$(bindir)/ veristat |
| 56 | + |
| 57 | +$(OUTPUT) $(OUTPUT)/libbpf: |
| 58 | + $(call msg,MKDIR,$@) |
| 59 | + $(Q)mkdir -p $@ |
| 60 | + |
| 61 | +# Build libbpf |
| 62 | +$(LIBBPF_OBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(OUTPUT)/libbpf |
| 63 | + $(call msg,LIB,$@) |
| 64 | + $(Q)$(MAKE) -C $(LIBBPF_SRC) BUILD_STATIC_ONLY=1 \ |
| 65 | + OBJDIR=$(dir $@)/libbpf DESTDIR=$(dir $@) \ |
| 66 | + INCLUDEDIR= LIBDIR= UAPIDIR= \ |
| 67 | + EXTRA_CFLAGS="-g -O$(if $(DEBUG),0,2)" \ |
| 68 | + install |
| 69 | + |
| 70 | +# Build .c files |
| 71 | +$(OUTPUT)/%.o: %.c $(wildcard %.h) | $(OUTPUT) |
| 72 | + $(call msg,CC,$@) |
| 73 | + $(Q)$(CC) $(ALL_CFLAGS) $(INCLUDES) -c $(filter %.c,$^) -o $@ |
| 74 | + |
| 75 | +# Build application binary |
| 76 | +veristat: $(OUTPUT)/veristat.o $(LIBBPF_OBJ) | $(OUTPUT) |
| 77 | + $(call msg,BINARY,$@) |
| 78 | + $(Q)$(CC) $(ALL_CFLAGS) $^ $(ALL_LDFLAGS) -lelf -lz -o $@ |
| 79 | + |
| 80 | +# delete failed targets |
| 81 | +.DELETE_ON_ERROR: |
| 82 | + |
| 83 | +# keep intermediate (.skel.h, .bpf.o, etc) targets |
| 84 | +.SECONDARY: |
0 commit comments