blob: cc56f65629853df641a61c07915fe66d676c3c00 [file] [log] [blame]
Joey Armstrongaadcaa42024-02-01 19:54:17 -05001# Copyright 2020-2024 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandoloa4285862020-12-01 18:10:10 -08002#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Joey Armstrongee4d8262023-08-22 15:19:19 -040015$(if $(DEBUG),$(warning ENTER))
Matteo Scandoloa4285862020-12-01 18:10:10 -080016
Joey Armstrongee4d8262023-08-22 15:19:19 -040017.PHONY: help clean help test
18.DEFAULT_GOAL := help
19
20##-------------------##
21##---] GLOBALS [---##
22##-------------------##
23TOP ?=$(strip \
24 $(dir \
25 $(abspath $(lastword $(MAKEFILE_LIST)))\
26 )\
27)
28
29##--------------------##
30##---] INCLUDES [---##
31##--------------------##
32include $(TOP)/config.mk# # configure
33
34# -----------------------------------------------------------------------
35# https://jira.opencord.org/browse/VOL-5163
36# - only a tiny subset of logic is enabled right now.
37# - several targets can also be refactored across several repos
38# into additional library targets.
39# -----------------------------------------------------------------------
40include $(TOP)/makefiles/include.mk # top level include
Matteo Scandoloa4285862020-12-01 18:10:10 -080041
42# Variables
43VERSION ?= $(shell cat ./VERSION)
44
45DOCKER_LABEL_VCS_DIRTY = false
46ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
47 DOCKER_LABEL_VCS_DIRTY = true
48endif
49## Docker related
50DOCKER_EXTRA_ARGS ?=
51DOCKER_REGISTRY ?=
52DOCKER_REPOSITORY ?=
53DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
54IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}bbsim-sadis-server
55TYPE ?= minimal
56
57## Docker labels. Only set ref and commit date if committed
58DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
59DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
60DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
61DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
62
63DOCKER_BUILD_ARGS ?= \
64 ${DOCKER_EXTRA_ARGS} \
65 --build-arg org_label_schema_version="${VERSION}" \
66 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
67 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
68 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
69 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
70 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
71
72DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \
73 --build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
74
75# tool containers
David K. Bainbridge4242c722021-04-09 16:16:36 +000076VOLTHA_TOOLS_VERSION ?= 2.4.0
Matteo Scandoloa4285862020-12-01 18:10:10 -080077
78GO = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang go
79GO_JUNIT_REPORT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-go-junit-report go-junit-report
Matteo Scandoloabf872d2020-12-14 08:22:06 -100080GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/bbsim-sadis-server -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
Matteo Scandoloa4285862020-12-01 18:10:10 -080081GOLANGCI_LINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golangci-lint golangci-lint
82HADOLINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-hadolint hadolint
83
Joey Armstrongee4d8262023-08-22 15:19:19 -040084help ::
Matteo Scandoloa4285862020-12-01 18:10:10 -080085 @echo "TODO write the help"
86
Joey Armstrongee4d8262023-08-22 15:19:19 -040087.PHONY: build
Matteo Scandoloa4285862020-12-01 18:10:10 -080088build:
Joey Armstrongee4d8262023-08-22 15:19:19 -040089 $(call banner-enter,Target $@)
Matteo Scandoloa4285862020-12-01 18:10:10 -080090 @${GO} build -mod=vendor ./cmd/bbsim-sadis-server.go
Joey Armstrongee4d8262023-08-22 15:19:19 -040091 $(call banner-leave,Target $@)
Matteo Scandoloa4285862020-12-01 18:10:10 -080092
Joey Armstrongee4d8262023-08-22 15:19:19 -040093.PHONY: build-local
Matteo Scandoloa4285862020-12-01 18:10:10 -080094build-local:
95 @go build -mod=vendor ./cmd/bbsim-sadis-server.go
96
97## Docker targets
Joey Armstrongee4d8262023-08-22 15:19:19 -040098.PHONY: docker-build
Matteo Scandoloa4285862020-12-01 18:10:10 -080099docker-build:
100 docker build $(DOCKER_BUILD_ARGS) -t ${IMAGENAME}:${DOCKER_TAG} -f build/package/Dockerfile .
101
Joey Armstrongee4d8262023-08-22 15:19:19 -0400102.PHONY: docker-push
Matteo Scandoloa4285862020-12-01 18:10:10 -0800103docker-push:
104 docker push ${IMAGENAME}:${DOCKER_TAG}
105
Joey Armstrongee4d8262023-08-22 15:19:19 -0400106.PHONY: docker-kind-load
Matteo Scandoloa4285862020-12-01 18:10:10 -0800107docker-kind-load:
108 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
109 kind load docker-image ${IMAGENAME}:${DOCKER_TAG} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | rev | cut -c 2- | rev)
110
111## lint and unit tests
112
Joey Armstrongee4d8262023-08-22 15:19:19 -0400113.PHONY: lint-dockerfile
Matteo Scandoloa4285862020-12-01 18:10:10 -0800114lint-dockerfile:
115 @echo "Running Dockerfile lint check..."
116 @${HADOLINT} $$(find ./build -name "Dockerfile*")
117 @echo "Dockerfile lint check OK"
118
Joey Armstrongee4d8262023-08-22 15:19:19 -0400119## -----------------------------------------------------------------------
120## -----------------------------------------------------------------------
Matteo Scandoloa4285862020-12-01 18:10:10 -0800121lint-mod:
Joey Armstrongee4d8262023-08-22 15:19:19 -0400122 $(call banner-entry,Target $@)
Matteo Scandoloa4285862020-12-01 18:10:10 -0800123 @echo "Running dependency check..."
124 @${GO} mod verify
125 @echo "Dependency check OK. Running vendor check..."
126 @git status > /dev/null
127 @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Staged or modified files must be committed before running this test" && git status -- go.mod go.sum vendor && exit 1)
128 @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files must be cleaned up before running this test" && git status -- go.mod go.sum vendor && exit 1)
Joey Armstrongee4d8262023-08-22 15:19:19 -0400129
130 $(HIDE)$(MAKE) --no-print-directory mod-update
131
Matteo Scandoloa4285862020-12-01 18:10:10 -0800132 @git status > /dev/null
133 @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Modified files detected after running go mod tidy / go mod vendor" && git status -- go.mod go.sum vendor && git checkout -- go.mod go.sum vendor && exit 1)
134 @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files detected after running go mod tidy / go mod vendor" && git status -- go.mod go.sum vendor && git checkout -- go.mod go.sum vendor && exit 1)
135 @echo "Vendor check OK."
Joey Armstrongee4d8262023-08-22 15:19:19 -0400136 $(call banner-leave,Target $@)
Matteo Scandoloa4285862020-12-01 18:10:10 -0800137
138
Joey Armstrongee4d8262023-08-22 15:19:19 -0400139## -----------------------------------------------------------------------
140## -----------------------------------------------------------------------
141.PHONY: mod-update
Joey Armstrongaadcaa42024-02-01 19:54:17 -0500142mod-update: go-version mod-tidy mod-vendor
143
144## -----------------------------------------------------------------------
145## -----------------------------------------------------------------------
146.PHONY: go-version
147go-version :
148 $(call banner-enter,Target $@)
149 ${GO} version
150 $(call banner-leave,Target $@)
Joey Armstrongee4d8262023-08-22 15:19:19 -0400151
152## -----------------------------------------------------------------------
153## -----------------------------------------------------------------------
154.PHONY: mod-tidy
155mod-tidy :
156 $(call banner-enter,Target $@)
157 ${GO} mod tidy
158 $(call banner-leave,Target $@)
159
160## -----------------------------------------------------------------------
161## -----------------------------------------------------------------------
162.PHONY: mod-vendor
163mod-vendor : mod-tidy
164mod-vendor :
165 $(call banner-enter,Target $@)
166 $(if $(LOCAL_FIX_PERMS),chmod o+w $(CURDIR))
167 ${GO} mod vendor
168 $(if $(LOCAL_FIX_PERMS),chmod o-w $(CURDIR))
169 $(call banner-leave,Target $@)
170
171## -----------------------------------------------------------------------
172## -----------------------------------------------------------------------
Matteo Scandoloa4285862020-12-01 18:10:10 -0800173lint: lint-mod lint-dockerfile
174
Joey Armstrongee4d8262023-08-22 15:19:19 -0400175## -----------------------------------------------------------------------
176## Coverage report: Static code analysis
177## -----------------------------------------------------------------------
Matteo Scandoloa4285862020-12-01 18:10:10 -0800178sca:
179 @rm -rf ./sca-report
180 @mkdir -p ./sca-report
181 @echo "Running static code analysis..."
182 @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
183 @echo ""
184 @echo "Static code analysis OK"
185
Joey Armstrongee4d8262023-08-22 15:19:19 -0400186## -----------------------------------------------------------------------
187## -----------------------------------------------------------------------
Matteo Scandoloa4285862020-12-01 18:10:10 -0800188test:
189 @mkdir -p ./tests/results
190 @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
191 RETURN=$$? ;\
192 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
193 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
194 exit $$RETURN
195
Joey Armstrongee4d8262023-08-22 15:19:19 -0400196## -----------------------------------------------------------------------
197## -----------------------------------------------------------------------
Matteo Scandoloa4285862020-12-01 18:10:10 -0800198distclean:
Joey Armstrongee4d8262023-08-22 15:19:19 -0400199 $(RM) -r ./sca-report
Matteo Scandoloa4285862020-12-01 18:10:10 -0800200
Joey Armstrongee4d8262023-08-22 15:19:19 -0400201# [EOF]