Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 1 | # Copyright 2020-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 2 | # |
| 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 Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 15 | $(if $(DEBUG),$(warning ENTER)) |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 16 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 17 | .PHONY: help clean help test |
| 18 | .DEFAULT_GOAL := help |
| 19 | |
| 20 | ##-------------------## |
| 21 | ##---] GLOBALS [---## |
| 22 | ##-------------------## |
| 23 | TOP ?=$(strip \ |
| 24 | $(dir \ |
| 25 | $(abspath $(lastword $(MAKEFILE_LIST)))\ |
| 26 | )\ |
| 27 | ) |
| 28 | |
| 29 | ##--------------------## |
| 30 | ##---] INCLUDES [---## |
| 31 | ##--------------------## |
| 32 | include $(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 | # ----------------------------------------------------------------------- |
| 40 | include $(TOP)/makefiles/include.mk # top level include |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 41 | |
| 42 | # Variables |
| 43 | VERSION ?= $(shell cat ./VERSION) |
| 44 | |
| 45 | DOCKER_LABEL_VCS_DIRTY = false |
| 46 | ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0) |
| 47 | DOCKER_LABEL_VCS_DIRTY = true |
| 48 | endif |
| 49 | ## Docker related |
| 50 | DOCKER_EXTRA_ARGS ?= |
| 51 | DOCKER_REGISTRY ?= |
| 52 | DOCKER_REPOSITORY ?= |
| 53 | DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true) |
| 54 | IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}bbsim-sadis-server |
| 55 | TYPE ?= minimal |
| 56 | |
| 57 | ## Docker labels. Only set ref and commit date if committed |
| 58 | DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) |
| 59 | DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD) |
| 60 | DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") |
| 61 | DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD) |
| 62 | |
| 63 | DOCKER_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 | |
| 72 | DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \ |
| 73 | --build-arg LOCAL_PROTOS=${LOCAL_PROTOS} |
| 74 | |
| 75 | # tool containers |
David K. Bainbridge | 4242c72 | 2021-04-09 16:16:36 +0000 | [diff] [blame] | 76 | VOLTHA_TOOLS_VERSION ?= 2.4.0 |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 77 | |
| 78 | GO = 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 |
| 79 | GO_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 Scandolo | abf872d | 2020-12-14 08:22:06 -1000 | [diff] [blame] | 80 | GOCOVER_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 Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 81 | GOLANGCI_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 |
| 82 | HADOLINT = 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 Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 84 | help :: |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 85 | @echo "TODO write the help" |
| 86 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 87 | .PHONY: build |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 88 | build: |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 89 | $(call banner-enter,Target $@) |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 90 | @${GO} build -mod=vendor ./cmd/bbsim-sadis-server.go |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 91 | $(call banner-leave,Target $@) |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 92 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 93 | .PHONY: build-local |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 94 | build-local: |
| 95 | @go build -mod=vendor ./cmd/bbsim-sadis-server.go |
| 96 | |
| 97 | ## Docker targets |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 98 | .PHONY: docker-build |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 99 | docker-build: |
| 100 | docker build $(DOCKER_BUILD_ARGS) -t ${IMAGENAME}:${DOCKER_TAG} -f build/package/Dockerfile . |
| 101 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 102 | .PHONY: docker-push |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 103 | docker-push: |
| 104 | docker push ${IMAGENAME}:${DOCKER_TAG} |
| 105 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 106 | .PHONY: docker-kind-load |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 107 | docker-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 Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 113 | .PHONY: lint-dockerfile |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 114 | lint-dockerfile: |
| 115 | @echo "Running Dockerfile lint check..." |
| 116 | @${HADOLINT} $$(find ./build -name "Dockerfile*") |
| 117 | @echo "Dockerfile lint check OK" |
| 118 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 119 | ## ----------------------------------------------------------------------- |
| 120 | ## ----------------------------------------------------------------------- |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 121 | lint-mod: |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 122 | $(call banner-entry,Target $@) |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 123 | @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 Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 129 | |
| 130 | $(HIDE)$(MAKE) --no-print-directory mod-update |
| 131 | |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 132 | @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 Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 136 | $(call banner-leave,Target $@) |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 137 | |
| 138 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 139 | ## ----------------------------------------------------------------------- |
| 140 | ## ----------------------------------------------------------------------- |
| 141 | .PHONY: mod-update |
| 142 | mod-update: mod-tidy mod-vendor |
| 143 | |
| 144 | ## ----------------------------------------------------------------------- |
| 145 | ## ----------------------------------------------------------------------- |
| 146 | .PHONY: mod-tidy |
| 147 | mod-tidy : |
| 148 | $(call banner-enter,Target $@) |
| 149 | ${GO} mod tidy |
| 150 | $(call banner-leave,Target $@) |
| 151 | |
| 152 | ## ----------------------------------------------------------------------- |
| 153 | ## ----------------------------------------------------------------------- |
| 154 | .PHONY: mod-vendor |
| 155 | mod-vendor : mod-tidy |
| 156 | mod-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 Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 165 | lint: lint-mod lint-dockerfile |
| 166 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 167 | ## ----------------------------------------------------------------------- |
| 168 | ## Coverage report: Static code analysis |
| 169 | ## ----------------------------------------------------------------------- |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 170 | sca: |
| 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 Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 178 | ## ----------------------------------------------------------------------- |
| 179 | ## ----------------------------------------------------------------------- |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 180 | test: |
| 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 Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 188 | ## ----------------------------------------------------------------------- |
| 189 | ## ----------------------------------------------------------------------- |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 190 | distclean: |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 191 | $(RM) -r ./sca-report |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 192 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame^] | 193 | # [EOF] |