-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMakefile
127 lines (101 loc) · 3.78 KB
/
Makefile
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
120
121
122
123
124
125
126
127
# SPDX-License-Identifier: Apache-2.0
# Copyright 2024 Authors of Tarian & the Organization created Tarian
# executable Files Path
EXECUTABLE=bin
# executable File name
EXECUTABLE_FILE = tarian_detector
# header files path in the project.
HEADERS_PATH=headers
# required C header files
HEADERS_FILES = bpf_helpers bpf_helper_defs bpf_endian bpf_core_read bpf_tracing
# extracts the major, minor, and patch version numbers of the kernel version
KERNEL_VERSION = $(word 1, $(subst -, ,$(shell uname -r)))
KV_S = $(subst ., ,$(KERNEL_VERSION))
KV_MAJOR = $(word 1,$(KV_S))
KV_MINOR = $(word 2,$(KV_S))
KV_PATCH = $(word 3,$(KV_S))
# flags to be passed to clang for compiling C files.
CFLAGS := -O2 -g -Wall -Werror \
-DLINUX_VERSION_MAJOR=$(KV_MAJOR) \
-DLINUX_VERSION_MINOR=$(KV_MINOR) \
-DLINUX_VERSION_PATCH=$(KV_PATCH) \
$(CFLAGS)
# architecture of the system.
ARCH := $(shell uname -m | sed 's/x86_64/amd64/g; s/aarch64/arm64/g')
# project dependencies
DEPENDENCIES:=golang clang-12 llvm-12 libelf-dev libbpf-dev linux-tools-$(shell uname -r) linux-headers-$(shell uname -r)
# package manager
PKG_MGR=apt-get
# recipe for listing available commands.
help:
@echo "make build - builds the project"
@echo "make run - start the application"
@echo "make dev_run - builds and starts the application"
@echo "make install - installs the project dependencies"
@echo "make uinstall - uinstalls the project dependencies"
@echo "make bpf_helpers - generates the header files"
@echo "make lint - analyze the project code"
@echo "make file FILE_PATH=</your/file/path/filename.ext> - create a file with copyrights and license comments"
@echo "make clean - deletes all object files(*.o)"
@echo "make help - prints the available commands"
# recipe for running all 'go generate' commands in the project.
gen: export CURR_ARCH := $(ARCH)
gen: export BPF_CFLAGS := $(CFLAGS)
gen:
go generate ./...
# recipe for Building the Project
build: gen
go build -o ./$(EXECUTABLE)/ ./cmd/...
# recipe to start the application
run: execute
# recipe to build and start the application
dev_run: build execute
# recipe to execute the executable file
execute: export LINUX_VERSION_MAJOR := $(KV_MAJOR)
execute: export LINUX_VERSION_MINOR := $(KV_MINOR)
execute: export LINUX_VERSION_PATCH := $(KV_PATCH)
execute:
./$(EXECUTABLE)/$(EXECUTABLE_FILE)
# recipe to install project dependencies
install:
$(PKG_MGR) -y update && \
$(PKG_MGR) -y install $(DEPENDENCIES)
# recipe to uninstall project dependencies
uninstall:
$(PKG_MGR) -y remove $(DEPENDENCIES)
# recipe to copy the C program dependent header files.
headers: vmlinux bpf_helpers
# recipe to generate the vmlinux.h file
vmlinux:
@bpftool btf dump file /sys/kernel/btf/vmlinux format c > $(HEADERS_PATH)/vmlinux.h
# recipe to copy the bpf_helper header files from system into /headers folder
bpf_helpers:
@for file in $(HEADERS_FILES) ; do \
cp /usr/include/bpf/$${file}.h $(HEADERS_PATH)/$${file}.h; \
done
# run go fmt against code.
fmt:
go fmt ./...
# run go vet against code.
vet:
go vet ./...
lint: fmt vet
revive -formatter stylish -config .revive.toml ./pkg/...
staticcheck ./...
.PHONY: clean file
# recipe to create a file with license and copyright details.
file:
ifeq ($(FILE_PATH),)
@echo "ERROR: Please provide a valid file path using 'make $@ FILE_PATH=/your/file/path/filename.ext'"
@exit 1
endif
@if [ -e "$(FILE_PATH)" ]; then \
echo "ERROR: File already exists at $(FILE_PATH)"; \
exit 1; \
else \
echo "Creating file: $(FILE_PATH)" && echo "// SPDX-License-Identifier: Apache-2.0 \n// Copyright $(shell date +'%Y') Authors of Tarian & the Organization created Tarian" > $(FILE_PATH); \
echo "File created successfully at path: $(shell realpath $(FILE_PATH))"; \
fi
# recipe to remove all object files(*.o)
clean:
find -type f -name *.o -delete