|
| 1 | +# SPDX-FileCopyrightText: 2026 Intel Corporation |
1 | 2 | # SPDX-FileCopyrightText: 2021 Open Networking Foundation <info@opennetworking.org> |
2 | 3 | # Copyright 2019 free5GC.org |
3 | 4 | # |
4 | 5 | # SPDX-License-Identifier: Apache-2.0 |
5 | 6 | # |
| 7 | +# |
| 8 | + |
| 9 | +PROJECT_NAME := webui |
| 10 | +VERSION ?= $(shell cat ./VERSION 2>/dev/null || echo "dev") |
6 | 11 |
|
| 12 | +# Extract minimum Go version from go.mod file |
| 13 | +GOLANG_MINIMUM_VERSION ?= $(shell awk '/^go / {print $$2}' go.mod 2>/dev/null || echo "1.24") |
7 | 14 |
|
8 | | -PROJECT_NAME := sdcore |
9 | | -DOCKER_VERSION ?= $(shell cat ./VERSION) |
| 15 | +# Number of processors for parallel builds (Linux only) |
| 16 | +NPROCS := $(shell nproc) |
10 | 17 |
|
11 | | -## Docker related |
| 18 | +## Docker configuration |
12 | 19 | DOCKER_REGISTRY ?= |
13 | 20 | DOCKER_REPOSITORY ?= |
14 | | -DOCKER_TAG ?= ${DOCKER_VERSION} |
15 | | -DOCKER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}${PROJECT_NAME}:${DOCKER_TAG} |
| 21 | +DOCKER_TAG ?= $(VERSION) |
| 22 | +DOCKER_IMAGE_PREFIX ?= 5gc- |
| 23 | +DOCKER_IMAGENAME := $(DOCKER_REGISTRY)$(DOCKER_REPOSITORY)$(DOCKER_IMAGE_PREFIX)$(PROJECT_NAME):$(DOCKER_TAG) |
16 | 24 | DOCKER_BUILDKIT ?= 1 |
17 | | -DOCKER_BUILD_ARGS ?= |
18 | | - |
19 | | -## Docker labels. Only set ref and commit date if committed |
20 | | -DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) |
21 | | -DOCKER_LABEL_VCS_REF ?= $(shell git diff-index --quiet HEAD -- && git rev-parse HEAD || echo "unknown") |
22 | | -DOCKER_LABEL_COMMIT_DATE ?= $(shell git diff-index --quiet HEAD -- && git show -s --format=%cd --date=iso-strict HEAD || echo "unknown" ) |
| 25 | +DOCKER_BUILD_ARGS ?= --build-arg MAKEFLAGS=-j$(NPROCS) |
| 26 | +DOCKER_PULL ?= --pull |
| 27 | + |
| 28 | +## Docker labels with better error handling |
| 29 | +DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url origin 2>/dev/null || echo "unknown") |
| 30 | +DOCKER_LABEL_VCS_REF ?= $(shell \ |
| 31 | + echo "$${GIT_COMMIT:-$${GITHUB_SHA:-$${CI_COMMIT_SHA:-$(shell \ |
| 32 | + if git rev-parse --git-dir > /dev/null 2>&1; then \ |
| 33 | + git rev-parse HEAD 2>/dev/null; \ |
| 34 | + else \ |
| 35 | + echo "unknown"; \ |
| 36 | + fi \ |
| 37 | + )}}}") |
23 | 38 | DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") |
24 | 39 |
|
25 | | -DOCKER_TARGETS ?= webui |
26 | | - |
27 | | -# https://docs.docker.com/engine/reference/commandline/build/#specifying-target-build-stage---target |
28 | | -GO_BIN_PATH = bin |
29 | | -GO_SRC_PATH = ./ |
30 | | -C_BUILD_PATH = build |
31 | | -ROOT_PATH = $(shell pwd) |
32 | | - |
33 | | -WEBCONSOLE = webconsole |
| 40 | +## Build configuration |
| 41 | +BINARY_NAME := $(PROJECT_NAME) |
| 42 | +UI_BINARY_NAME := webconsole-ui |
| 43 | +GO_PACKAGES ?= ./... |
34 | 44 |
|
35 | | -WEBCONSOLE_GO_FILES = $(shell find ./ -name "*.go" ! -name "*_test.go") |
| 45 | +## Directory configuration |
| 46 | +BIN_DIR := bin |
| 47 | +COVERAGE_DIR := .coverage |
36 | 48 |
|
37 | | -VERSION = $(shell git describe --tags) |
38 | | -BUILD_TIME = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ") |
39 | | -WEBCONSOLE_COMMIT_HASH = $(shell git submodule status | grep $(WEBCONSOLE) | awk '{print $$(1)}' | cut -c1-8) |
40 | | -WEBCONSOLE_COMMIT_TIME = $(shell git log --pretty="%ai" -1 | awk '{time=$$(1)"T"$$(2)"Z"; print time}') |
| 49 | +## Go build configuration |
| 50 | +GO_FILES := $(shell find . -name "*.go" ! -name "*_test.go" 2>/dev/null) |
41 | 51 |
|
42 | | -.PHONY: $(NF) clean docker-build docker-push |
| 52 | +## Tool versions (for reproducible builds) |
| 53 | +GOLANGCI_LINT_VERSION ?= latest |
43 | 54 |
|
44 | | -.DEFAULT_GOAL: nfs |
| 55 | +# Default target |
| 56 | +.DEFAULT_GOAL := help |
45 | 57 |
|
46 | | -all: $(WEBCONSOLE) |
| 58 | +## Help target |
| 59 | +help: ## Show this help message |
| 60 | + @echo "Available targets:" |
| 61 | + @awk 'BEGIN {FS = ":.*##"} /^[a-zA-Z_-]+:.*##/ { printf " %-20s %s\n", $$1, $$2 }' $(MAKEFILE_LIST) | sort |
47 | 62 |
|
48 | | -$(WEBCONSOLE): $(GO_BIN_PATH)/$(WEBCONSOLE) |
| 63 | +## Build targets |
| 64 | +build: $(BIN_DIR)/$(BINARY_NAME) ## Build binary |
49 | 65 |
|
50 | | -$(GO_BIN_PATH)/$(WEBCONSOLE): server.go $(WEBCONSOLE_GO_FILES) |
51 | | - @echo "Start building $(@F)...." |
52 | | - go build -o $(ROOT_PATH)/$@ ./server.go |
| 66 | +all: build ## Build binary (alias for compatibility) |
53 | 67 |
|
54 | | -vpath %.go $(addprefix $(GO_SRC_PATH)/, $(GO_NF)) |
| 68 | +$(BIN_DIR)/$(BINARY_NAME): $(GO_FILES) | bin-dir |
| 69 | + @echo "Building $(BINARY_NAME)..." |
| 70 | + @CGO_ENABLED=0 go build -o $@ . |
55 | 71 |
|
56 | | -webconsole-ui: $(GO_BIN_PATH)/$(WEBCONSOLE)-ui |
| 72 | +webconsole-ui: $(BIN_DIR)/$(UI_BINARY_NAME) |
57 | 73 |
|
58 | | -$(GO_BIN_PATH)/$(WEBCONSOLE)-ui: server.go $(WEBCONSOLE_GO_FILES) |
59 | | - @echo "Start building $(@F)...." |
60 | | - go build --tags ui -o $(ROOT_PATH)/$@ ./server.go |
| 74 | +$(BIN_DIR)/$(UI_BINARY_NAME): $(GO_FILES) | bin-dir |
| 75 | + @echo "Building $(UI_BINARY_NAME)..." |
| 76 | + @CGO_ENABLED=0 go build --tags ui -o $@ . |
61 | 77 |
|
62 | | -clean: |
63 | | - rm -rf $(ROOT_PATH)/$(GO_BIN_PATH)/$(WEBCONSOLE) |
64 | | - rm -rf $(ROOT_PATH)/$(GO_BIN_PATH)/$(WEBCONSOLE)-ui |
| 78 | +bin-dir: ## Create binary directory |
| 79 | + @mkdir -p $(BIN_DIR) |
65 | 80 |
|
66 | | -docker-build: |
| 81 | +## Docker targets |
| 82 | +docker-build: ## Build Docker image |
| 83 | + @echo "Building Docker image: $(DOCKER_IMAGENAME)" |
67 | 84 | @go mod vendor |
68 | | - for target in $(DOCKER_TARGETS); do \ |
69 | | - DOCKER_BUILDKIT=$(DOCKER_BUILDKIT) docker build $(DOCKER_BUILD_ARGS) \ |
70 | | - --target $$target \ |
71 | | - --tag ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}5gc-$$target:${DOCKER_TAG} \ |
72 | | - --build-arg org_label_schema_version="${DOCKER_VERSION}" \ |
73 | | - --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
74 | | - --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
75 | | - --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
76 | | - --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
77 | | - . \ |
78 | | - || exit 1; \ |
79 | | - done |
80 | | - rm -rf vendor |
81 | | - |
82 | | -docker-push: |
83 | | - for target in $(DOCKER_TARGETS); do \ |
84 | | - docker push ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}5gc-$$target:${DOCKER_TAG}; \ |
85 | | - done |
86 | | - |
87 | | -.coverage: |
88 | | - rm -rf $(CURDIR)/.coverage |
89 | | - mkdir -p $(CURDIR)/.coverage |
90 | | - |
91 | | -test: .coverage |
92 | | - docker run --rm -v $(CURDIR):/webconsole -w /webconsole golang:latest \ |
| 85 | + @DOCKER_BUILDKIT=$(DOCKER_BUILDKIT) docker build $(DOCKER_PULL) $(DOCKER_BUILD_ARGS) \ |
| 86 | + --build-arg VERSION="$(VERSION)" \ |
| 87 | + --build-arg VCS_URL="$(DOCKER_LABEL_VCS_URL)" \ |
| 88 | + --build-arg VCS_REF="$(DOCKER_LABEL_VCS_REF)" \ |
| 89 | + --build-arg BUILD_DATE="$(DOCKER_LABEL_BUILD_DATE)" \ |
| 90 | + --tag $(DOCKER_IMAGENAME) \ |
| 91 | + . \ |
| 92 | + || exit 1 |
| 93 | + @rm -rf vendor |
| 94 | + |
| 95 | +docker-push: ## Push Docker image to registry |
| 96 | + @echo "Pushing Docker image: $(DOCKER_IMAGENAME)" |
| 97 | + @docker push $(DOCKER_IMAGENAME) |
| 98 | + |
| 99 | +docker-clean: ## Remove local Docker image |
| 100 | + @echo "Cleaning local Docker image..." |
| 101 | + @docker rmi $(DOCKER_IMAGENAME) 2>/dev/null || true |
| 102 | + |
| 103 | +## Testing targets |
| 104 | +$(COVERAGE_DIR): ## Create coverage directory |
| 105 | + @mkdir -p $(COVERAGE_DIR) |
| 106 | + |
| 107 | +test: $(COVERAGE_DIR) ## Run unit tests with coverage |
| 108 | + @echo "Running unit tests..." |
| 109 | + @docker run --rm \ |
| 110 | + -v $(CURDIR):/$(PROJECT_NAME) \ |
| 111 | + -w /$(PROJECT_NAME) \ |
| 112 | + golang:$(GOLANG_MINIMUM_VERSION) \ |
93 | 113 | go test \ |
| 114 | + -race \ |
94 | 115 | -failfast \ |
95 | | - -coverprofile=.coverage/coverage-unit.txt \ |
| 116 | + -coverprofile=$(COVERAGE_DIR)/coverage-unit.txt \ |
96 | 117 | -covermode=atomic \ |
97 | 118 | -v \ |
98 | | - ./ ./... |
| 119 | + $(GO_PACKAGES) |
| 120 | + |
| 121 | +test-local: $(COVERAGE_DIR) ## Run unit tests locally (without Docker) |
| 122 | + @echo "Running unit tests locally..." |
| 123 | + @go test \ |
| 124 | + -race \ |
| 125 | + -failfast \ |
| 126 | + -coverprofile=$(COVERAGE_DIR)/coverage-unit.txt \ |
| 127 | + -covermode=atomic \ |
| 128 | + -v \ |
| 129 | + $(GO_PACKAGES) |
| 130 | + |
| 131 | +## Code quality targets |
| 132 | +fmt: ## Format Go code |
| 133 | + @echo "Formatting Go code..." |
| 134 | + @go fmt ./... |
| 135 | + |
| 136 | +lint: ## Run linter |
| 137 | + @echo "Running linter..." |
| 138 | + @docker run --rm \ |
| 139 | + -v $(CURDIR):/app \ |
| 140 | + -w /app \ |
| 141 | + golangci/golangci-lint:$(GOLANGCI_LINT_VERSION) \ |
| 142 | + golangci-lint run -v --config /app/.golangci.yml |
| 143 | + |
| 144 | +lint-local: ## Run linter locally (without Docker) |
| 145 | + @echo "Running linter locally..." |
| 146 | + @golangci-lint run -v --config .golangci.yml |
| 147 | + |
| 148 | +check-reuse: ## Check REUSE compliance |
| 149 | + @echo "Checking REUSE compliance..." |
| 150 | + @docker run --rm \ |
| 151 | + -v $(CURDIR):/$(PROJECT_NAME) \ |
| 152 | + -w /$(PROJECT_NAME) \ |
| 153 | + omecproject/reuse-verify:latest \ |
| 154 | + reuse lint |
| 155 | + |
| 156 | +check: fmt lint check-reuse ## Run all code quality checks |
| 157 | + |
| 158 | +## Utility targets |
| 159 | +clean: ## Clean build artifacts |
| 160 | + @echo "Cleaning build artifacts..." |
| 161 | + @rm -rf $(BIN_DIR) |
| 162 | + @rm -rf $(COVERAGE_DIR) |
| 163 | + @rm -rf vendor |
| 164 | + @docker system prune -f --filter label=org.opencontainers.image.source="$(DOCKER_LABEL_VCS_URL)" 2>/dev/null || true |
| 165 | + |
| 166 | +print-version: ## Print current version |
| 167 | + @echo $(VERSION) |
| 168 | + |
| 169 | +env: ## Print environment variables |
| 170 | + @echo "PROJECT_NAME=$(PROJECT_NAME)" |
| 171 | + @echo "VERSION=$(VERSION)" |
| 172 | + @echo "GOLANG_MINIMUM_VERSION=$(GOLANG_MINIMUM_VERSION)" |
| 173 | + @echo "BINARY_NAME=$(BINARY_NAME)" |
| 174 | + @echo "DOCKER_REGISTRY=$(DOCKER_REGISTRY)" |
| 175 | + @echo "DOCKER_REPOSITORY=$(DOCKER_REPOSITORY)" |
| 176 | + @echo "DOCKER_IMAGE_PREFIX=$(DOCKER_IMAGE_PREFIX)" |
| 177 | + @echo "DOCKER_TAG=$(DOCKER_TAG)" |
| 178 | + @echo "DOCKER_IMAGENAME=$(DOCKER_IMAGENAME)" |
| 179 | + @echo "DOCKER_LABEL_VCS_URL=$(DOCKER_LABEL_VCS_URL)" |
| 180 | + @echo "DOCKER_LABEL_VCS_REF=$(DOCKER_LABEL_VCS_REF)" |
| 181 | + @echo "NPROCS=$(NPROCS)" |
| 182 | + |
| 183 | +## Phony targets |
| 184 | +.PHONY: all \ |
| 185 | + bin-dir \ |
| 186 | + build \ |
| 187 | + check \ |
| 188 | + check-reuse \ |
| 189 | + clean \ |
| 190 | + docker-build \ |
| 191 | + docker-clean \ |
| 192 | + docker-push \ |
| 193 | + env \ |
| 194 | + fmt \ |
| 195 | + help \ |
| 196 | + lint \ |
| 197 | + lint-local \ |
| 198 | + print-version \ |
| 199 | + test \ |
| 200 | + test-local \ |
| 201 | + webconsole-ui |
0 commit comments