-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrbpf.inc.mk
119 lines (94 loc) · 3.02 KB
/
rbpf.inc.mk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
include $(SMING_HOME)/util.mk
ifeq (,$(RBPF_OUTDIR))
$(error RBPF_OUTDIR undefined)
endif
RBPF_OBJDIR := $(RBPF_OUTDIR)/obj
RBPF_INCDIR := $(RBPF_OUTDIR)/include
RBPF_COMPONENT_DIR := $(call dirx,$(firstword $(MAKEFILE_LIST)))
# Obtain blob file path
# $1 -> source file(s)
define BlobFile
$(addprefix $(RBPF_OBJDIR)/,$(patsubst %,%.bin,$(basename $1)))
endef
# List of relative paths to source files
RBPF_SOURCES := $(patsubst $(CURDIR)/%,%,$(call ListAllFiles,$(CURDIR),*.c *.cpp))
# Header file for all defined containers
RBPF_INCFILE := $(RBPF_INCDIR)/rbpf/containers.h
# Source file with actual blob imports
RBPF_SRCFILE := $(RBPF_OUTDIR)/containers.cpp
CLANG ?= clang
all: blobs
CLANG_SYS_INCLUDE := $(call FixPath,$(shell $(CLANG) -print-file-name=include))
INC_FLAGS = \
-nostdinc \
-isystem "$(CLANG_SYS_INCLUDE)" \
-Iinclude \
-I$(RBPF_COMPONENT_DIR)/bpf/include \
-I$(RBPF_COMPONENT_DIR)/src/include/bpf
# Generate build targets
# $1 -> Source file
# $2 -> Blob file
define GenerateTarget
TARGET_OBJ := $(2:.bin=.obj)
$$(TARGET_OBJ): $1
@echo "rBPF: CC $$<"
$(Q) mkdir -p $$(@D)
$(Q) $$(CLANG) -Wall -Wextra -g3 -Os $$(INC_FLAGS) -target bpf -c $$< -o $$@
$2: $$(TARGET_OBJ)
$(Q) $$(RBPF_GENRBF) generate $$< [email protected]
$(Q) mv -f [email protected] $$@
endef
$(foreach f,$(RBPF_SOURCES),$(eval $(call GenerateTarget,$f,$(call BlobFile,$f))))
# Get name to use for blob symbol
# $1 -> source file
define GetSymbolName
$(subst /,_,$(basename $1))
endef
# Generate code for header file
# $1 -> source file
define GenerateHeader
@printf "DECLARE_FSTR_ARRAY($(call GetSymbolName,$1), uint8_t)\n" >> $@
endef
$(RBPF_INCFILE): $(call BlobFile,$(RBPF_SOURCES))
@echo "rBPF: Create $(patsubst $(PROJECT_DIR)/%,%,$@)"
@mkdir -p $(@D)
@echo "#pragma once" > $@
@echo "#include <FlashString/Array.hpp>" >> $@
@echo "" >> $@
@echo "namespace rBPF {" >> $@
@echo "namespace Container {" >> $@
$(foreach f,$(RBPF_SOURCES),$(call GenerateHeader,$f))
@echo "} // namespace Container" >> $@
@echo "} // namespace rBPF" >> $@
# Generate code for BLOB source file
# $1 -> source file
define GenerateSource
@printf "IMPORT_FSTR_ARRAY($(call GetSymbolName,$1), uint8_t, $(patsubst $(PROJECT_DIR)/%,PROJECT_DIR \"/%\",$(call BlobFile,$1)))\n" >> $@
endef
$(RBPF_SRCFILE): $(call BlobFile,$(RBPF_SOURCES))
@echo "rBPF: Create $(patsubst $(PROJECT_DIR)/%,%,$@)"
@echo "#include <FlashString/Array.hpp>" > $@
@echo "" >> $@
@echo "namespace rBPF {" >> $@
@echo "namespace Container {" >> $@
$(foreach f,$(RBPF_SOURCES),$(call GenerateSource,$f))
@echo "} // namespace Container" >> $@
@echo "} // namespace rBPF" >> $@
.PHONY: blobs
blobs: $(RBPF_INCFILE) $(RBPF_SRCFILE) | $(RBPF_INCDIR) $(RBPF_OBJDIR)
$(RBPF_INCDIR) $(RBPF_OBJDIR):
$(Q) mkdir -p $@
#
# $1 -> Path to blob
define DumpBlob
@echo Contents of \"$(patsubst $(RBPF_OBJDIR)/%,%,$1)\"
@echo -----------------------------
@$(RBPF_GENRBF) dump $1
@echo ""
endef
.PHONY: dump
dump: blobs
$(foreach f,$(call BlobFile,$(RBPF_SOURCES)),$(call DumpBlob,$f))
.PHONY: clean
clean:
$(Q) rm -rf $(RBPF_OUTDIR)