blob: 2482efd5f78410f1056c78f9c171c26bedf9e983 [file] [log] [blame]
Joey Armstrongee4d8262023-08-22 15:19:19 -04001# Copyright 2020-2023 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
142mod-update: mod-tidy mod-vendor
143
144## -----------------------------------------------------------------------
145## -----------------------------------------------------------------------
146.PHONY: mod-tidy
147mod-tidy :
148 $(call banner-enter,Target $@)
149 ${GO} mod tidy
150 $(call banner-leave,Target $@)
151
152## -----------------------------------------------------------------------
153## -----------------------------------------------------------------------
154.PHONY: mod-vendor
155mod-vendor : mod-tidy
156mod-vendor :
157 $(call banner-enter,Target $@)
158 $(if $(LOCAL_FIX_PERMS),chmod o+w $(CURDIR))
159 ${GO} mod vendor
160 $(if $(LOCAL_FIX_PERMS),chmod o-w $(CURDIR))
161 $(call banner-leave,Target $@)
162
163## -----------------------------------------------------------------------
164## -----------------------------------------------------------------------
Matteo Scandoloa4285862020-12-01 18:10:10 -0800165lint: lint-mod lint-dockerfile
166
Joey Armstrongee4d8262023-08-22 15:19:19 -0400167## -----------------------------------------------------------------------
168## Coverage report: Static code analysis
169## -----------------------------------------------------------------------
Matteo Scandoloa4285862020-12-01 18:10:10 -0800170sca:
171 @rm -rf ./sca-report
172 @mkdir -p ./sca-report
173 @echo "Running static code analysis..."
174 @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
175 @echo ""
176 @echo "Static code analysis OK"
177
Joey Armstrongee4d8262023-08-22 15:19:19 -0400178## -----------------------------------------------------------------------
179## -----------------------------------------------------------------------
Matteo Scandoloa4285862020-12-01 18:10:10 -0800180test:
181 @mkdir -p ./tests/results
182 @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
183 RETURN=$$? ;\
184 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
185 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
186 exit $$RETURN
187
Joey Armstrongee4d8262023-08-22 15:19:19 -0400188## -----------------------------------------------------------------------
189## -----------------------------------------------------------------------
Matteo Scandoloa4285862020-12-01 18:10:10 -0800190distclean:
Joey Armstrongee4d8262023-08-22 15:19:19 -0400191 $(RM) -r ./sca-report
Matteo Scandoloa4285862020-12-01 18:10:10 -0800192
Joey Armstrongee4d8262023-08-22 15:19:19 -0400193# [EOF]