forked from qpoint-io/qtap
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
330 lines (291 loc) · 11.2 KB
/
Makefile
File metadata and controls
330 lines (291 loc) · 11.2 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# =============================================================================
# 🔄 Load Environment & Configs
# =============================================================================
# Suppress the output of the commands
MAKEFLAGS += --no-print-directory
# Load .env file if it exists
ifneq (,$(wildcard .env))
include .env
# Only export specific variables that need to be in the environment
export GOOS
export GOARCH
export CGO_ENABLED
export PROJECT_NAME
export ORGANIZATION
export DATABASE_URL
export LOG_LEVEL
# Add other variables that actually need to be in the environment
endif
# =============================================================================
# 🎯 Project Configuration
# =============================================================================
# Project Settings
PROJECT_NAME ?= qtap
BINARY_NAME ?= qtap
ORGANIZATION ?= qpoint-io
DESCRIPTION ?= "🧬 Qtap: An eBPF agent that captures pre-encrypted network traffic, providing rich context about egress connections and their originating processes."
MAINTAINER ?= "Qpoint Team \<hello@qpoint.io\>"
LOG_LEVEL ?= info
VERSION=$${GIT_VERSION:-$$(git describe --tags --always --dirty)}
GIT_COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
BUILD_TIME ?= $(shell date -u '+%Y-%m-%d_%H:%M:%S')
# Go Configuration
GO ?= go
GOCMD = $(shell which go)
GOPATH ?= $(shell $(GO) env GOPATH)
GOBIN ?= $(GOPATH)/bin
GOOS ?= $(shell $(GO) env GOOS)
GOARCH ?= $(shell $(GO) env GOARCH)
CGO_ENABLED ?= 0
# Tools & Linters
GOLANGCI_LINT ?= $(GO) tool github.com/golangci/golangci-lint/cmd/golangci-lint
GOVULNCHECK ?= $(GO) tool golang.org/x/vuln/cmd/govulncheck
GOTESTSUM ?= $(GO) tool gotest.tools/gotestsum
MOCKGEN ?= $(GO) tool go.uber.org/mock/mockgen
# Directories
ROOT_DIR ?= $(shell pwd)
BIN_DIR ?= $(ROOT_DIR)/bin
BPF_DIR ?= $(ROOT_DIR)/bpf
DIST_DIR ?= $(ROOT_DIR)/dist
# Source Files
GOFILES = $(shell find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./.git/*")
GOPACKAGES = $(shell $(GO) list ./... | grep -v /vendor/)
# Build Configuration
BUILD_TAGS ?=
EXTRA_TAGS ?=
ALL_TAGS = $(BUILD_TAGS) $(EXTRA_TAGS)
# Linker Flags
LD_FLAGS += -s -w
LD_FLAGS += -X '$(shell GOWORK=off go list -m)/pkg/buildinfo.version=$(VERSION)'
LD_FLAGS += -X '$(shell GOWORK=off go list -m)/pkg/buildinfo.commit=$(GIT_COMMIT)'
LD_FLAGS += -X '$(shell GOWORK=off go list -m)/pkg/buildinfo.branch=$(GIT_BRANCH)'
LD_FLAGS += -X '$(shell GOWORK=off go list -m)/pkg/buildinfo.buildTime=$(BUILD_TIME)'
# Performance & Debug Flags
GCFLAGS ?=
ASMFLAGS ?=
# Test Configuration
TEST_TIMEOUT ?= 5m
TEST_TIMEOUT_E2E ?= 20m
TEST_FLAGS ?= -v -race
BENCH_FLAGS ?= -benchmem
BENCH_TIME ?= 2s
TEST_PATTERN ?= .
SKIP_PATTERN ?=
# Cross-Compilation Targets
PLATFORMS ?= \
linux/amd64/- \
linux/arm64/-
# =============================================================================
# 🎨 Terminal Colors & Emoji
# =============================================================================
# Colors
BLUE := \033[34m
GREEN := \033[32m
YELLOW := \033[33m
RED := \033[31m
MAGENTA := \033[35m
CYAN := \033[36m
WHITE := \033[37m
BOLD := \033[1m
RST := \033[0m
# Status Indicators - define these as functions, not variables
# Status Indicators
INFO := $(shell printf "$(BLUE)ℹ️ ")
SUCCESS := $(shell printf "$(GREEN)✅ ")
WARN := $(shell printf "$(YELLOW)⚠️ ")
ERROR := $(shell printf "$(RED)❌ ")
WORKING := $(shell printf "$(CYAN)🔨 ")
DEBUG := $(shell printf "$(MAGENTA)🔍 ")
ROCKET := $(shell printf "$(GREEN)🚀 ")
PACKAGE := $(shell printf "$(CYAN)📦 ")
TRASH := $(shell printf "$(YELLOW)🗑️ ")
RESET := $(shell printf "$(RST)")
# =============================================================================
# 🎯 Core Build System
# =============================================================================
.PHONY: build
build: $(BIN_DIR) generate ## Build for the current platform
@echo $(INFO) Building $(BINARY_NAME)... $(RESET)
CGO_ENABLED=$(CGO_ENABLED) \
$(GO) build -tags '$(ALL_TAGS)' \
-ldflags '$(LD_FLAGS)' \
-gcflags '$(GCFLAGS)' \
-asmflags '$(ASMFLAGS)' \
-o $(BIN_DIR)/$(notdir $(BINARY_NAME)) \
cmd/$(BINARY_NAME)/main.go
@echo $(SUCCESS) Build complete! $(RESET)
.PHONY: build-devtools
build-devtools:
@echo $(INFO) Building devtools... $(RESET)
cd pkg/devtools/app && npm ci --prefer-offline && npm run build
@echo $(SUCCESS) Devtools built! $(RESET)
# =============================================================================
# 🔄 Development Workflow
# =============================================================================
.PHONY: run
run: build ## Run the application
@echo $(ROCKET) Running $(PROJECT_NAME)... $(RESET)
./bin/$(BINARY_NAME) --log-level=$(LOG_LEVEL) --log-encoding=console
.PHONY: run-config
run-config: build ## Run the application with a specific config
@echo $(ROCKET) Running $(PROJECT_NAME) with config... $(RESET)
./bin/$(BINARY_NAME) --log-level=$(LOG_LEVEL) --log-encoding=console --config=$$(find ./examples -type f -name "*.yaml" | go tool gum filter --prompt="> " --indicator=">" --placeholder="Select a config file..." --header="Select a config file to run")
.PHONY: generate
generate: ## Run code generation
@echo $(WORKING) Running code generation... $(RESET)
$(GO) generate ./...
@echo $(SUCCESS) Generation complete! $(RESET)
# =============================================================================
# 🧪 Testing & Quality
# =============================================================================
.PHONY: test
test: ## Run tests
@echo $(INFO) Running tests... $(RESET)
$(GOTESTSUM) --format pkgname --hide-summary=skipped --format-hide-empty-pkg --format-icons=hivis -- \
-timeout $(TEST_TIMEOUT) \
-run '$(TEST_PATTERN)' \
$(if $(SKIP_PATTERN),-skip '$(SKIP_PATTERN)') \
./...
.PHONY: e2e
e2e: ## Run e2e tests
@echo $(INFO) Running e2e tests... $(RESET)
$(GOTESTSUM) --format testname --format-hide-empty-pkg --format-icons=hivis \
--junitfile /tmp/e2e-tests-results.xml \
-- \
-timeout $(TEST_TIMEOUT_E2E) \
-run '$(TEST_PATTERN)' \
-tags e2e \
$(if $(SKIP_PATTERN),-skip '$(SKIP_PATTERN)') \
./e2e/...
.PHONY: lint
lint: ## Run linters
@echo $(INFO) Running linters... $(RESET)
$(GOLANGCI_LINT) run --config ./.golangci.yaml --fix
@echo $(SUCCESS) Lint complete! $(RESET)
.PHONY: fmt
fmt: ## Format code
@echo $(INFO) Formatting Go code... $(RESET)
$(GO) fmt ./...
@echo $(SUCCESS) Format Go complete! $(RESET)
@echo $(INFO) Formatting BPF code... $(RESET)
@find ./bpf -type f -not -path "./bpf/headers/*" -name "*.[ch]" | xargs clang-format -i --Werror
@echo $(SUCCESS) Format BPF complete! $(RESET)
.PHONY: vet
vet: ## Run go vet
@echo $(INFO) Running go vet... $(RESET)
$(GO) vet ./...
@echo $(SUCCESS) Vet complete! $(RESET)
.PHONY: security
security: ## Run security checks
@echo $(INFO) Running security checks... $(RESET)
$(GOVULNCHECK) ./...
@echo $(SUCCESS) Security check complete! $(RESET)
# =============================================================================
# 🏗️ Build Variations
# =============================================================================
.PHONY: build-all
build-all: $(DIST_DIR) ## Build for all platforms
@echo $(WORKING) Building for all platforms... $(RESET)
@$(foreach platform,$(PLATFORMS),\
$(eval OS := $(word 1,$(subst /, ,$(platform)))) \
$(eval ARCH := $(word 2,$(subst /, ,$(platform)))) \
$(eval ARM := $(word 3,$(subst /, ,$(platform)))) \
echo $(WORKING) Building for $(OS) $(ARCH)... && \
GOOS=$(OS) GOARCH=$(ARCH) $(if $(ARM),GOARM=$(ARM)) \
CGO_ENABLED=$(CGO_ENABLED) \
$(GO) build -tags '$(ALL_TAGS)' \
-ldflags '$(LD_FLAGS)' \
-o $(DIST_DIR)/$(BINARY_NAME)-$(OS)-$(ARCH) \
./cmd/$(BINARY_NAME)/main.go ; \
)
@echo $(PACKAGE) Creating release archives... $(RESET)
@cd $(DIST_DIR) && \
for file in $(BINARY_NAME)-* ; do \
if [ -f "$$file" ] && [ "$$file" != *.tar.gz ]; then \
tar czf "$$file-$(VERSION).tar.gz" "$$file" || exit 1; \
rm -f "$$file"; \
fi \
done
@echo $(SUCCESS) All platforms built and archived! $(RESET)
.PHONY: build-debug
build-debug: GCFLAGS += -N -l ## Build with debug symbols
build-debug: BUILD_TAGS += debug
build-debug: build
.PHONY: build-race
build-race: CGO_ENABLED=1 ## Build with race detector
build-race: BUILD_TAGS += race
build-race: build
# =============================================================================
# 🔄 CI/CD Integration
# =============================================================================
.PHONY: ci
ci: ## Run CI pipeline
@echo $(INFO) Running CI pipeline... $(RESET)
$(MAKE) deps-verify
$(MAKE) lint
$(MAKE) security
if [ -n "$$(git status --porcelain)" ]; then \
if [ "$${ENV}" = "dev" ]; then \
echo $(WARN) Working tree is not clean. This will fail CI checks. $(RESET); \
else \
git status; \
git --no-pager diff; \
echo 'Working tree is not clean, did you forget to run "make fmt" or "make generate"?'; \
exit 1; \
fi \
fi
$(MAKE) test
@echo $(SUCCESS) CI pipeline complete! $(RESET)
# =============================================================================
# 🧹 Cleanup & Maintenance
# =============================================================================
.PHONY: clean
clean: ## Clean build artifacts
@echo $(TRASH) Cleaning build artifacts... $(RESET)
rm -rf $(BIN_DIR) $(DIST_DIR)
$(GO) clean -cache -testcache -modcache
@echo $(SUCCESS) Clean complete! $(RESET)
.PHONY: deps
deps: ## Install dependencies
@echo $(WORKING) Installing dependencies... $(RESET)
$(GO) mod download
@echo $(SUCCESS) Dependencies installed! $(RESET)
.PHONY: deps-update
deps-update: ## Update dependencies
@echo $(WORKING) Updating dependencies... $(RESET)
$(GO) get -u ./...
$(GO) mod tidy
@echo $(SUCCESS) Dependencies updated! $(RESET)
.PHONY: deps-verify
deps-verify: ## Verify dependencies
@echo $(INFO) Verifying dependencies... $(RESET)
$(GO) mod verify
@echo $(SUCCESS) Dependencies verified! $(RESET)
# =============================================================================
# 🛠️ Tools & Utilities
# =============================================================================
.PHONY: version
version: ## Display version information
@echo "$(CYAN)Version:$(RST) $(VERSION)"
@echo "$(CYAN)Commit:$(RST) $(GIT_COMMIT)"
@echo "$(CYAN)Branch:$(RST) $(GIT_BRANCH)"
@echo "$(CYAN)Built:$(RST) $(BUILD_TIME)"
@echo "$(CYAN)Go version:$(RST) $(shell $(GO) version)"
# =============================================================================
# 📁 Directory Creation
# =============================================================================
$(BIN_DIR) $(DIST_DIR) $(DOCS_DIR):
mkdir -p $@
# =============================================================================
# 💡 Help
# =============================================================================
.PHONY: help
help: ## Show this help message
@echo "$(CYAN)$(BOLD) $(DESCRIPTION)$(RST)"
@echo "$(WHITE)Maintained by $(MAINTAINER)$(RST)\n"
@echo "$(CYAN)$(BOLD)Available targets:$(RST)"
@awk 'BEGIN {FS = ":.*##"; printf ""} \
/^[a-zA-Z_-]+:.*?##/ { printf " $(CYAN)%-20s$(RST) %s\n", $$1, $$2 } \
/^##@/ { printf "\n$(MAGENTA)%s$(RST)\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
.DEFAULT_GOAL := help