|
| 1 | +.PHONY: all clean |
| 2 | +.SUFFIXES: |
| 3 | + |
| 4 | +include ./Makefile.common |
| 5 | + |
| 6 | +VFSDIR := vfs/ |
| 7 | +NETWORKDIR := network/ |
| 8 | +BPFTESTDIR := test/ |
| 9 | + |
| 10 | +NETWORK = bpf_network.o |
| 11 | +VFS = bpf_vfs.o bpf_vfs_v511.o bpf_vfs_v62.o |
| 12 | +# BPFTEST = bpf_lseek.o |
| 13 | + |
| 14 | +OBJSDIR := objs/ |
| 15 | +DEPSDIR := deps/ |
| 16 | + |
| 17 | +VFSOBJ := $(addprefix $(OBJSDIR),$(VFS)) |
| 18 | +NETWORKOBJ := $(addprefix $(OBJSDIR),$(NETWORK)) |
| 19 | +TESTOBJ := $(addprefix $(OBJSDIR),$(BPFTEST)) |
| 20 | +OBJS := $(VFSOBJ) $(NETWORKOBJ) $(TESTOBJ) |
| 21 | +LLOBJS := $(patsubst $(OBJSDIR)%.o,$(OBJSDIR)%.ll,$(OBJS)) |
| 22 | +DEPS := $(patsubst $(OBJSDIR)%.ll,$(DEPSDIR)%.d,$(LLOBJS)) |
| 23 | + |
| 24 | +all: $(OBJS) $(DEPS) |
| 25 | + |
| 26 | +# NB: https://www.gnu.org/software/make/manual/html_node/Prerequisite-Types.html |
| 27 | +$(OBJS): | $(OBJSDIR) |
| 28 | +$(DEPS): | $(DEPSDIR) |
| 29 | +$(LLOBJS): | $(OBJSDIR) |
| 30 | + |
| 31 | +$(OBJSDIR): |
| 32 | + mkdir $(OBJSDIR) |
| 33 | + |
| 34 | +$(DEPSDIR): |
| 35 | + mkdir $(DEPSDIR) |
| 36 | + |
| 37 | +define DEFINE_VARIANT |
| 38 | +VAR := $1 |
| 39 | +deps/bpf_vfs_$$(VAR).d: vfs/bpf_vfs.c |
| 40 | +endef |
| 41 | + |
| 42 | +# Generic build targets for each sub-dir |
| 43 | + |
| 44 | +$(eval $(call DEFINE_VARIANT,v511)) |
| 45 | +$(eval $(call DEFINE_VARIANT,v62)) |
| 46 | + |
| 47 | +# Build only for relevant architectures |
| 48 | +ifeq ($(BPF_TARGET_COMPILE),1) |
| 49 | + |
| 50 | +# VFSDIR |
| 51 | +objs/%.ll: $(VFSDIR)%.c |
| 52 | + $(CLANG) $(CLANG_FLAGS) -D__KERNEL_POST_v62 -c $< -o $@ |
| 53 | + |
| 54 | +objs/%_v511.ll: |
| 55 | + $(CLANG) $(CLANG_FLAGS) -D__KERNEL_PRE_v511 -c $< -o $@ |
| 56 | + |
| 57 | +objs/%_v62.ll: |
| 58 | + $(CLANG) $(CLANG_FLAGS) -D__KERNEL_POST_v512_PRE_v62 -c $< -o $@ |
| 59 | + |
| 60 | +$(DEPSDIR)%.d: $(VFSDIR)%.c |
| 61 | + $(CLANG) $(CLANG_FLAGS) -D__KERNEL_POST_v62 -MM -MP -MT $(patsubst $(DEPSDIR)%.d, $(OBJSDIR)%.ll, $@) $< > $@ |
| 62 | + |
| 63 | +$(DEPSDIR)%_v511.d: |
| 64 | + $(CLANG) $(CLANG_FLAGS) -D__KERNEL_PRE_v511 -MM -MP -MT $(patsubst $(DEPSDIR)%.d, $(OBJSDIR)%.ll, $@) $< > $@ |
| 65 | + |
| 66 | +$(DEPSDIR)%_v62.d: |
| 67 | + $(CLANG) $(CLANG_FLAGS) -D__KERNEL_POST_v512_PRE_v62 -MM -MP -MT $(patsubst $(DEPSDIR)%.d, $(OBJSDIR)%.ll, $@) $< > $@ |
| 68 | + |
| 69 | +# NETWORKDIR |
| 70 | +objs/%.ll: $(NETWORKDIR)%.c |
| 71 | + $(CLANG) $(CLANG_FLAGS) -c $< -o $@ |
| 72 | + |
| 73 | +$(DEPSDIR)%.d: $(NETWORKDIR)%.c |
| 74 | + $(CLANG) $(CLANG_FLAGS) -MM -MP -MT $(patsubst $(DEPSDIR)%.d, $(OBJSDIR)%.ll, $@) $< > $@ |
| 75 | + |
| 76 | +# BPFTESTDIR |
| 77 | +objs/%.ll: $(BPFTESTDIR)%.c |
| 78 | + $(CLANG) $(CLANG_FLAGS) -c $< -o $@ |
| 79 | + |
| 80 | +$(DEPSDIR)%.d: $(BPFTESTDIR)%.c |
| 81 | + $(CLANG) $(CLANG_FLAGS) -MM -MP -MT $(patsubst $(DEPSDIR)%.d, $(OBJSDIR)%.ll, $@) $< > $@ |
| 82 | + |
| 83 | +# Remaining objects are built without mcpu=v2 |
| 84 | +objs/%.o: objs/%.ll |
| 85 | + $(LLC) $(LLC_FLAGS) -filetype=obj $< -o $@ |
| 86 | + |
| 87 | +# include dependencies, see https://lists.gnu.org/archive/html/make-w32/2004-03/msg00062.html |
| 88 | +ifeq (,$(filter $(MAKECMDGOALS),clean run-test)) |
| 89 | +-include $(DEPS) |
| 90 | +endif |
| 91 | + |
| 92 | +endif |
| 93 | + |
| 94 | +# the 'test' target is already taken |
| 95 | +run-test: |
| 96 | + $(MAKE) -C tests test |
| 97 | + |
| 98 | +# SUBDIRS=tests |
| 99 | + |
| 100 | +clean: |
| 101 | + @$(ECHO_CLEAN) |
| 102 | + $(QUIET) $(foreach TARGET,$(SUBDIRS), \ |
| 103 | + $(MAKE) -C $(TARGET) clean) |
| 104 | + $(QUIET)rm -f $(OBJSDIR)*.{o,ll,i,s} |
| 105 | + $(QUIET)rm -f $(DEPSDIR)*.d |
0 commit comments