-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMakefile
More file actions
192 lines (162 loc) · 5.49 KB
/
Makefile
File metadata and controls
192 lines (162 loc) · 5.49 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
# SPDX-FileCopyrightText: 2024 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#
#
PROJECT_NAME := n3iwf
VERSION ?= $(shell cat ./VERSION 2>/dev/null || echo "dev")
# Extract minimum Go version from go.mod file
GOLANG_MINIMUM_VERSION ?= $(shell awk '/^go / {print $$2}' go.mod 2>/dev/null || echo "1.25")
# Number of processors for parallel builds (Linux only)
NPROCS := $(shell nproc)
## Docker configuration
DOCKER_REGISTRY ?=
DOCKER_REPOSITORY ?=
DOCKER_TAG ?= $(VERSION)
DOCKER_IMAGE_PREFIX ?= 5gc-
DOCKER_IMAGENAME := $(DOCKER_REGISTRY)$(DOCKER_REPOSITORY)$(DOCKER_IMAGE_PREFIX)$(PROJECT_NAME):$(DOCKER_TAG)
DOCKER_BUILDKIT ?= 1
DOCKER_BUILD_ARGS ?= --build-arg MAKEFLAGS=-j$(NPROCS)
DOCKER_PULL ?= --pull
## Docker labels with better error handling
DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url origin 2>/dev/null || echo "unknown")
DOCKER_LABEL_VCS_REF ?= $(shell \
echo "$${GIT_COMMIT:-$${GITHUB_SHA:-$${CI_COMMIT_SHA:-$(shell \
if git rev-parse --git-dir > /dev/null 2>&1; then \
git rev-parse HEAD 2>/dev/null; \
else \
echo "unknown"; \
fi \
)}}}")
DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
## Build configuration
BINARY_NAME := $(PROJECT_NAME)
GO_PACKAGES ?= ./...
## Directory configuration
BIN_DIR := bin
COVERAGE_DIR := .coverage
## Go build configuration
GO_FILES := $(shell find . -name "*.go" ! -name "*_test.go" 2>/dev/null)
GO_FILES_ALL := $(shell find . -name "*.go" 2>/dev/null)
## Tool versions (for reproducible builds)
GOLANGCI_LINT_VERSION ?= latest
# Default target
.DEFAULT_GOAL := help
## Help target
help: ## Show this help message
@echo "Available targets:"
@awk 'BEGIN {FS = ":.*##"} /^[a-zA-Z_-]+:.*##/ { printf " %-20s %s\n", $$1, $$2 }' $(MAKEFILE_LIST) | sort
## Build targets
build: $(BIN_DIR)/$(BINARY_NAME) ## Build binary
all: build ## Build binary (alias for compatibility)
$(BIN_DIR)/$(BINARY_NAME): $(GO_FILES) | bin-dir
@echo "Building $(BINARY_NAME)..."
@CGO_ENABLED=0 go build -o $@ .
bin-dir: ## Create binary directory
@mkdir -p $(BIN_DIR)
## Docker targets
docker-build: ## Build Docker image
@echo "Building Docker image: $(DOCKER_IMAGENAME)"
@go mod vendor
@DOCKER_BUILDKIT=$(DOCKER_BUILDKIT) docker build $(DOCKER_PULL) $(DOCKER_BUILD_ARGS) \
--build-arg VERSION="$(VERSION)" \
--build-arg VCS_URL="$(DOCKER_LABEL_VCS_URL)" \
--build-arg VCS_REF="$(DOCKER_LABEL_VCS_REF)" \
--build-arg BUILD_DATE="$(DOCKER_LABEL_BUILD_DATE)" \
--tag $(DOCKER_IMAGENAME) \
. \
|| exit 1
@rm -rf vendor
docker-push: ## Push Docker image to registry
@echo "Pushing Docker image: $(DOCKER_IMAGENAME)"
@docker push $(DOCKER_IMAGENAME)
docker-clean: ## Remove local Docker image
@echo "Cleaning local Docker image..."
@docker rmi $(DOCKER_IMAGENAME) 2>/dev/null || true
## Testing targets
$(COVERAGE_DIR): ## Create coverage directory
@mkdir -p $(COVERAGE_DIR)
test: $(COVERAGE_DIR) ## Run unit tests with coverage
@echo "Running unit tests..."
@docker run --rm \
-v $(CURDIR):/$(PROJECT_NAME) \
-w /$(PROJECT_NAME) \
golang:$(GOLANG_MINIMUM_VERSION) \
go test \
-race \
-failfast \
-coverprofile=$(COVERAGE_DIR)/coverage-unit.txt \
-covermode=atomic \
-v \
$(GO_PACKAGES)
test-local: $(COVERAGE_DIR) ## Run unit tests locally (without Docker)
@echo "Running unit tests locally..."
@go test \
-race \
-failfast \
-coverprofile=$(COVERAGE_DIR)/coverage-unit.txt \
-covermode=atomic \
-v \
$(GO_PACKAGES)
## Code quality targets
fmt: ## Format Go code
@echo "Formatting Go code..."
@go fmt ./...
lint: ## Run linter
@echo "Running linter..."
@docker run --rm \
-v $(CURDIR):/app \
-w /app \
golangci/golangci-lint:$(GOLANGCI_LINT_VERSION) \
golangci-lint run -v --config /app/.golangci.yml
lint-local: ## Run linter locally (without Docker)
@echo "Running linter locally..."
@golangci-lint run -v --config .golangci.yml
check-reuse: ## Check REUSE compliance
@echo "Checking REUSE compliance..."
@docker run --rm \
-v $(CURDIR):/$(PROJECT_NAME) \
-w /$(PROJECT_NAME) \
omecproject/reuse-verify:latest \
reuse lint
check: fmt lint check-reuse ## Run all code quality checks
## Utility targets
clean: ## Clean build artifacts
@echo "Cleaning build artifacts..."
@rm -rf $(BIN_DIR)
@rm -rf $(COVERAGE_DIR)
@rm -rf vendor
@docker system prune -f --filter label=org.opencontainers.image.source="https://github.com/omec-project/$(PROJECT_NAME)" 2>/dev/null || true
print-version: ## Print current version
@echo $(VERSION)
env: ## Print environment variables
@echo "PROJECT_NAME=$(PROJECT_NAME)"
@echo "VERSION=$(VERSION)"
@echo "GOLANG_MINIMUM_VERSION=$(GOLANG_MINIMUM_VERSION)"
@echo "BINARY_NAME=$(BINARY_NAME)"
@echo "DOCKER_REGISTRY=$(DOCKER_REGISTRY)"
@echo "DOCKER_REPOSITORY=$(DOCKER_REPOSITORY)"
@echo "DOCKER_IMAGE_PREFIX=$(DOCKER_IMAGE_PREFIX)"
@echo "DOCKER_TAG=$(DOCKER_TAG)"
@echo "DOCKER_IMAGENAME=$(DOCKER_IMAGENAME)"
@echo "DOCKER_LABEL_VCS_URL=$(DOCKER_LABEL_VCS_URL)"
@echo "DOCKER_LABEL_VCS_REF=$(DOCKER_LABEL_VCS_REF)"
@echo "NPROCS=$(NPROCS)"
## Phony targets
.PHONY: all \
bin-dir \
build \
check \
check-reuse \
clean \
docker-build \
docker-clean \
docker-push \
env \
fmt \
help \
lint \
lint-local \
print-version \
test \
test-local