-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Makefile
115 lines (89 loc) · 2.65 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
.ONESHELL:
# .SHELLFLAGS = -e
SHELL = /bin/sh
GIT = $(shell which git || /bin/false)
OUTPUT = ./output
BPF_SRC = ./bpf
LIBPCAP = ./lib/libpcap
LIBPCAP_SRC = $(abspath $(LIBPCAP))
LIBPCAP_DIST_DIR ?= $(abspath $(OUTPUT)/libpcap)
LIBPCAP_HEADER_DIR = $(abspath $(LIBPCAP_DIST_DIR)/include)
LIBPCAP_OBJ_DIR = $(abspath $(LIBPCAP_DIST_DIR)/lib)
LIBPCAP_OBJ = $(abspath $(LIBPCAP_OBJ_DIR)/libpcap.a)
GIT_COMMIT ?= $(shell git rev-parse --short HEAD)
VERSION ?= $(shell git describe --tags --abbrev=0)
CGO_CFLAGS_STATIC = "-I$(LIBPCAP_HEADER_DIR)"
CGO_LDFLAGS_STATIC = "-L$(LIBPCAP_OBJ_DIR) -lpcap $(LIBPCAP_OBJ)"
CGO_ENABLED ?= 1
GOARCH ?= $(shell go env GOARCH)
GOOS ?= $(shell go env GOOS)
LDFLAGS := -linkmode "external" -extldflags "-static"
LDFLAGS += -X github.com/mozillazg/ptcpdump/internal.Version=$(VERSION)
LDFLAGS += -X github.com/mozillazg/ptcpdump/internal.GitCommit=$(GIT_COMMIT)
CARCH ?= $(shell uname -m)
LIBPCAP_ARCH = $(CARCH)-unknown-linux-gnu
LIBPCAP_CC ?= gcc
IMAGE_DEV ?= quay.io/ptcpdump/develop:latest
IMAGE_BIN ?= quay.io/ptcpdump/ptcpdump:latest
.PHONY: libpcap
libpcap: $(LIBPCAP_OBJ)
$(LIBPCAP_OBJ): $(LIBPCAP_SRC)/pcap.h $(wildcard $(LIBPCAP_SRC)/*.[ch]) | $(LIBPCAP_DIST_DIR)
cd $(LIBPCAP_SRC) && \
sh autogen.sh && \
CC=$(LIBPCAP_CC) ./configure --disable-shared --disable-usb --disable-netmap --disable-bluetooth --disable-dbus --without-libnl \
--host=$(LIBPCAP_ARCH) && \
$(MAKE) && \
$(MAKE) install prefix=$(LIBPCAP_DIST_DIR)
$(LIBPCAP_SRC)/pcap.h:
ifeq ($(wildcard $@), )
echo "INFO: updating submodule 'libpcap'"
$(GIT) submodule update --init --recursive
endif
$(LIBPCAP_DIST_DIR): $(LIBPCAP_SRC)
$(OUTPUT):
mkdir -p $(OUTPUT)
.PHONY: build
build: libpcap
CGO_CFLAGS=$(CGO_CFLAGS_STATIC) \
CGO_LDFLAGS=$(CGO_LDFLAGS_STATIC) \
CGO_ENABLED=1 go build -tags static -ldflags "$(LDFLAGS)"
.PHONY: test
test:
CGO_CFLAGS=$(CGO_CFLAGS_STATIC) \
CGO_LDFLAGS=$(CGO_LDFLAGS_STATIC) \
CGO_ENABLED=1 go test -v ./...
.PHONY: generate
generate: build-bpf
.PHONY: build-bpf
build-bpf:
TARGET=amd64 go generate ./...
TARGET=arm64 go generate ./...
.PHONY: build-bpf-via-docker
build-bpf-via-docker:
docker run --rm -v `pwd`:/app $(IMAGE_DEV) make build-bpf
.PHONY: build-via-docker
build-via-docker:
docker run --rm -v `pwd`:/app $(IMAGE_DEV) make build
.PHONY: lint
lint: deps fmt
.PHONY: fmt
fmt:
go fmt ./...
clang-format -i bpf/ptcpdump.c
clang-format -i bpf/*.h
clang-format -i bpf/headers/*.h
.PHONY: vet
vet:
go vet ./...
.PHONY: deps
deps:
go mod tidy
go mod vendor
.PHONY: e2e
e2e: lint build-bpf build
sudo bash testdata/run_e2e.sh
.PHONY: clean
clean:
$(MAKE) -C $(LIBPCAP_SRC) clean
rm -rf $(OUTPUT)
rm -f ./ptcpdump